RobotManage.cs 8.5 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    public static class RobotManage
    {
        public static MainMachine mainMachine;
        public static StoreMachine rightMachine;
        public static StoreMachine leftMachine;
        public static Robot_Config Config;
        public static R_Config RConfig;
        public static L_Config LConfig;

        public static List<IRobot> Stores = new List<IRobot>();
        public static bool IsLoadOk = true;
        public static bool IsConfigMode = false;
        public static bool InoutDebugMode = false;
        public static bool DisableUpdownProtect {
            get => Setting_Init.Device_DisableUpdownProtect;
        }
        public delegate void LoadFinish(bool state,string msg);
        public static event LoadFinish LoadFinishEvent;
        public static bool isRunning { get => Stores.Sum(x => x.isRunning ? 1 : 0) > 0; }
        public static Dictionary<string, ACStorePosition> allPositionMap { get => CSVPositionReader<ACStorePosition>.allPositionMap; }

        static List<string> _positionNumList=null;
        public static List<string> PositionNumList { get {
                if (_positionNumList == null)
                     _positionNumList = CSVPositionReader<ACStorePosition>.allPositionMap.Keys.ToList();
                return _positionNumList;
            }
        }
        static string baseDir = Application.StartupPath;
        static Thread mainThread=null;
        static Thread rightThread;
        static Thread leftThread;
        public static bool haveFixpos=false;


        public static HIKCamera CameraA=new HIKCamera();
        public static void Init() {
            string msg = "";
            try
            {
                mainMachine = null;                
                string configFile = Path.Combine(baseDir, "config\\Config.csv");
                Config = new Robot_Config(0, "", configFile);
                Config = (Robot_Config)CSVConfigReader.LoadConfig<IO_Type>(Config);
                RConfig = new R_Config(0, "", Path.Combine(baseDir, "config\\Right\\Config.csv"));
                RConfig = (R_Config)CSVConfigReader.LoadConfig<SIO_Type>(RConfig);
                LConfig = new L_Config(0, "", Path.Combine(baseDir, "config\\Left\\Config.csv"));
                LConfig = (L_Config)CSVConfigReader.LoadConfig<SIO_Type>(LConfig);

                //CodeManager.LoadConfig();
                mainMachine = new MainMachine(Config);
                rightMachine = new StoreMachine(RConfig, Config, MachineSideE.Right, out string rmsg);
                if (!string.IsNullOrEmpty(rmsg))
                    msg += rmsg;
                leftMachine = new StoreMachine(LConfig, Config, MachineSideE.Left, out string lmsg);
                if (!string.IsNullOrEmpty(rmsg))
                    msg += lmsg;
                Stores.Add(rightMachine);
                Stores.Add(leftMachine);

                //Thread.Sleep(5000);
                if (!IOManager.ConnectionIOList(new List<string>()))
                {
                    IsLoadOk = false;
                    msg += crc.GetString("Res0135","IO板卡初始化失败")+ "\n";
                }
                //if (!CameraA.LoadCameraConfig("CameraA", out string errmsg))
                //{
                //    IsLoadOk = false;
                //    msg += errmsg + "\r\n";
                //}
                if (!HumitureController.Init(Setting_Init.Device_Humiture_Port)) {
                    IsLoadOk = false;
                    msg += crc.GetString("Res0136","温湿度传感器初始化失败,端口:")+ Setting_Init.Device_Humiture_Port+"\n";
                }
                IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.HIGH,Config);

            }
            catch (Exception ex) {
                LoadFinishEvent?.Invoke(false, ex.Message);
                return;
            }
            LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
        }
        public static void LoadDebug() {
            LoadFinishEvent?.Invoke(true, crc.GetString("Res0137","打开调试模式"));
        }
        public static void Start(MachineSideE machineSide) {
            //Init();
            if (!IsLoadOk)
            {
                LogUtil.info("系统还未加载完毕,无法启动");
                if (!IsConfigMode)
                    return;
            }
            //isRunning = true;
            if (mainThread == null || !mainThread.IsAlive)
            {
                LogUtil.info("启动共用线程");
                mainThread = new Thread(new ThreadStart(mainMachine.Start));
                mainThread.Start();
                GC.KeepAlive(mainThread);
            }
            if ((machineSide & MachineSideE.Right) == MachineSideE.Right && !rightMachine.isRunning)
            {
                LogUtil.info("启动右料仓");
                rightThread = new Thread(new ThreadStart(rightMachine.Start));
                rightThread.Start();
                GC.KeepAlive(rightThread);
            }
            if ((machineSide & MachineSideE.Left) == MachineSideE.Left && !leftMachine.isRunning)
            {
                LogUtil.info("启动左料仓");
                leftThread = new Thread(new ThreadStart(leftMachine.Start));
                leftThread.Start();
                GC.KeepAlive(leftThread);
            }
            Thread.Sleep(500);
            Stores.ForEach(s => {
                if ((machineSide & s.MachineSide) == s.MachineSide && s.isRunning)
                {
                    LogUtil.info($"{machineSide}侧回原");
                    //AxisBean.List[s.MachineSide].ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
                    s.BeginHomeReset();
                }
            });
            //Task.Run(()=> {
            //    Task.Delay(1000).Wait();
            //    if (mainMachine.DeviceCheck())
            //    {                   
            //        Stores.ForEach(s=> {
            //            if ((machineSide & s.MachineSide) == s.MachineSide && s.isRunning)
            //            {
            //                LogUtil.info($"{machineSide}侧回原");
            //                AxisBean.List[s.MachineSide].ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
            //                s.BeginHomeReset();
            //            }
            //        });
            //    }
            //});
            Thread.Sleep(100);
        }

        public static void Stop()
        {
            LogUtil.info("开始停止系统.");
            if (mainMachine != null)
            {
                mainMachine.Stop();
                Stores.ForEach(s => s.Stop());
                Stores.ForEach(s => LogUtil.info(s.Name +": "+s.isRunning.ToString()));
            }
            //isRunning = false;
        }
        public static void ShutDown()
        {
            LogUtil.info("开始关闭系统.");
            IOManager.CloseAllConnection();
            CameraA.stopCamera();
            if (mainMachine != null)
            {
                Stores.ForEach(s => s.ShutDown());
            }
        }
        public static void UserPause(bool userpause)
        {
            UserPause("", userpause);
        }
        public static void UserPause(string msg="",bool userpause=true) {
            mainMachine.UserPause = userpause;
            if (userpause)
            {
                if(string.IsNullOrEmpty(msg))
                    LogUtil.info("用户暂停");
                else
                    LogUtil.info("系统暂停: "+msg);
            }
            else
                LogUtil.info("用户取消暂停:"+ msg);
        }
        public static void IgnoreSafecheck(bool s)
        {
            RobotManage.Stores.ForEach(so => so.IgnoreSafecheck = s);
            if (s)
                LogUtil.info("用户设置忽略安全检查");
            else
                LogUtil.info("用户取消忽略安全检查");
        }
        public static void IgnoreGratingSignal(bool s)
        {
            mainMachine.IgnoreGratingSignal = s;
            if (s)
                LogUtil.info("用户设置忽略安全光栅");
            else
                LogUtil.info("用户取消忽略安全光栅");
        }
    }

    public enum StoreType { 
    TypeA,TypeB
    }
}