MainMachine _LedProcess.cs 8.3 KB
using OnlineStore;
using CodeLibrary;
using Newtonsoft.Json;
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.info("灯塔亮灯配置加载失败:" + 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.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.SystemPause] = crc.GetString("ledstate_SystemPause","暂停");
            MachineLedState.Add(MachineLedStateE.SystemPause, nls(LedState.blink, LedState.off, LedState.blink));
            //温湿度超限30分钟. 红闪,黄闪
            //MachineLedStateName[MachineLedStateE.THoutRangeOver30m] = "温湿度超限30分钟";
            //MachineLedState.Add(MachineLedStateE.THoutRangeOver30m, nls(LedState.blink, LedState.blink, LedState.none));
            //温湿度超限 绿闪黄闪
           // MachineLedStateName[MachineLedStateE.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>();
            a.Add(LedColor.red, AlarmLedstate);
            a.Add(LedColor.yellow, StandbyLedstate);
            a.Add(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)
                    return;
                Led.LedColors[ledcolor].LedState = ledcfg[ledcolor];
            }
        }
        void LedProcess(object o)
        {
            StandbyLed.LedState = LedState.off;
            AlarmLed.LedState = LedState.off;
            RunningLed.LedState = LedState.off;
            //回原 绿闪
            if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.HomeReset ? 1 : 0) > 0)
            {
                ProcessLefCfg(MachineLedStateE.HomeReset);
            }
            //正常 绿亮
            else if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.Running ? 1 : 0) > 0)
            {
                ProcessLefCfg(MachineLedStateE.Running);

                //出入库 绿闪 黄闪
                if (RobotManage.Stores.Sum(x=>x.isBusy?1:0)>0){
                    ProcessLefCfg(MachineLedStateE.InOut);
                }
                //系统暂停,说明书未定义, 绿闪, 红闪
                if (!canRunning || UserPause)
                {
                    ProcessLefCfg(MachineLedStateE.SystemPause);
                }
            }
            else if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.Stop ? 1 : 0) > 0)
            {
                //系统停止时有报警, 红亮
                if (RobotManage.Stores.Sum(x => x.isAlarm ? 1 : 0) > 0)
                   // if (hasAlarm)
                {
                    ProcessLefCfg(MachineLedStateE.AlarmStop);
                }
            }
            //系统运行时报警, 绿亮,红闪
            if (runStatus != RunStatus.Stop && isAlarm)
            {
                ProcessLefCfg(MachineLedStateE.Alarm);
            }
            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
    {
        AlarmStop,
        Alarm,
        HomeReset,
        Running,
        SystemPause,
        THoutRangeOver30m,
        THoutRange,
        InOut,
    }
}