RobotManage.cs 7.4 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using Robot.UR;
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 Robot_Config Config;
        public static Dictionary<string,DeviceGroup> DeviceGroup;
        public static bool IsLoadOk = true;
        public static bool IsConfigMode = false;

        public delegate void LoadFinish(bool state,string msg);
        public static event LoadFinish LoadFinishEvent;

        public static event EventHandler<bool> UserPauseSet;
        public static bool isRunning = false;

        static string baseDir = Application.StartupPath;
        static Thread mainThread;
        internal static bool InoutDebugMode;


        public static Dictionary<string, RobotPosition> MI1Postion;
        public static Dictionary<string, RobotPosition> MI2Postion;
        public static Dictionary<string, RobotPosition> CIPostion;
        public static URRobotControl Robot_MI1;
        public static URRobotControl Robot_MI2;
        public static URRobotControl Robot_CI;
        public static VStoreCollection VStoreCollection;

        public static void Init() {
            string msg = "";
            try
            {
                mainMachine = null;
                string configFile = "config\\Config.csv";
                Config = new Robot_Config(0, "", configFile);
                Config = (Robot_Config)CSVConfigReader.LoadConfig(Config);
                string DeviceGroupFile = "config\\DeviceGroup.csv";
                DeviceGroup = CSVPositionReader<DeviceGroup>.AddCSVFile(DeviceGroupFile);
                if (File.Exists(configFile))
                {
                    LogUtil.info("加载位置文件:" + configFile);
                }
                else {
                    IsLoadOk = false;
                    msg += crc.GetString("Res0180","找不到库位配置文件")+ "\n";
                }

                string MI1PostionFile = "config\\MI1Postion.csv";
                string MI2PostionFile = "config\\MI2Postion.csv";
                string CIPostionFile = "config\\CIPostion.csv";
                MI1Postion = CSVPositionReader<RobotPosition>.LoadCSVFile(MI1PostionFile);
                MI2Postion = CSVPositionReader<RobotPosition>.LoadCSVFile(MI2PostionFile);
                CIPostion = CSVPositionReader<RobotPosition>.LoadCSVFile(CIPostionFile);

                Robot_MI1 = new URRobotControl(Setting_Init.URRobot_MI1_IP);
                Robot_MI1.SetSpeedRate(Setting_Init.URRobot_MI1_Speed_Rate);
                Robot_MI2 = new URRobotControl(Setting_Init.URRobot_MI2_IP);
                Robot_MI2.SetSpeedRate(Setting_Init.URRobot_MI2_Speed_Rate);
                Robot_CI = new URRobotControl(Setting_Init.URRobot_CI_IP);
                Robot_CI.SetSpeedRate(Setting_Init.URRobot_CI_Speed_Rate);

                VStoreCollection = new VStoreCollection();
                //if (Robot_MI1.StartRobot()) {
                //    LogUtil.info("MI1 UR机器人启动成功,"+ Robot_MI1.GetStatus());
                //} else {
                //    IsLoadOk = false;
                //    msg += "MI1 UR机器人启动失败" + "\n";
                //}
                //if (Robot_MI2.StartRobot())
                //{
                //    LogUtil.info("MI2 UR机器人启动成功," + Robot_MI2.GetStatus());
                //}
                //else
                //{
                //    IsLoadOk = false;
                //    msg += "MI2 UR机器人启动失败" + "\n";
                //}
                //if (Robot_CI.StartRobot())
                //{
                //    LogUtil.info("CI UR机器人启动成功," + Robot_CI.GetStatus());
                //}
                //else
                //{
                //    IsLoadOk = false;
                //    msg += "CI UR机器人启动失败" + "\n";
                //}
                CodeManager.LoadConfig();
                RemoteService.Init();
                mainMachine = new MainMachine(out string m);
                if (!string.IsNullOrWhiteSpace(m)) {
                    IsLoadOk = false;
                    msg += m;
                }
                //Thread.Sleep(5000);
                if (!IOManager.ConnectionIOList(new List<string>()))
                {
                    IsLoadOk = false;
                    msg += crc.GetString("Res0181","IO板卡初始化失败")+ "\n";
                }



                //IsLoadOk = true;
            }
            catch (Exception ex) {
                LogUtil.error(ex.ToString());
                MessageBox.Show(ex.ToString());
                LoadFinishEvent?.Invoke(false, ex.Message);
                return;
            }
            LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
        }
        public static void LoadDebug() {
            LoadFinishEvent?.Invoke(true, crc.GetString("Res0182","打开调试模式"));
        }
        public static void Start() {
            //Init();
            if (!IsLoadOk)
            {
                LogUtil.info("系统还未加载完毕,无法启动");
                if (!IsConfigMode)
                    return;
            }
            mainThread = new Thread(new ThreadStart(mainMachine.Start));
            mainThread.Start();
            VStoreCollection.Start();
            Thread.Sleep(50);
            var d1 = new DeviceRunControl("SideMove", SideMove.DeviceList.Values.ToList<IDevice>());
            var d2 = new DeviceRunControl("TransplantMove", TransplantMove.DeviceList.Values.ToList<IDevice>());
            var d3 = new DeviceRunControl("TrayStop", TrayStop.DeviceList.Values.ToList<IDevice>());
            var d4 = new DeviceRunControl("MI", MI.DeviceList.Values.ToList<IDevice>());

            isRunning = true;
            GC.KeepAlive(mainThread);
            mainMachine.BeginHomeReset(true);

            d1.Start();
            d2.Start();
            d3.Start();
            d4.Start();

        }

        public static void Stop()
        {
            LogUtil.info("开始停止系统.");
            if (mainMachine != null)
            {
                mainMachine.Stop();
                mainMachine.UserPause = false;
            }
            VStoreCollection.Stop();
            isRunning = false;
        }
        public static void ShutDown()
        {
            LogUtil.info("开始关闭系统.");
            IOManager.CloseAllConnection();
            VStoreCollection.ShutDown();
            Robot_MI1.StopRobot();
            Robot_MI2.StopRobot();
            Robot_CI.StopRobot();
        }
        public static void UserPause(bool userpause)
        {
            UserPause("", userpause);
        }
        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);
                Thread.Sleep(500);
            }
            else
                LogUtil.info("用户取消暂停:"+ msg);
        }
    }

    public enum StoreType { 
    TypeA,TypeB
    }
}