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

namespace DeviceLibrary
{
    public static class RobotManage
    {
        public static event EventHandler<bool> UserPauseSet;
        public static MainMachine mainMachine;
        public static Robot_Config Config;
        public static bool IsLoadOk = true;
        public static bool IsDebug = false;
        public delegate void LoadFinish(bool state, string msg);
        public static event LoadFinish LoadFinishEvent;
        public static bool isRunning = false;
        public static PrinterHelper printerHelper = new PrinterHelper();
        /// <summary>
        /// 抓取料盘夹具
        /// </summary>
        public static ClampTool clampTool;
        static string baseDir = Application.StartupPath;
        static Thread mainThread;
        public static void Init()
        {
            try
            {
                mainMachine = null;

                string msg = "";
                string configFile = Path.Combine(baseDir, "config\\Config.csv");
                Config = new Robot_Config(0, "", configFile);
                Config = (Robot_Config)CSVConfigReader.LoadConfig(Config);
                CodeManager.LoadConfig();

                clampTool = new ClampTool();
                if (!clampTool.Open())
                {
                    msg += crc.GetString("Res0188", "打开夹爪失败,{0}",0) + "\n";
                    IsLoadOk = false;
                }

                mainMachine = new MainMachine(RobotManage.Config);
                if (!IOManager.ConnectionIOList(new List<string>()))
                {
                    IsLoadOk = false;
                    msg += crc.GetString("Res0189", "IO板卡初始化失败") + "\n";
                }
                if (!printerHelper.Connection(""))
                {
                    msg += crc.GetString("Res0217", "标签打印机打开失败") + "\n";
                    IsLoadOk = false;
                }
                else
                {
                    LogUtil.info("标签打印机打开成功");
                }

               // IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.HIGH);
                IOManager.IOMove(IO_Type.Camera_Led, IO_VALUE.HIGH);
                LoadFinishEvent?.Invoke(IsDebug ? IsDebug : IsLoadOk, msg);

            }
            catch (Exception ex)
            {
                LoadFinishEvent?.Invoke(false, ex.Message);
            }
        }
        public static void LoadDebug()
        {
            LoadFinishEvent?.Invoke(true, crc.GetString("Res0190", "打开配置模式"));
        }
        public static void Start()
        {
            //Init();
            if (!IsLoadOk)
            {
                LogUtil.info("系统还未加载完毕,无法启动");
                if (!IsDebug)
                    return;
            }
            mainMachine.ServerCM.StartConnectServer();
            clampTool.Open();
            mainThread = new Thread(new ThreadStart(mainMachine.Run));
            mainThread.Start();
            isRunning = true;
            GC.KeepAlive(mainThread);
            Task.Run(() =>
            {
                AxisBean.List.ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
                Task.Delay(1000).Wait();
                if (mainMachine.DeviceCheck())
                    mainMachine.BeginHomeReset(true);
            });
        }

        public static void Stop()
        {
            LogUtil.info("开始停止系统.");
            mainMachine?.ServerCM?.StopConnectServer();
            mainMachine?.Stop();
            mainMachine.UserPause = false;
            isRunning = false;
        }
        public static void ShutDown()
        {
            LogUtil.info("开始关闭系统.");
            IOManager.CloseAllDO();
            IOManager.CloseAllConnection();
            clampTool.Close();
        }
        public static void UserPause(bool userpause)
        {
            UserPause("", userpause);
            if (userpause)
                LogUtil.info("用户暂停");
            else
                LogUtil.info("用户取消暂停");
        }
        public static void IgnoreSafecheck(bool s)
        {
            mainMachine.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 static void LoadPrintSetting()
        {
            string PrintName = ConfigHelper.Config.Get(Setting_Init.PrinterName);
            string labelName = ConfigHelper.Config.Get(Setting_Init.LabelName);

            //RobotManage.printerHelper.Print(labelName, new Dictionary<string, string>(), out string msg);
            LogUtil.info("PrintLabel 打印机初始化完成【" + labelName + "】【" + PrintName + "】");
        }
        public static void UserPause(string msg = "", bool userpause = true)
        {
            UserPauseSet?.Invoke(null, userpause);
            mainMachine.UserPause = userpause;
            if (userpause)
            {
                if (string.IsNullOrEmpty(msg))
                    LogUtil.info("用户暂停");
                else
                    LogUtil.info("系统暂停: " + msg);
            }
            else
                LogUtil.info("用户取消暂停:" + msg);
        }
    }
}