RobotManage.cs 8.2 KB
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using X_Ray;

namespace DeviceLibrary
{
    using crc = OnlineStore.CodeResourceControl;
    public static class RobotManage
    {
        public static XRayMachine xrayMachine;
        public static LabelMachine labelMachine;
        public static FilterMachine filterMachine;
        public static T1Machine t1Machine;
        public static XRay XRay=new XRay("XRay");
        public static XrayImage xrayImage = new Asa.XrayImage("XrayImage", XrayImage.DeviceType.HAOBO_V2);
        public static Config_XRay Config;
        public static bool IsLoadOk = true;
        public static bool IsDebug = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="state">加载成功状态</param>
        /// <param name="msg">加载消息</param>
        public delegate void LoadFinish(bool state,string msg);
        public static event LoadFinish LoadFinishEvent;
        public static bool isRunning = false;
        public static Asa.PrintLabel PrintBean;
        public static ElectricGripper electricGripper;
        public static List<IRobot> robots = new List<IRobot>();
        public static WistonAgvClient wistonAgvClient;
        public static bool offlinemode = false;
        public static ReelLocation offlinereelLocation = new ReelLocation();
        public static LineRunMonitor Line1;
        public static LineRunMonitor Line2;
        public static int ProductivityCount = 0;
        public static int DefectiveCount = 0;
        //static string baseDir = Application.StartupPath;
        public static void Init() {
            try
            {
                string msgs = "";
                xrayMachine = new XRayMachine();
                labelMachine = new LabelMachine();
                filterMachine = new FilterMachine();
                t1Machine = new T1Machine();


                wistonAgvClient = new WistonAgvClient();
                RemoteService.Init();
                robots.AddRange(new IRobot[] { xrayMachine, labelMachine, filterMachine, t1Machine });
                //robots.AddRange(new IRobot[] { xrayMachine });
                robots.ForEach((device)=> {
                    LogUtil.info(device.DeviceName + " init");
                    if (!device.Init(out string msg))
                    {
                        IsLoadOk = false;
                        msgs += $"{device.DeviceNameShow} Init Failed\n";
                    }
                    LogUtil.info(device.DeviceName + " init end");
                });
                
                CodeManager.LoadConfig();
                PrintBean = new Asa.PrintLabel(Application.StartupPath + "\\Label",100);
                labelMachine.InitPrint();
                if (!XRay.Open(ConfigHelper.Config.Get("XRay_Port"), string.Format("{0:yyyy-MM-dd}", DateTime.Now)))
                {
                    msgs += crc.GetString("device_xray_open_failed","X光管通讯失败:{0}\n", ConfigHelper.Config.Get("XRay_Port"));
                    IsLoadOk = false;
                }
                else 
                    LogUtil.info("xray init ok");

                Thread.Sleep(500);

                if (!XRay.SetVC(ConfigHelper.Config.Get("XRay_Voltage"), ConfigHelper.Config.Get("XRay_Current")))
                {
                    msgs += crc.GetString("device_xray_setvc_failed", "X光电压电流设置失败:{0},{1}\n", ConfigHelper.Config.Get("XRay_Voltage"), ConfigHelper.Config.Get("XRay_Current"));
                    IsLoadOk = false;
                }
                else
                    LogUtil.info($"xray V:{ConfigHelper.Config.Get("XRay_Voltage")} ,C:{ConfigHelper.Config.Get("XRay_Current")} set ok");

                if (!xrayImage.Open()) {
                    msgs += crc.GetString("device_xrayimage_open_failed", $"图像平板打开失败\n");
                    IsLoadOk = false;
                }
                //xrayImage.Close();

                var ElectricGripperPort = ConfigHelper.Config.Get("ElectricGripperPort");
                electricGripper = new ElectricGripper(ElectricGripperPort);
                if (!electricGripper.OpenPort()) {
                    msgs += crc.GetString("device_electricGripper_open_failed", "电夹爪通讯失败:{0}\n", ElectricGripperPort);
                    IsLoadOk = false;
                }
                electricGripper.HomeReset();
                //mainMachine = new MainMachine(RobotManage.Config);
                if (!IOManager.ConnectionIO())
                {
                    IsLoadOk = false;
                    msgs += crc.GetString("device_io_open_failed", "IO板卡初始化失败\n");
                }
                Line1 = new LineRunMonitor("line1", labelMachine.Config.DOList[IO_Label_Type.Line1_Run].GetIOAddr());
                Line2 = new LineRunMonitor("line2", labelMachine.Config.DOList[IO_Label_Type.Line2_Run].GetIOAddr());
                LogUtil.info($"XRay StartDate:{UseData.GetStartDateTime},TotalSecond:{UseData.GetTotalUseSeconds},TotalTimes:{UseData.GetTimes}");

                var wiston_spnlist = ConfigHelper.Config.Get<string[]>("wiston_spnlist");
                if (wiston_spnlist == null)
                    wiston_spnlist = new string[] { "" };
                xrayMachine.pnlst = new HashSet<string>(wiston_spnlist);
                xrayMachine.relmove = ConfigHelper.Config.Get<int>("wiston_relmove", 50);
                xrayMachine.ptrycount = ConfigHelper.Config.Get<int>("wiston_strycount", 3);
                var a= ConfigHelper.Config.Get<int>("wiston_smaxcount", 10500);
                LogUtil.info("加载到特殊pn序列" + string.Join(",", ConfigHelper.Config.Get<string[]>("wiston_spnlist")));
                LogUtil.info("加载到相对移动偏移" + xrayMachine.relmove+",重试次数"+ xrayMachine.ptrycount);

                LoadFinishEvent?.Invoke(IsDebug?IsDebug:IsLoadOk, msgs);
            }
            catch (Exception ex) {
                LoadFinishEvent?.Invoke(false, ex.ToString());
                LogUtil.error(ex.ToString());
            }
        }
        public static void LoadDebug() {
            LoadFinishEvent?.Invoke(true, crc.GetString("open_debugmode","打开调试模式"));
        }
        public static void Start()
        {
            if (!IsLoadOk)
            {
                LogUtil.info("系统还未加载完毕,无法启动");
                if (!IsDebug)
                    return;
            }
            robots.ForEach((device) => device.Run());
            isRunning = true;
            Task.Run(() =>
            {
                Task.Delay(1000).Wait();
                //开始初始化回原
                robots.ForEach((device) =>
                {
                    if (device.DeviceCheck())
                        device.BeginHomeReset(true);
                });
            });
        }

        public static void Stop()
        {
            LogUtil.info("开始停止系统.");

            robots.ForEach((device) => device.Stop());
            isRunning = false;
            robots.ForEach((device) => device.UserPause = false);
        }

        public static void ShutDown() {
            LogUtil.info("开始关闭系统.");
            IOManager.CloseAllConnection();
            electricGripper.ClosePort();
        }
        public static void UserPause(bool userpause) {
            //xrayMachine.UserPause = userpause;
            robots.ForEach((device) => device.UserPause= userpause);
            if (userpause)
                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);
            PrintBean.LoadLabel(labelName);
            PrintBean.Printer(PrintName, false);

            LogUtil.info("PrintLabel 打印机初始化完成【" + labelName + "】【" + PrintName + "】");
        }
    }
}