MainMachine.cs 5.7 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    public partial class MainMachine// : IRobot<Robot_Config>
    {
        public string Name { get; set; } = "DualSISO";
        private bool _canRunning = true;
        public bool canRunning
        {
            get { return _canRunning; }
            set
            {
                if (_canRunning != value) {
                    //Msg.setlogones();
                }
                _canRunning = value;
            }
        }
        public bool isAlarm { get=>RobotManage.Stores.Sum(s=>s.isAlarm?1:0)>0;}
        public RunStatus runStatus { get; set; } = RunStatus.Stop;
        public Robot_Config Config { get; set; }
        public bool UserPause { get; set; } = false;



        /// <summary>
        /// 是否在急停中
        /// </summary>
        public bool isInSuddenDown = false;



        public MainMachine(Robot_Config _config) {
            Config = _config;
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;

            #region 初始化led
            RunningLed = new Led(Config.DOList[IO_Type.Run_Led].GetIOAddr(),LedColor.green);
            StandbyLed = new Led(Config.DOList[IO_Type.Standby_Led].GetIOAddr(), LedColor.yellow);
            AlarmLed = new Led(Config.DOList[IO_Type.Alarm_Led].GetIOAddr(), LedColor.red);
            
            #endregion
            #region 初始化伺服轴
            Crc_LanguageChangeEvent(null, EventArgs.Empty);
            #endregion

            AlarmBuzzer.SetOnOffAction(() =>{ IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.HIGH,Config); }, () => { IOMove(IO_Type.Alarm_Buzzer, IO_VALUE.LOW, Config); });

            IOMonitor.RegisterIO(IO_Type.Reset_BTN, Config, IO_VALUE.HIGH, Reset_BTN, 2500, 100);
            LedProcessInit();

            
        }

        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {


        }
        
        //public bool hasAlarm = false;
        /// <summary>
        /// 整机启动变量,设置为false后将退出线程,只在停止时调用
        /// </summary>
        bool mstart=true;
        public void Run() {
            mstart = true;
            while (mstart) {
                try
                {
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        BtnProcess();
                        canRunning = SafeCheck();
                    }
                    Thread.Sleep(200);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        ioMonitor();
                    }
                    else if (runStatus == RunStatus.HomeReset)
                    {
                        //HomeReset();
                    }
                }
                catch (Exception ex)
                {
                    MsgService.Add(ex.ToString(), MsgLevel.warning);
                }
                finally {
                    if (isAlarm)
                    {                        
                        AlarmBuzzer.OFF();
                    }
                    else {
                        AlarmBuzzer.ON();
                    }
                }
            }
            LogUtil.info("主线程已退出.");
        }
        public void Start() {
            Run();
        }
        public void Stop() {
            mstart = false;
            UserPause = false;
            Thread.Sleep(300);
            LedProcess(null);
        }
        
        public bool IgnoreGratingSignal = false;

        bool lastSafeCheckStatus = true;
        bool SafeCheck() {
            bool ok = true;            
            lastSafeCheckStatus = ok;
            return ok;
        }
        void DeviceSuddenStop() {           
            if (lastSafeCheckStatus)
            {
            }
        }

        public bool DeviceCheck() {
            bool ok = true;
            isInSuddenDown = IOValue(IO_Type.SuddenStop_BTN, Config).Equals(IO_VALUE.LOW);
            if (UserPause)
            {
                MsgService.Add(crc.GetString("Res0133","系统暂停"), MsgLevel.warning);
                DeviceSuddenStop();
                lastSafeCheckStatus = false;
                ok = false;
                return ok;
            }
            else if (isInSuddenDown)
            {
                MsgService.Add(crc.GetString("Res0134","急停中"), MsgLevel.alarm);
                ok = false;
            }       
            
            return ok;
        }

        public IO_VALUE IOValue(string ioType, DeviceConfig config) => IOManager.IOValue(ioType, config);
        void IOMove(string IoType, IO_VALUE value, DeviceConfig config, bool isCheck = false, int msTime = 0)
        {
            if (msTime <= 0)
            {
                if (isCheck && (IOValue(IoType, config).Equals(value)))
                {
                    return;
                }
                IOManager.IOMove(IoType, value, config);
            }
            else
            {
                Task.Run(() =>
                {
                    IOManager.IOMove(IoType, value, config);
                    Thread.Sleep(msTime);
                    IO_VALUE tValue = value.Equals(IO_VALUE.HIGH) ? IO_VALUE.LOW : IO_VALUE.HIGH;

                    LogUtil.info(Name + "定时回写IO:  [" + IoType + "]=[" + value + "],msTime=" + msTime);

                    IOManager.IOMove(IoType, tValue, config);
                });
            }
        }
    }
}