using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.Util;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.CommonData.Prewet;
using MECF.Framework.Common.Device.Safety;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.TwinCat;
using MECF.Framework.Common.Utilities;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CyberX8_RT.Devices.AXIS;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.WaferHolder;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Devices.Safety
{
    public class SafetyDevice : BaseDevice, IDevice
    {
        #region 常量
        private const string TWINCAT_STATE = "TwincatState";
        private const string MAIN_COMM_ERR = "MainCommErr";
        private const string LOADER_PUF_COMM_ERR="LoaderPufCommErr";
        private const string SRD_COMM_ERR = "SrdCommErr";
        private const string TRANSPORTER_COMM_ERR="TransportCommErr";
        private const string MAIN_FUNCTION_BLOCK_ERR= "MainFunctionBlockErr";
        private const string LOADER_PUF_FUNCTION_BLOCK_ERR="LoaderPufFunctionBlockErr";
        private const string SRD_FUNCTION_BLOCK_ERR = "SrdFunctionBlockErr";
        private const string TRANSPORTER_FUNCTION_BLOCK_ERR="TransporterFunctionBlockErr";
        private const string IS_INITIALIZED = "IsInitialized";
        private const double OP_HOT_CONNECT_STATE = 8;
        #region Hot Connect
        private const string HOT_CONNECT_STATE = "HotConnectState";
        private const string LOADER_HOT_CONNECT_STATE= "LoaderHotConnectState";
        private const string PUF_HOT_CONNECT_STATE="PufHotConnectState";
        private const string SRD_HOT_CONNECT_STATE="SrdHotConnectState";
        private const string TRANSPORTER_HOT_CONNECT_STATE="TransporterHotConnectState";
        private const string LDR_HOT_CONNECT_STATE="LdrHotConnectState";
        private const string PRC_HOT_CONNECT_STATE="PrcHotConnectState";
        private const string RESERVOIR1_HOT_CONNECT_STATE="Reservoir1HotConnectState";
        private const string RESERVOIR2_HOT_CONNECT_STATE="Reservoir2HotConnectState";
        private const string FFU_HOT_CONNECT_STATE="FFUHotConnectState";
        #endregion
        #region Restart
        private const string RUNSTOP = "RunStop";
        private const string ERRACK = "ErrAck";
        private const string SLS_RESTART = "SlsRestart";
        private const string TX_STO_MON_Restart = "TxStoMonRestart";
        private const string TX_STO1_ESTOP_Restart = "TxSto1EstopRestart";
        private const string TX_STO2_ESTOP_RESTART = "TxSto2EstopRestart";
        private const string LDR_PUF_STO_MON_RESTART = "LdrPufStoMonRestart";
        private const string LDR_PUF_STO1_ESTOP_RESTART = "LdrPufSto1EstopRestart";
        private const string LDR_PUF_STO2_ESTOP_RESTART = "LdrPufSto2EstopRestart";
        private const string SRD_STO_ESTOP_RESTART = "SrdStoEstopRestart";
        private const string PUMP_STO_ESTOP_RESTART = "PumpStoEstopRestart";
        private const string FLUID_ESTOP_RESTART = "FluidEstopRestart";
        #endregion
        #region Door Lock
        private const string LOADER_DOOR_LOCK= "LoaderDoorLock";
        private const string LOADER_DOOR_LOCKED="LoaderDoorLocked";
        private const string LOADER_PANEL_REMOVED="LoaderPanelRemoved";
        private const string LOWER_LOADER_PANEL_REMOVED = "LowerLoaderPanelRemoved";
        private const string BUFFER_DOOR_LOCK="BufferDoorLock";
        private const string BUFFER_DOOR_LOCKED="BufferDoorLocked";
        private const string BUFFER_PANEL_REMOVED = "BufferPanelRemoved";
        private const string PROCESS_DOOR_LOCK="ProcessDoorLock";
        private const string PROCESS_DOOR1_LOCKED="ProcessDoor1Locked";
        private const string PROCESS_DOOR2_LOCKED="ProcessDoor2Locked";
        private const string PROCESS_DOOR3_LOCKED="ProcessDoor3Locked";
        private const string PROCESS_DOOR4_LOCKED="ProcessDoor4Locked";
        private const string PROCESS_UPPER_PANELS_ON = "ProcessUpperPanelsOn";
        #endregion
        private const string SRD_TOP_PANEL_REMOVED="SrdTopPanelRemoved";
        private const string SRD_LOWER_PANEL_REMOVED="SrdLowerPanelRemoved";
        private const string PLUMBING_DOOR_CLOSED = "PlumbingDoorClosed";
        private const string PROCESS_HALT_BUTTON1="ProcessHaltButton1";
        private const string PROCESS_HALT_BUTTON2="ProcessHaltButton2";
        private const string LOADER_HALT_BUTTON= "LoaderHaltButton";
        private const string MODE_SELECTOR_SWITCH="ModeSelectorSwitch";
        private const string MODE_SIREN="ModeSiren";
        #region SLS
        private const string LOADER_ROTATION_SLS="LoaderRotationSls";
        private const string LOADER_SWINGA_SLS="LoaderSwingASls";
        private const string LOADER_SWINGB_SLS="LoaderSwingBSls";
        private const string LOADER_TILTA_SLS="LoaderTiltASls";
        private const string LOADER_TILTB_SLS="LoaderTiltBSls";
        private const string PUF1_VERTICAL_SLS="Puf1VerticalSls";
        private const string PUF2_VERTICAL_SLS="Puf2VerticalSls";
        private const string LOADER_GANTRY_SLS="LoaderGantrySls";
        private const string LOADER_ELEVATOR_SLS="LoaderElevatorSls";
        private const string PROCESS_GANTRY_SLS="ProcessGantrySls";
        private const string PROCESS_ELEVATOR_SLS="ProcessElevatorSls";
        #endregion
        #region Disable
        private const string PUF_VERTICAL_DISABLE= "PufVerticalDisable";
        private const string PUF1_FLIP_ROTATION_DISABLE="Puf1FlipRotationDisable";
        private const string PUF2_FLIP_ROTATION_DISABLE="Puf2FlipRotationDisable";
        private const string LOADER_DISABLE="LoaderDisable";
        private const string TRANSPORTER_DISABLE="TransporterDisable";
        private const string SRD_ARM_DISABLE="SrdArmDisable";
        private const string SRD_ROTATION_DISABLE="SrdRotationDisable";
        #endregion
        #region safety
        private const string TRANSPORTER_SAFE_SLS="TransporterSafeSls";
        private const string LOADER_SAFE_SLS="LoaderSafeSls";
        private const string PUF_VERTICAL_SAFE_SLS="PufVerticalSafeSls";
        private const string LOADER_SAFE_STO="LoaderSafeSto";
        private const string PUF_VERTICAL_SAFE_STO="PufVerticalSafeSto";
        private const string PUF1_FLIP_ROTATION_SAFE_STO="Puf1FlipRotationSafeSto";
        private const string PUF2_FLIP_ROTATION_SAFE_STO="Puf2FlipRotationSafeSto";
        private const string TRANSPORTER_SAFE_STO="TransporterSafeSto";
        private const string SRD_ROTATION_SAFE_STO = "SrdRotationSafeSto";
        private const string SRD_ARM_SAFE_STO="SrdArmSafeSto";
        #endregion
        #region Plumbling
        private const string PUMP_EDM="PumpEdm";
        private const string RESERVIOR_HIGH_LEVEL="ReservoirHighLevel";
        private const string FLUID_ENABLE_EDM="FluidEnableEdm";
        #endregion
        private const string SRD_ARM_RESET="SRDArmReset";
        private const string SAFETY_DATA="SafetyData";
        #endregion
        #region 内部变量
        /// 
        /// Reset Routine
        /// 
        private SafetyResetRoutine _resetRoutine;
        /// 
        /// All Switch On Routine
        /// 
        private SafetyNewAllOnRoutine _allOnRoutine;
        /// 
        /// AllOnRoutine状态
        /// 
        private RState _allOnStatus;
        /// 
        /// 操作当前状态
        /// 
        private RState _status;
        /// 
        /// 变量是否初始化字典
        /// 
        private Dictionary _variableInitializeDic = new Dictionary();
        /// 
        /// 定时器Job
        /// 
        PeriodicJob _periodicJob = null;
        #endregion
        #region 属性
        /// 
        /// 数据对象
        /// 
        public SafetyData SafetyData { get; set; }
        /// 
        /// 状态
        /// 
        public RState Status { get; set; }
        /// 
        /// 所有io变量是否初始化
        /// 
        public bool IOInitialized { get { return IOVariableDictionaryUtil.AllIoVariableInitialized(eEvent.ERR_SAFETY,Module,_variableInitializeDic); } }
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public SafetyDevice() : base("Safety", "Safety", "Safety", "Safety")
        {           
            SafetyData = new SafetyData();
        }
        /// 
        /// 定时器
        /// 
        /// 
        private bool OnTimer()
        {
            if (_status==RState.Running)
            {
                _status = _resetRoutine.Monitor();
                if(_status==RState.End)
                {
                    LOG.WriteLog(eEvent.INFO_SAFETY, Module, "Reset Complete");
                }
                else if(_status==RState.Failed)
                {
                    LOG.WriteLog(eEvent.ERR_SAFETY, Module, "Reset Error");
                }
            }
            //AllOnRoutine监控
            if (_allOnStatus == RState.Running)
            {
                _allOnStatus = _allOnRoutine.Monitor();
                if (_allOnStatus == RState.End)
                {
                    LOG.WriteLog(eEvent.INFO_SAFETY, Module, "All On Complete");
                }
                else if (_allOnStatus == RState.Failed)
                {
                    LOG.WriteLog(eEvent.ERR_SAFETY, Module, "All On Error");
                }
            }
            SLSAxisModeSiren();
            //WaferShuttleUsageMonitor
            WaferHolderManager.Instance.WaferShuttleUsageMonitor();
            return true;
        }
        /// 
        /// SLS模块下Axis Mode Siren
        /// 
        private void SLSAxisModeSiren()
        {
            int count = 0;
            //count += AxisModeSirenOn(SafetyData.Puf1VerticalSls, $"{ModuleName.PUF1}.Vertical");
            //count += AxisModeSirenOn(SafetyData.Puf2VerticalSls, $"{ModuleName.PUF2}.Vertical");
            //count += AxisModeSirenOn(SafetyData.LoaderTiltASls, $"{ModuleName.Loader1}.TiltA");
            //count += AxisModeSirenOn(SafetyData.LoaderTiltBSls, $"{ModuleName.Loader1}.TiltB");
            //count += AxisModeSirenOn(SafetyData.LoaderSwingASls, $"{ModuleName.Loader1}.SwingA");
            //count += AxisModeSirenOn(SafetyData.LoaderSwingBSls, $"{ModuleName.Loader1}.SwingB");
            //count += AxisModeSirenOn(SafetyData.LoaderRotationSls, $"{ModuleName.Loader1}.Rotation");
            //count += AxisModeSirenOn(SafetyData.LoaderGantrySls, $"{ModuleName.Transporter2}.Gantry");
            //count += AxisModeSirenOn(SafetyData.LoaderElevatorSls, $"{ModuleName.Transporter2}.Elevator");
            //count += AxisModeSirenOn(SafetyData.ProcessGantrySls, $"{ModuleName.Transporter1}.Gantry");
            //count += AxisModeSirenOn(SafetyData.ProcessElevatorSls, $"{ModuleName.Transporter1}.Elevator");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.PUF1}.Vertical");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.PUF2}.Vertical");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Loader1}.TiltA");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Loader1}.TiltB");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Loader1}.ShuttleA");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Loader1}.ShuttleB");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Loader1}.Rotation");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Transporter2}.Gantry");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Transporter2}.Elevator");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Transporter1}.Gantry");
            count += AxisModeSirenOn(!SafetyData.ModeSelectorSwitch, $"{ModuleName.Transporter1}.Elevator");
            if (count==0&&SafetyData.ModeSiren)
            {
                ModeSirenOff();
            }
        }
        /// 
        /// Axis运动Mode Siren On
        /// 
        /// 
        private int AxisModeSirenOn(bool sls,string axisName)
        {
            JetAxisBase axis = DEVICE.GetDevice(axisName);
            if (axis != null && axis.IsSwitchOn)
            {
                if (axis.IsRun&&sls)
                {
                    ModeSirenOn();
                    return 1;
                }
            }
            return 0;
        }
        /// 
        /// 初始化
        /// 
        /// 
        public bool Initialize()
        {
            InitializeRoutine();
            InitializeData();
            SubscribeValueAction();
            InitializeOperation();
            _periodicJob = new PeriodicJob(200, OnTimer, $"{Module}.OnTimer", true);
            return true;
        }
        /// 
        /// 初始化Routine
        /// 
        private void InitializeRoutine()
        {
            _resetRoutine = new SafetyResetRoutine(Module);
            _allOnRoutine = new SafetyNewAllOnRoutine(Module);
        }
        /// 
        /// 初始化DATA
        /// 
        private void InitializeData()
        {
            DATA.Subscribe($"{Module}.{SAFETY_DATA}",() => SafetyData, SubscriptionAttribute.FLAG.IgnoreSaveDB);
            DATA.Subscribe($"{Module}.{IS_INITIALIZED}", () => IOInitialized, SubscriptionAttribute.FLAG.IgnoreSaveDB);
            DATA.Subscribe($"{Module}.TwincatState", () => SafetyData.TwincatState, SubscriptionAttribute.FLAG.IgnoreSaveDB);
        }
        /// 
        /// 初始化Operation
        /// 
        private void InitializeOperation()
        {
            OP.Subscribe($"{Module}.LockProcessDoors", (cmd,args) => LockProcessDoor());
            OP.Subscribe($"{Module}.UnlockProcessDoors", (cmd, args) => UnlockProcessDoor());
            OP.Subscribe($"{Module}.LockLoaderDoors", (cmd, args) => LockLoaderDoor());
            OP.Subscribe($"{Module}.UnlockLoaderDoors", (cmd, args) => UnlockLoaderDoor());
            OP.Subscribe($"{Module}.LockBufferDoors", (cmd, args) => LockBufferDoor());
            OP.Subscribe($"{Module}.UnlockBufferDoors", (cmd, args) => UnlockBufferDoor());
            OP.Subscribe($"{Module}.ResetOperation",(cmd, args) => ResetOperation());
            OP.Subscribe($"{Module}.AllOnOperation", (cmd, args) => AllOnOperation());
        }
        /// 
        /// 订阅变量数值发生变化
        /// 
        private void SubscribeValueAction()
        {
            BeckhoffIoSubscribeUpdateVariable( MAIN_COMM_ERR);
            BeckhoffIoSubscribeUpdateVariable( LOADER_PUF_COMM_ERR);
            BeckhoffIoSubscribeUpdateVariable( SRD_COMM_ERR);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_COMM_ERR);
            BeckhoffIoSubscribeUpdateVariable( MAIN_FUNCTION_BLOCK_ERR);
            BeckhoffIoSubscribeUpdateVariable( LOADER_PUF_FUNCTION_BLOCK_ERR);
            BeckhoffIoSubscribeUpdateVariable( SRD_FUNCTION_BLOCK_ERR);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_FUNCTION_BLOCK_ERR);
            BeckhoffIoSubscribeUpdateVariable( TWINCAT_STATE);
            BeckhoffIoSubscribeUpdateVariable( LOADER_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( FFU_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( PRC_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( LDR_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( SRD_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( PUF_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( RESERVOIR2_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( RESERVOIR1_HOT_CONNECT_STATE);
            BeckhoffIoSubscribeUpdateVariable( RUNSTOP);
            BeckhoffIoSubscribeUpdateVariable( ERRACK);
            BeckhoffIoSubscribeUpdateVariable( SLS_RESTART);
            BeckhoffIoSubscribeUpdateVariable( TX_STO1_ESTOP_Restart);
            BeckhoffIoSubscribeUpdateVariable( TX_STO_MON_Restart);
            BeckhoffIoSubscribeUpdateVariable( TX_STO2_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( LDR_PUF_STO_MON_RESTART);
            BeckhoffIoSubscribeUpdateVariable( LDR_PUF_STO1_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( LDR_PUF_STO2_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( SRD_STO_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( PUMP_STO_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( FLUID_ESTOP_RESTART);
            BeckhoffIoSubscribeUpdateVariable( LOADER_DOOR_LOCK);
            BeckhoffIoSubscribeUpdateVariable( LOADER_DOOR_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( LOADER_PANEL_REMOVED);
            BeckhoffIoSubscribeUpdateVariable( LOWER_LOADER_PANEL_REMOVED);
            BeckhoffIoSubscribeUpdateVariable( BUFFER_DOOR_LOCK);
            BeckhoffIoSubscribeUpdateVariable( BUFFER_DOOR_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( BUFFER_PANEL_REMOVED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_DOOR_LOCK);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_DOOR1_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_DOOR2_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_DOOR3_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_DOOR4_LOCKED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_UPPER_PANELS_ON);
            BeckhoffIoSubscribeUpdateVariable( SRD_TOP_PANEL_REMOVED);
            BeckhoffIoSubscribeUpdateVariable( SRD_LOWER_PANEL_REMOVED);
            BeckhoffIoSubscribeUpdateVariable( PLUMBING_DOOR_CLOSED);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_HALT_BUTTON1);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_HALT_BUTTON2);
            BeckhoffIoSubscribeUpdateVariable( LOADER_HALT_BUTTON);
            BeckhoffIoSubscribeUpdateVariable( MODE_SELECTOR_SWITCH);
            BeckhoffIoSubscribeUpdateVariable( MODE_SIREN);
            BeckhoffIoSubscribeUpdateVariable( LOADER_ROTATION_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_SWINGA_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_SWINGB_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_TILTA_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_TILTB_SLS);
            BeckhoffIoSubscribeUpdateVariable( PUF1_VERTICAL_SLS);
            BeckhoffIoSubscribeUpdateVariable( PUF2_VERTICAL_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_GANTRY_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_ELEVATOR_SLS);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_GANTRY_SLS);
            BeckhoffIoSubscribeUpdateVariable( PROCESS_ELEVATOR_SLS);
            BeckhoffIoSubscribeUpdateVariable( PUF_VERTICAL_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( PUF1_FLIP_ROTATION_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( PUF2_FLIP_ROTATION_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( LOADER_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( SRD_ARM_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( SRD_ROTATION_DISABLE);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_SAFE_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_SAFE_SLS);
            BeckhoffIoSubscribeUpdateVariable( PUF_VERTICAL_SAFE_SLS);
            BeckhoffIoSubscribeUpdateVariable( LOADER_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( PUF_VERTICAL_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( PUF1_FLIP_ROTATION_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( PUF2_FLIP_ROTATION_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( TRANSPORTER_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( SRD_ARM_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( SRD_ROTATION_SAFE_STO);
            BeckhoffIoSubscribeUpdateVariable( PUMP_EDM);
            BeckhoffIoSubscribeUpdateVariable( RESERVIOR_HIGH_LEVEL);
            BeckhoffIoSubscribeUpdateVariable(FLUID_ENABLE_EDM);
            BeckhoffIoSubscribeUpdateVariable(SRD_ARM_RESET);
        }
        /// 
        /// 订阅IO变量
        /// 
        /// 
        private void BeckhoffIoSubscribeUpdateVariable(string variable)
        {
            _variableInitializeDic[variable] = false;
            IOModuleManager.Instance.SubscribeModuleVariable(Module,variable, UpdateVariableValue);
        }
        /// 
        /// 更新变量数值
        /// 
        /// 
        /// 
        private void UpdateVariableValue(string variable, object value)
        {
            if (!SafetyData.IsDataInitialized)
            {
                SafetyData.IsDataInitialized = true;
            }
            PropertyInfo property = SafetyData.GetType().GetProperty(variable);
            if (property != null)
            {
                property.SetValue(SafetyData, value);
            }
            if (_variableInitializeDic.ContainsKey(variable) && !_variableInitializeDic[variable])
            {
                _variableInitializeDic[variable] = true;
            }
            if(variable.EndsWith("Sls"))
            {
                SlsChangedSpeed(variable,(bool)value);
            }
            CheckIsDataAbonrmal(variable, value);
        }
        #region 监控指示灯是否异常 方法
        /// 
        /// 监控指示灯是否异常,是则打印log
        /// 
        /// 
        /// 
        private void CheckIsDataAbonrmal(string variable,object value)
        {
            if (variable == "ReservoirHighLevel" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "ReservoirHigh Level Error");
            }
            else if (variable == "MainCommErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "MainCommErr Error");
            }
            else if (variable == "LoaderPufCommErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "LoaderPufCommErr Error");
            }
            else if (variable == "SrdCommErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "SrdCommErr Error");
            }
            else if (variable == "TransportCommErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "TransportCommErr Error");
            }
            else if (variable == "MainFunctionBlockErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "MainFunctionBlockErr Error");
            }
            else if (variable == "LoaderPufFunctionBlockErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "LoaderPufFunctionBlockErr Error");
            }
            else if (variable == "SrdFunctionBlockErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "SrdFunctionBlockErr Error");
            }
            else if (variable == "TransporterFunctionBlockErr" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "TransporterFunctionBlockErr Error");
            }
            else if (variable == "ProcessHaltButton1" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "ProcessHaltButton1 Activate");
            }
            else if (variable == "ProcessHaltButton2" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "ProcessHaltButton2 Activate");
            }
            else if (variable == "LoaderHaltButton" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "LoaderHaltButton Activate");
            }
            else if (variable == "LoaderPanelRemoved" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "LoaderPanelRemoved Activate");
            }
            else if (variable == "BufferPanelRemoved" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "BufferPanelRemoved Activate");
            }
            else if (variable == "SrdTopPanelRemoved" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "SrdTopPanelRemoved Activate");
            }
            else if (variable == "SrdLowerPanelRemoved" && (bool)value == true)
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "SrdLowerPanelRemoved Activate");
            }
            else if (variable == "PlumbingDoorClosed" && (bool)value == false) //信号是反的
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "PlumbingDoorClosed Activate");
            }
            else if (variable == "ProcessUpperPanelsOn" && (bool)value == false) //信号是反的
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "ProcessUpperPanelsOn Activate");
            }
            else if (variable == "PumpEdm" && (bool)value == false) //信号是反的
            {
                LOG.WriteLog(eEvent.ERR_SAFETY, Module, "PumpEdm Activate");
            }
        }
        #endregion
        
        #region SLS
        /// 
        /// SLS更改速度
        /// 
        /// 
        /// 
        private void SlsChangedSpeed(string variable,bool sls)
        {
            int slsSpeed = SC.GetValue("Twincat.SlsSpeed");
            if(sls)
            {
                ModuleChangeSpeed(variable, slsSpeed);
            }
            else
            {
                ModuleChangeSpeed(variable, 100);
            }
        }
        /// 
        /// 模块更改速度
        /// 
        /// 
        /// 
        private void ModuleChangeSpeed(string variable,int percent)
        {
            switch(variable)
            {
                case PUF1_VERTICAL_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.PUF1}.Vertical"), percent);
                    break;
                case PUF2_VERTICAL_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.PUF2}.Vertical"), percent);
                    break;
                case LOADER_TILTA_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Loader1}.TiltA"), percent);
                    break;
                case LOADER_TILTB_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Loader1}.TiltB"), percent);
                    break;
                case LOADER_SWINGA_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Loader1}.ShuttleA"), percent);
                    break;
                case LOADER_SWINGB_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Loader1}.ShuttleB"), percent);
                    break;
                case LOADER_ROTATION_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Loader1}.Rotation"), percent);
                    break;
                case LOADER_GANTRY_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Transporter2}.Gantry"), percent);
                    break;
                case LOADER_ELEVATOR_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Transporter2}.Elevator"), percent);
                    break;
                case PROCESS_GANTRY_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Transporter1}.Gantry"), percent);
                    break;
                case PROCESS_ELEVATOR_SLS:
                    UpdateAxisSpeed(DEVICE.GetDevice($"{ModuleName.Transporter1}.Elevator"), percent);
                    break;
            }
        }
        /// 
        /// 更改Axis速度百分比
        /// 
        /// 
        /// 
        private void UpdateAxisSpeed(JetAxisBase jetAxisBase,int percent)
        {
            jetAxisBase.ChangePercentSpeedAceleration(percent);
        }
        #endregion
        #region Reset
        /// 
        /// Reset Operation
        /// 
        /// 
        private bool ResetOperation()
        {
            _status = _resetRoutine.Start();
            return _status == RState.Running;
        }
        #endregion
        #region All On
        /// 
        /// All On Operation
        /// 
        /// 
        private bool AllOnOperation()
        {
            _allOnStatus = _allOnRoutine.Start();
            return _allOnStatus == RState.Running;
        }
        #endregion
        #region Lock Door
        /// 
        /// Lock Process Door
        /// 
        /// 
        public bool LockProcessDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{PROCESS_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, true);
        }
        /// 
        /// Unlock Process Door
        /// 
        /// 
        public bool UnlockProcessDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{PROCESS_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, false);
        }
        /// 
        /// Lock Buffer Door
        /// 
        /// 
        public bool LockBufferDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{BUFFER_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, true);
        }
        /// 
        /// Unlock Buffer Door
        /// 
        /// 
        public bool UnlockBufferDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{BUFFER_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, false);
        }
        /// 
        /// Lock Loader Door
        /// 
        /// 
        public bool LockLoaderDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{LOADER_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, true);
        }
        /// 
        /// Unlock Loader Door
        /// 
        /// 
        public bool UnlockLoaderDoor()
        {
            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{LOADER_DOOR_LOCK}");
            return IOModuleManager.Instance.WriteIoValue(ioName, false);
        }
        #endregion
        #region Mode Siren 
        /// 
        /// Mode Siren On
        /// 
        /// 
        public bool ModeSirenOn()
        {
            if (!SafetyData.ModeSiren)
            {
                string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{MODE_SIREN}");
                return IOModuleManager.Instance.WriteIoValue(ioName, true);
            }
            return true;
        }
        /// 
        /// Mode Siren Off
        /// 
        /// 
        public bool ModeSirenOff()
        {
            if (SafetyData.ModeSiren)
            {
                string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{MODE_SIREN}");
                return IOModuleManager.Instance.WriteIoValue(ioName, false);
            }
            return true;
        }
        #endregion
        public void Monitor()
        {
        }
        public void Reset()
        {
        }
        public void Terminate()
        {
            _periodicJob.Stop(false);
        }
    }
}