using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sln.Iot.PLC;
using HslCommunication;
using System.Collections;
using Sln.Iot.Business.Entity;
using Sln.Iot.Repository.service;
using Sln.Iot.Repository.dao;
using Sln.Iot.CFX.CFXConnect;
using CFX.ResourcePerformance;
using Sln.Iot.CFX.Event;
using CFX;
using CFX.Structures;
using Sln.Iot.Serilog;
using Amqp;
using TouchSocket.Core;

namespace Sln.Iot.Business
{
    public class ErrorAnalyseBusiness
    {
        private Timer _timer;

        private readonly PLCConnect _plc = PLCConnect.Instance;
        private readonly SerilogHelper _log = SerilogHelper.Instance;

        private readonly ErrorService errorService = ErrorService.Instance;
        private readonly TrayBindingService trayBindingService = TrayBindingService.Instance;

        private FaultOccurredEvent faultOccurredEvent = new FaultOccurredEvent();
        private FaultClearedEvent faultClearedEvent = new FaultClearedEvent();

        private CFXConnect1 connect1 = CFXConnect1.Instance;
        private CFXConnect2 connect2 = CFXConnect2.Instance;
        private CFXConnect3 connect3 = CFXConnect3.Instance;

        public ErrorAnalyseBusiness()
        {
            _timer = new Timer(TimerCallback, null, 0, 2000);
        }

        /// <summary>
        /// 报警点位刷新
        /// </summary>
        /// <param name="state"></param>
        public void TimerCallback(object? state)
        {
            try
            {
                byte[] errorBytes1 = new byte[1];

                //隧道烤箱报警
                var result20 = _plc.ReadBytes(_plc.DeltaInstance0, "D100", 1);
                //真空箱1报警
                var result21 = _plc.ReadBytes(_plc.DeltaInstance1, "D80", 11);
                //真空箱2报警
                var result22 = _plc.ReadBytes(_plc.DeltaInstance2, "D80", 11);
                //提升机报警
                var result23 = _plc.ReadBytes(_plc.DeltaInstance3, "D100", 1);

                if (result20.IsSuccess && result20.Content != null)
                {
                    BitArray bits20 = new BitArray(ToolBusiness.SwapAdjacentBytes(result20.Content));
                    bool[] bools20 = new bool[bits20.Count];
                    bits20.CopyTo(bools20, 0);
                    OvenAlarmAnalyse(bools20);
                }

                if (result21.IsSuccess && result21.Content != null)
                {
                    BitArray bits21 = new BitArray(ToolBusiness.SwapAdjacentBytes(result21.Content));
                    bool[] bools21 = new bool[bits21.Count];
                    bits21.CopyTo(bools21, 0);
                    VacuumAlarm1Analyse(bools21);
                }

                if (result22.IsSuccess && result22.Content != null)
                {
                    BitArray bits22 = new BitArray(ToolBusiness.SwapAdjacentBytes(result22.Content));
                    bool[] bools22 = new bool[bits22.Count];
                    bits22.CopyTo(bools22, 0);
                    VacuumAlarm2Analyse(bools22);
                }

                if (result23.IsSuccess && result23.Content != null)
                {
                    BitArray bits23 = new BitArray(ToolBusiness.SwapAdjacentBytes(result23.Content));
                    bool[] bools23 = new bool[bits23.Count];
                    bits23.CopyTo(bools23, 0);
                    LiftAlarmAnalyse(bools23);
                }
            }
            catch (Exception ex)
            {
                _log.Error("报警信息处理发生错误", ex);
            }
        }


        /// <summary>
        /// 真空箱1报警分析
        /// </summary>
        /// <param name="bools"></param>
        public void VacuumAlarm1Analyse(bool[] bools)
        {
            //现存的错误数据
            List<Vacuum1Error>? errorList = errorService.GetV1List();

            foreach (var errorMessage in VacuumAlarms)
            {
                if (bools[errorMessage.ErrorIndex])
                {
                    //如果原来没有 现在有 添加错误
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() == 0)
                    {
                        Guid.TryParse(trayBindingService.TidGet(), out Guid transID);
                        Guid logGuid = Guid.NewGuid();
                        errorService.InsertVacuum1Error(logGuid.ToString(), errorMessage.ENAlarmMessage, errorMessage.CNAlarmMessage, errorMessage.ErrorCode);
                        connect1.PublishEvent(new CFXEnvelope(faultOccurredEvent.Handle(errorMessage.ErrorCode, LevelTrans(errorMessage.AlarmDegarde), logGuid, errorMessage.ENAlarmMessage + "-Vacuum1", transID)));
                        _log.Info($"真空箱1报警发生 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
                else
                {
                    //如果原来有 现在没有 则删除
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() != 0)
                    {
                        //删除错误
                        string delGuid = errorService.DeleteVacuum2Error(errorMessage.ENAlarmMessage);
                        Guid.TryParse(delGuid, out Guid guidRes);
                        connect1.PublishEvent(new CFXEnvelope(faultClearedEvent.Handle(guidRes)));
                        _log.Info($"真空箱1报警消除 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
            }
        }

        /// <summary>
        /// 真空箱2报警分析
        /// </summary>
        /// <param name="bools"></param>
        public void VacuumAlarm2Analyse(bool[] bools)
        {
            //现存的错误数据
            List<Vacuum2Error>? errorList = errorService.GetV2List();

            foreach (var errorMessage in VacuumAlarms)
            {
                if (bools[errorMessage.ErrorIndex])
                {
                    //如果原来没有 现在有 添加错误
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() == 0)
                    {
                        Guid.TryParse(trayBindingService.TidGet(), out Guid transID);
                        Guid logGuid = Guid.NewGuid();
                        errorService.InsertVacuum2Error(logGuid.ToString(), errorMessage.ENAlarmMessage, errorMessage.CNAlarmMessage, errorMessage.ErrorCode);
                        connect1.PublishEvent(new CFXEnvelope(faultOccurredEvent.Handle(errorMessage.ErrorCode, LevelTrans(errorMessage.AlarmDegarde), logGuid, errorMessage.ENAlarmMessage + "-Vacuum2", transID)));
                        _log.Info($"真空箱2报警发生 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
                else
                {
                    //如果原来有 现在没有 则删除
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() != 0)
                    {
                        //删除错误
                        string delGuid = errorService.DeleteVacuum2Error(errorMessage.ENAlarmMessage);
                        Guid.TryParse(delGuid, out Guid guidRes);
                        connect1.PublishEvent(new CFXEnvelope(faultClearedEvent.Handle(guidRes)));
                        _log.Info($"真空箱2报警消除 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
            }
        }

        /// <summary>
        /// 提升机报警分析
        /// </summary>
        /// <param name="bools"></param>
        public void LiftAlarmAnalyse(bool[] bools)
        {
            //现存的错误数据
            List<LiftError>? errorList = errorService.GetLiftList();

            foreach (var errorMessage in LiftAlarms)
            {
                if (bools[errorMessage.ErrorIndex])
                {
                    //如果原来没有 现在有 添加错误
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() == 0)
                    {
                        Guid.TryParse(trayBindingService.TidGet(), out Guid transID);
                        Guid logGuid = Guid.NewGuid();
                        errorService.InsertLiftError(logGuid.ToString(), errorMessage.ENAlarmMessage, errorMessage.CNAlarmMessage, errorMessage.ErrorCode);
                        connect2.PublishEvent(new CFXEnvelope(faultOccurredEvent.Handle(errorMessage.ErrorCode, LevelTrans(errorMessage.AlarmDegarde), logGuid, errorMessage.ENAlarmMessage, transID)));
                        _log.Info($"提升机报警发生 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
                else
                {
                    //如果原来有 现在没有 则删除
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() != 0)
                    {
                        //删除错误
                        string delGuid = errorService.DeleteliftError(errorMessage.ENAlarmMessage);
                        Guid.TryParse(delGuid, out Guid guidRes);
                        connect2.PublishEvent(new CFXEnvelope(faultClearedEvent.Handle(guidRes)));
                        _log.Info($"提升机报警消除 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
            }
        }

        /// <summary>
        /// 烤箱报警分析
        /// </summary>
        /// <param name="bools"></param>
        public void OvenAlarmAnalyse(bool[] bools)
        {
            //现存的错误数据
            List<OvenError>? errorList = errorService.GetOvenList();

            foreach (var errorMessage in OvenAlarms)
            {
                if (bools[errorMessage.ErrorIndex])
                {
                    //如果原来没有 现在有 添加错误
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() == 0)
                    {
                        Guid.TryParse(trayBindingService.TidGet(), out Guid transID);
                        Guid logGuid = Guid.NewGuid();
                        errorService.InsertOvenError(logGuid.ToString(), errorMessage.ENAlarmMessage, errorMessage.CNAlarmMessage, errorMessage.ErrorCode);
                        connect3.PublishEvent(new CFXEnvelope(faultOccurredEvent.Handle(errorMessage.ErrorCode, LevelTrans(errorMessage.AlarmDegarde), logGuid, errorMessage.ENAlarmMessage, transID)));
                        _log.Info($"烤箱报警发生 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
                else
                {
                    //如果原来有 现在没有 则删除
                    if (errorList.Where(x => x.ErrorName == errorMessage.ENAlarmMessage).Count() != 0)
                    {
                        //删除错误
                        string delGuid = errorService.DeleteOvenError(errorMessage.ENAlarmMessage);
                        Guid.TryParse(delGuid, out Guid guidRes);
                        connect3.PublishEvent(new CFXEnvelope(faultClearedEvent.Handle(guidRes)));
                        _log.Info($"烤箱报警消除 内容:{errorMessage.CNAlarmMessage} 级别:{errorMessage.AlarmDegarde.GetDescription()}");
                    }
                }
            }
        }



        /// <summary>
        /// 报警等级转换
        /// </summary>
        /// <returns></returns>
        private FaultSeverity LevelTrans(AlarmDegardeEnum alarmDegardeEnum)
        {
            switch (alarmDegardeEnum)
            {
                case AlarmDegardeEnum.Warning:
                    return FaultSeverity.Information;
                case AlarmDegardeEnum.Error:
                    return FaultSeverity.Warning;
                case AlarmDegardeEnum.Fault:
                    return FaultSeverity.Error;
                default:
                    return FaultSeverity.Information;
            }
        }


        /// <summary>
        /// 真空箱报警定义
        /// </summary>
        public static readonly List<AlarmMessageEntity> VacuumAlarms = new()
        {
            // ===== D80 =====
            new() { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "1# Pump A Backward Limit Reached!", CNAlarmMessage = "泵A负限超程！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "1# Pump A Forward Limit Reached!", CNAlarmMessage = "泵A正限超程！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "1# Pump B Backward Limit Reached!", CNAlarmMessage = "泵B负限超程！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "1# Pump B Forward Limit Reached!", CNAlarmMessage = "泵B正限超程！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "1# Servo A Error!", CNAlarmMessage = "泵A伺服报警！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "1# Servo B Error!", CNAlarmMessage = "泵B伺服报警！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 6, ENAlarmMessage = "Mixed Servo Error!", CNAlarmMessage = "混合伺服报警！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 7, ENAlarmMessage = "Mixed Motor Rotating Error!", CNAlarmMessage = "混合电机旋转异常！", AlarmDegarde = AlarmDegardeEnum.Fault },

            new() { ErrorIndex = 16 * 0 + 12, ENAlarmMessage = "A1 Heating Error", CNAlarmMessage = "A1加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 13, ENAlarmMessage = "B1 Heating Error", CNAlarmMessage = "B1加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 14, ENAlarmMessage = "A2 Heating Error", CNAlarmMessage = "A2加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 15, ENAlarmMessage = "B2 Heating Error", CNAlarmMessage = "B2加热异常", AlarmDegarde = AlarmDegardeEnum.Error },

            // ===== D81 =====
            new() { ErrorIndex = 16 * 1 + 0, ENAlarmMessage = "A Potting Valve Open Error", CNAlarmMessage = "A吐出阀打开异常", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 1 + 1, ENAlarmMessage = "B Potting Valve Open Error", CNAlarmMessage = "B吐出阀打开异常", AlarmDegarde = AlarmDegardeEnum.Fault },

            // ===== D82 =====
            new() { ErrorIndex = 16 * 2 + 0, ENAlarmMessage = "1# Tank A Globe Valve Error!", CNAlarmMessage = "A1桶球阀打开异常！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 2 + 1, ENAlarmMessage = "1# Tank B Globe Valve Error!", CNAlarmMessage = "B1桶球阀打开异常！", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 2 + 2, ENAlarmMessage = "Vacuum Chamber Inlet Door Open Error", CNAlarmMessage = "真空箱入口门打开异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 3, ENAlarmMessage = "Vacuum Chamber Inlet Door Close Error", CNAlarmMessage = "真空箱入口门关闭异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 4, ENAlarmMessage = "Vacuum Chamber Outlet Door Open Error", CNAlarmMessage = "真空箱出口门打开异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 5, ENAlarmMessage = "Vacuum Chamber Outlet Door Close Error", CNAlarmMessage = "真空箱出口门关闭异常", AlarmDegarde = AlarmDegardeEnum.Error },

            new() { ErrorIndex = 16 * 2 + 8, ENAlarmMessage = "Axis X Servo Error", CNAlarmMessage = "X轴伺服报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 2 + 9, ENAlarmMessage = "Axis Y Servo Error", CNAlarmMessage = "Y轴伺服报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 2 + 10, ENAlarmMessage = "Axis Z Servo Error", CNAlarmMessage = "Z轴伺服报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 2 + 11, ENAlarmMessage = "Vacuum Tank Vacuum Pump Error", CNAlarmMessage = "真空箱真空泵异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 12, ENAlarmMessage = "Vacuum Chamber Lifter Error", CNAlarmMessage = "灌胶定位缸异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 13, ENAlarmMessage = "Vacuum Chamber Vacuum Valve Error", CNAlarmMessage = "真空阀打开异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 2 + 14, ENAlarmMessage = "Vacuum Vent Valve Close Error", CNAlarmMessage = "放气阀关闭异常", AlarmDegarde = AlarmDegardeEnum.Error },

            // ===== D83 =====
            new() { ErrorIndex = 16 * 3 + 0, ENAlarmMessage = "Air Pressure Alarm", CNAlarmMessage = "系统气压报警", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 3 + 2, ENAlarmMessage = "Glue Tank Vacuum Pump Error!", CNAlarmMessage = "料桶真空泵报警！", AlarmDegarde = AlarmDegardeEnum.Error },

            // ===== D84 =====
            new() { ErrorIndex = 16 * 4 + 0, ENAlarmMessage = "Pump A Pressure Over Upper Limit", CNAlarmMessage = "A泵压力上限报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 4 + 1, ENAlarmMessage = "Pump A Pressure Below Lower Limit", CNAlarmMessage = "A泵压力下限报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 4 + 2, ENAlarmMessage = "Pump B Pressure Over Upper Limit", CNAlarmMessage = "B泵压力上限报警", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 4 + 3, ENAlarmMessage = "Pump B Pressure Below Lower Limit", CNAlarmMessage = "B泵压力下限报警", AlarmDegarde = AlarmDegardeEnum.Fault },

            // ===== D90 =====
            new() { ErrorIndex = 16 * 10 + 0, ENAlarmMessage = "1# Tank A Upper Limit!", CNAlarmMessage = "A1桶上限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 1, ENAlarmMessage = "1# Tank A Lower Limit!", CNAlarmMessage = "A1桶下限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 2, ENAlarmMessage = "1# Tank B Upper Limit!", CNAlarmMessage = "B1桶上限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 3, ENAlarmMessage = "1# Tank B Lower Limit!", CNAlarmMessage = "B1桶下限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 4, ENAlarmMessage = "2# Tank A Upper Limit!", CNAlarmMessage = "A2桶上限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 5, ENAlarmMessage = "2# Tank A Lower Limit!", CNAlarmMessage = "A2桶下限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 6, ENAlarmMessage = "2# Tank B Upper Limit!", CNAlarmMessage = "B2桶上限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 7, ENAlarmMessage = "2# Tank B Lower Limit!", CNAlarmMessage = "B2桶下限报警！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 8, ENAlarmMessage = "1# Antisolid Time Up!", CNAlarmMessage = "防固时间到！", AlarmDegarde = AlarmDegardeEnum.Warning },
            new() { ErrorIndex = 16 * 10 + 9, ENAlarmMessage = "Globe Valve Not Opened!", CNAlarmMessage = "球阀未打开！", AlarmDegarde = AlarmDegardeEnum.Error },
        };

        /// <summary>
        /// 加热线报警定义
        /// </summary>
        public static readonly List<AlarmMessageEntity> OvenAlarms = new()
        {
            new() { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "Line E.Stop ON!", CNAlarmMessage = "线急停按下", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "Preheating Zone Heating Error!", CNAlarmMessage = "预热区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "Precuring Zone I Heating Error!", CNAlarmMessage = "预固1区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "Precuring Zone II Heating Error!", CNAlarmMessage = "预固2区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "Precuring Zone III Heating Error!", CNAlarmMessage = "预固3区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "Precuring Zone IV Heating Error!", CNAlarmMessage = "预固4区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 6, ENAlarmMessage = "Curing Zone I Heating Error!", CNAlarmMessage = "固化1区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 7, ENAlarmMessage = "Curing Zone II Heating Error!", CNAlarmMessage = "固化2区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 8, ENAlarmMessage = "Curing Zone III Heating Error!", CNAlarmMessage = "固化3区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
            new() { ErrorIndex = 16 * 0 + 9, ENAlarmMessage = "Curing Zone IV Heating Error!", CNAlarmMessage = "固化4区加热异常", AlarmDegarde = AlarmDegardeEnum.Error },
        };

        /// <summary>
        /// 提升机报警定义
        /// </summary>
        public static readonly List<AlarmMessageEntity> LiftAlarms = new()
        {
            new() { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "Loading Lifter Down Error", CNAlarmMessage = "上料提升机下降异常", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "Loading Lifter Up Error", CNAlarmMessage = "上料提升机上升异常", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "Loading Lifter E.Stop Pressed", CNAlarmMessage = "上料提升机急停按下", AlarmDegarde = AlarmDegardeEnum.Fault },

            new() { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "Unloading Lifter Down Error", CNAlarmMessage = "下料提升机下降异常", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "Unloading Lifter Up Error", CNAlarmMessage = "下料提升机上升异常", AlarmDegarde = AlarmDegardeEnum.Fault },
            new() { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "Unloading Lifter E.Stop Pressed", CNAlarmMessage = "下料提升机急停按下", AlarmDegarde = AlarmDegardeEnum.Fault },
        };



        ///// <summary>
        ///// 真空注胶机1解析
        ///// </summary>
        ///// <param name="boolIndex"></param>
        ///// <returns></returns>
        //public static AlarmMessageEntity? GetVacuum1AlarmMessage(int boolIndex)
        //{
        //    switch (boolIndex)
        //    {
        //        // ===== D80 =====
        //        case 16 * 0 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "1# Pump A Backward Limit Reached!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "1# Pump A Forward Limit Reached!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "1# Pump B Backward Limit Reached!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "1# Pump B Forward Limit Reached!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 4: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "1# Servo A Error!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 5: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "1# Servo B Error!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 6: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 6, ENAlarmMessage = "Mixed Servo Error!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 7: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 7, ENAlarmMessage = "Mixed Motor Rotating Error!", AlarmDegarde = AlarmDegardeEnum.Fault };

        //        case 16 * 0 + 12: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 12, ENAlarmMessage = "A1 Heating Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 13: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 13, ENAlarmMessage = "B1 Heating Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 14: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 14, ENAlarmMessage = "A2 Heating Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 15: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 15, ENAlarmMessage = "B2 Heating Error", AlarmDegarde = AlarmDegardeEnum.Error };

        //        // ===== D81 =====
        //        case 16 * 1 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 1 + 0, ENAlarmMessage = "A Potting Valve Open Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 1 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 1 + 1, ENAlarmMessage = "B Potting Valve Open Error", AlarmDegarde = AlarmDegardeEnum.Fault };

        //        // ===== D82 =====
        //        case 16 * 2 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 0, ENAlarmMessage = "1# Tank A Globe Valve Error!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 2 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 1, ENAlarmMessage = "1# Tank B Globe Valve Error!", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 2 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 2, ENAlarmMessage = "1# Vacuum Chamber Inlet Door Open Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 3, ENAlarmMessage = "1# Vacuum Chamber Inlet Door Close Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 4: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 4, ENAlarmMessage = "1# Vacuum Chamber Outlet Door Open Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 5: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 5, ENAlarmMessage = "1# Vacuum Chamber Outlet Door Close Error", AlarmDegarde = AlarmDegardeEnum.Error };

        //        case 16 * 2 + 8: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 8, ENAlarmMessage = "Axis X Servo Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 2 + 9: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 9, ENAlarmMessage = "Axis Y Servo Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 2 + 10: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 10, ENAlarmMessage = "Axis Z Servo Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 2 + 11: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 11, ENAlarmMessage = "Vacuum Tank Vacuum Pump Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 12: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 12, ENAlarmMessage = "Vacuum Chamber Lifter Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 13: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 13, ENAlarmMessage = "Vacuum Chamber Vacuum Valve Error", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 2 + 14: return new AlarmMessageEntity { ErrorIndex = 16 * 2 + 14, ENAlarmMessage = "Vacuum Vent Valve Close Error", AlarmDegarde = AlarmDegardeEnum.Error };

        //        // ===== D83 =====
        //        case 16 * 3 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 3 + 0, ENAlarmMessage = "Air Pressure Alarm", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 3 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 3 + 2, ENAlarmMessage = "Glue Tank Vacuum Pump Error!", AlarmDegarde = AlarmDegardeEnum.Error };

        //        // ===== D84 =====
        //        case 16 * 4 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 4 + 0, ENAlarmMessage = "Pump A Pressure Over Upper Limit", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 4 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 4 + 1, ENAlarmMessage = "Pump A Pressure Below Lower Limit", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 4 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 4 + 2, ENAlarmMessage = "Pump B Pressure Over Upper Limit", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 4 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 4 + 3, ENAlarmMessage = "Pump B Pressure Below Lower Limit", AlarmDegarde = AlarmDegardeEnum.Fault };

        //        // ===== D90 =====
        //        case 16 * 10 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 0, ENAlarmMessage = "1# Tank A Upper Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 1, ENAlarmMessage = "1# Tank A Lower Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 2, ENAlarmMessage = "1# Tank B Upper Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 3, ENAlarmMessage = "1# Tank B Lower Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 4: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 4, ENAlarmMessage = "2# Tank A Upper Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 5: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 5, ENAlarmMessage = "2# Tank A Lower Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 6: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 6, ENAlarmMessage = "2# Tank B Upper Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 7: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 7, ENAlarmMessage = "2# Tank B Lower Limit!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 8: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 8, ENAlarmMessage = "1# Antisolid Time Up!", AlarmDegarde = AlarmDegardeEnum.Warning };
        //        case 16 * 10 + 9: return new AlarmMessageEntity { ErrorIndex = 16 * 10 + 9, ENAlarmMessage = "Globe Valve Not Opened!", AlarmDegarde = AlarmDegardeEnum.Error };

        //        default:
        //            return null;
        //    }
        //}

        ///// <summary>
        ///// 加热线警报
        ///// </summary>
        ///// <param name="boolIndex"></param>
        ///// <returns></returns>
        //public static AlarmMessageEntity? GetHeatingLineAlarm(int boolIndex)
        //{
        //    switch (boolIndex)
        //    {
        //        // ===== D100 =====
        //        case 16 * 0 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "Line E.Stop ON!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "Preheating Zone Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "Precuring Zone I Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "Precuring Zone II Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 4: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "Precuring Zone III Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 5: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "Precuring Zone IV Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 6: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 6, ENAlarmMessage = "Curing Zone I Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 7: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 7, ENAlarmMessage = "Curing Zone II Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 8: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 8, ENAlarmMessage = "Curing Zone III Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 9: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 9, ENAlarmMessage = "Curing Zone IV Heating Error!", AlarmDegarde = AlarmDegardeEnum.Error };

        //        default:
        //            return null;
        //    }
        //}

        ///// <summary>
        ///// 提升机警报
        ///// </summary>
        ///// <param name="boolIndex"></param>
        ///// <returns></returns>
        //public static AlarmMessageEntity? GetLifterAlarm(int boolIndex)
        //{
        //    switch (boolIndex)
        //    {
        //        // ===== D100 =====
        //        case 16 * 0 + 0: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 0, ENAlarmMessage = "Loading Lifter Down Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 1: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 1, ENAlarmMessage = "Loading Lifter Up Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 2: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 2, ENAlarmMessage = "Loading Lifter E.Stop Pressed", AlarmDegarde = AlarmDegardeEnum.Error };
        //        case 16 * 0 + 3: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 3, ENAlarmMessage = "Unloading Lifter Down Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 4: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 4, ENAlarmMessage = "Unloading Lifter Up Error", AlarmDegarde = AlarmDegardeEnum.Fault };
        //        case 16 * 0 + 5: return new AlarmMessageEntity { ErrorIndex = 16 * 0 + 5, ENAlarmMessage = "Unloading Lifter E.Stop Pressed", AlarmDegarde = AlarmDegardeEnum.Error };

        //        default:
        //            return null;
        //    }
        //}


    }
}
