MainMachine _LedProcess.cs 10.8 KB
using CodeLibrary;
using Newtonsoft.Json;
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
{
    partial class MainMachine
    {
        Led AlarmLed;
        Led StandbyLed;
        Led RunningLed;
        public Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>> MachineLedState = new Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>>();
        public Dictionary<MachineLedStateE, string> MachineLedStateName = new Dictionary<MachineLedStateE, string>();
        System.Threading.Timer ledtimer;
        void LedProcessInit()
        {
            DefaultLedCfg();
            LoadLedCfg();

            ledtimer = new System.Threading.Timer(new TimerCallback(LedProcess), null, 0, 1000);
            GC.KeepAlive(ledtimer);
        }
        public void LoadLedCfg()
        {
            if (File.Exists("Config\\LedConfig.json"))
            {
                var s = File.ReadAllText("Config\\LedConfig.json");
                try
                {
                    var ledcfgs = JsonConvert.DeserializeObject<Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>>>(s);
                    if (ledcfgs != null)
                        foreach (var key in ledcfgs.Keys)
                            MachineLedState[key] = ledcfgs[key];
                }
                catch (Exception e)
                {
                    LogUtil.error("灯塔亮灯配置加载失败:" + e.ToString());
                }
            }
        }
        public bool SaveLedCfg()
        {
            try
            {
                var cfgtxt = JsonConvert.SerializeObject(MachineLedState);
                File.WriteAllText("Config\\LedConfig.json", cfgtxt);
                return true;
            }
            catch (Exception e)
            {
                LogUtil.info("灯塔亮灯配置保存失败:" + e.ToString());
                return false;
            }
        }
        public void DefaultLedCfg()
        {
            MachineLedState.Clear();
            //安全光栅触发报警,红亮
            MachineLedStateName[MachineLedStateE.SafetyLightCurtains] = crc.GetString("ledstate_SafetyLightCurtains", "安全光栅触发");
            MachineLedState.Add(MachineLedStateE.SafetyLightCurtains, nls(LedState.on, LedState.off, LedState.off));
            //系统停止,红亮
            MachineLedStateName[MachineLedStateE.Stop] = crc.GetString("ledstate_Stop", "停机");
            MachineLedState.Add(MachineLedStateE.Stop, nls(LedState.on, LedState.off, LedState.off));
            //系统报警并停止,红亮
            MachineLedStateName[MachineLedStateE.AlarmStop] = crc.GetString("ledstate_AlarmStop", "停机报警");
            MachineLedState.Add(MachineLedStateE.AlarmStop, nls(LedState.on, LedState.off, LedState.off));
            //系统运行时报警, 绿亮,红闪
            MachineLedStateName[MachineLedStateE.Alarm] = crc.GetString("ledstate_Alarm", "报警");
            MachineLedState.Add(MachineLedStateE.Alarm, nls(LedState.blink, LedState.off, LedState.on));
            //系统复位中 绿闪
            MachineLedStateName[MachineLedStateE.HomeReset] = crc.GetString("ledstate_HomeReset", "复位");
            MachineLedState.Add(MachineLedStateE.HomeReset, nls(LedState.off, LedState.off, LedState.blink));
            //系统正常运行
            MachineLedStateName[MachineLedStateE.Running] = crc.GetString("ledstate_Running", "运行");
            MachineLedState.Add(MachineLedStateE.Running, nls(LedState.none, LedState.none, LedState.on));
            //调试,红亮
            MachineLedStateName[MachineLedStateE.Debug] = crc.GetString("ledstate_Debug", "调试");
            MachineLedState.Add(MachineLedStateE.Debug, nls(LedState.on, LedState.off, LedState.off));
            //系统暂停, 绿闪,红闪
            MachineLedStateName[MachineLedStateE.SystemPause] = crc.GetString("ledstate_SystemPause", "暂停");
            MachineLedState.Add(MachineLedStateE.SystemPause, nls(LedState.blink, LedState.off, LedState.blink));
            //温湿度超限30分钟.红闪,黄闪
            MachineLedStateName[MachineLedStateE.THoutRangeOver30m] = crc.GetString("Res0031.cbc9a6e9", "温湿度超限30分钟");
            MachineLedState.Add(MachineLedStateE.THoutRangeOver30m, nls(LedState.blink, LedState.blink, LedState.none));
            // 温湿度超限 绿闪黄闪
            MachineLedStateName[MachineLedStateE.THoutRange] = crc.GetString("ledstate_THOutRange", "温湿度超限");
            MachineLedState.Add(MachineLedStateE.THoutRange, nls(LedState.none, LedState.blink, LedState.blink));
            // 进出库, 绿亮,黄闪
            MachineLedStateName[MachineLedStateE.InOut] = crc.GetString("ledstate_InOut", "出入库中");
            MachineLedState.Add(MachineLedStateE.InOut, nls(LedState.none, LedState.blink, LedState.on));
        }
        Dictionary<LedColor, LedState> nls(LedState AlarmLedstate, LedState StandbyLedstate, LedState RunningLedstate)
        {
            var a = new Dictionary<LedColor, LedState>
            {
                { LedColor.red, AlarmLedstate },
                { LedColor.yellow, StandbyLedstate },
                { LedColor.green, RunningLedstate }
            };
            return a;
        }
        void ProcessLefCfg(MachineLedStateE machineLedStateE)
        {

            if (!MachineLedState.ContainsKey(machineLedStateE))
            {
                LogUtil.info("灯塔配置未包含:" + machineLedStateE.ToString());
                return;
            }

            var ledcfg = MachineLedState[machineLedStateE];

            foreach (var ledcolor in ledcfg.Keys)
            {
                if (ledcfg[ledcolor] == LedState.none)
                    continue;
                Led.LedColors[ledcolor].LedState = ledcfg[ledcolor];
            }
        }
        void LedProcess(object o)
        {
            StandbyLed.LedState = LedState.off;
            AlarmLed.LedState = LedState.off;
            RunningLed.LedState = LedState.off;
            if (SafetyLightStop)
            {
                ProcessLefCfg(MachineLedStateE.SafetyLightCurtains);
            }
            else
            {
                //回原 绿闪
                if (runStatus == RunStatus.HomeReset)
                {
                    ProcessLefCfg(MachineLedStateE.HomeReset);
                }
                //正常 绿亮
                else if (runStatus == RunStatus.Running)
                {
                    ProcessLefCfg(MachineLedStateE.Running);

                    //出入库 绿闪 黄闪
                    if (ClampMoveInfo.MoveStep > MoveStep.Wait
                    || StoreMoveInfo.MoveStep > MoveStep.Wait
                    || (StringMoveInfo.MoveStep > MoveStep.Wait && StringMoveInfo.MoveStep != MoveStep.StringReadyPut && StringMoveInfo.MoveStep <= MoveStep.StringOut_01))
                    {
                        ProcessLefCfg(MachineLedStateE.InOut);
                    }
                    //温度超限 绿亮 黄闪
                    if (IsTHoutRange())
                    {
                        ProcessLefCfg(MachineLedStateE.THoutRange);
                    }
                    //温度超限30分钟 绿亮 黄闪 红闪
                    if (IsTHoutRangeOver30m())
                    {
                        ProcessLefCfg(MachineLedStateE.THoutRangeOver30m);
                    }
                    if (AutoInOutTest)
                    {
                        ProcessLefCfg(MachineLedStateE.Debug);
                    }
                    //系统暂停,说明书未定义, 绿闪, 红闪
                    if (!canRunning || UserPause)
                    {
                        ProcessLefCfg(MachineLedStateE.SystemPause);
                    }
                }
                else if (runStatus == RunStatus.Stop)
                {
                    //系统停止时有报警, 红亮
                    if (hasAlarm)
                    {
                        ProcessLefCfg(MachineLedStateE.AlarmStop);
                    }
                }
                //系统运行时报警, 绿亮,红闪
                if (runStatus != RunStatus.Stop && hasAlarm)
                {
                    ProcessLefCfg(MachineLedStateE.Alarm);

                    //if (UserPause) {
                    //    RunningLed.LedState = LedState.blink;
                    //    StandbyLed.LedState = LedState.blink;
                    //}

                    //if (ResetMoveInfo.MoveStep >= MoveStep.H13_HomeReset && ResetMoveInfo.MoveStep <= MoveStep.H14_HomeReset)
                    //{
                    //    StandbyLed.LedState = LedState.blink;
                    //}
                    //if (ClampMoveInfo.MoveStep >= MoveStep.NGOUT_02 && ClampMoveInfo.MoveStep <= MoveStep.NGOUT_03)
                    //{
                    //    StandbyLed.LedState = LedState.blink;
                    //}
                }
            }

            Led.LedGroup.ForEach((x) => { x.run(); });
        }
    }
    public class Led
    {
        public static List<Led> LedGroup = new List<Led>();
        public static Dictionary<LedColor, Led> LedColors = new Dictionary<LedColor, Led>();
        public LedState LedState = LedState.off;
        public LedColor Color = LedColor.green;
        ushort ledio;
        public Led(ushort io, LedColor ledColor)
        {
            Color = ledColor;
            ledio = io;
            LedGroup.Add(this);
            LedColors.Add(ledColor, this);
        }
        IO_VALUE iovalue;
        IO_VALUE lastiovalue;
        public void run()
        {
            if (this.LedState == LedState.on)
            {
                iovalue = IO_VALUE.HIGH;
            }
            if (this.LedState == LedState.off)
            {
                iovalue = IO_VALUE.LOW;
            }
            if (this.LedState == LedState.blink)
            {
                if (iovalue == IO_VALUE.LOW)
                {
                    iovalue = IO_VALUE.HIGH;
                }
                else
                {
                    iovalue = IO_VALUE.LOW;
                }
            }
            if (iovalue != lastiovalue)
            {
                lastiovalue = iovalue;
                IOManager.WriteSingleDO("", 0x00, ledio, iovalue);
            }
        }
    }
    public enum LedState
    {
        none,
        off,
        on,
        blink
    }
    public enum LedColor
    {
        red,
        yellow,
        green
    }
    public enum MachineLedStateE
    {
        Stop,
        AlarmStop,
        Alarm,
        HomeReset,
        Running,
        SystemPause,
        THoutRangeOver30m,
        THoutRange,
        InOut,
        SafetyLightCurtains,
        Debug
    }
}