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 PunkHPX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using PunkHPX8_RT.Devices.AXIS;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.IOCore;
namespace PunkHPX8_RT.Devices.Safety
{
public class SafetyDevice : BaseDevice, IDevice
{
#region 常量
private const string IS_INITIALIZED = "IsInitialized";
private const double OP_HOT_CONNECT_STATE = 8;
#region Hot_Connect
///
/// Twincat状态
///
private const string TWINCAT_STATE="TwincatState";
///
/// Main通信故障
///
private const string MAIN_COMM_ERR="MainCommErr";
///
/// Main Function Block故障
///
private const string MAIN_FB_ERR="MainFbErr";
///
/// VPW状态
///
private const string VPW_HOTCONNECT_STATE="VpwHotConectState";
///
/// VPW通信故障
///
private const string VPW_COMM_ERR="VpwCommErr";
///
/// VPW function block故障
///
private const string VPW_FB_ERR="VpwFbErr";
///
/// RES1_2状态
///
private const string RES1_2_HOT_CONNECT_STATE="Res12HotConectState";
///
/// RES1_2通信故障
///
private const string RES1_2_COMM_ERR="Res12CommErr";
///
/// RES1_2 Function Block故障
///
private const string RES1_2_FB_ERR="Res12FbErr";
///
/// RES3_4状态
///
private const string RES3_4_HOT_CONNECT_STATE="Res34HotConectState";
///
/// RES3_4通信故障
///
private const string RES3_4_COMM_ERR="Res34CommErr";
///
/// RES3_4Function Block故障
///
private const string RES3_4_FB_ERR="Res34FbErr";
///
/// Facility状态
///
private const string FACILITY_HOT_CONNECT_STATE="FacilityHotConectState";
///
/// Facility通信故障
///
private const string FACILITY_COMM_ERR="FacilityCommErr";
///
/// Facility Function Block故障
///
private const string FACILITY_FB_ERR="FacilityFbErr";
///
/// PlatingCell1_2状态
///
private const string PLATING1_2_HOT_CONNECT_STATE="Plating12HotConectState";
///
/// Plating1_2通信故障
///
private const string PLATING1_2_COMM_ERR="Plating12CommErr";
///
/// Plating1_2 Function Block故障
///
private const string PLATING1_2_FB_ERR="Plating12FbErr";
///
/// Plating3_4状态
///
private const string PLATING3_4_HOT_CONNECT_STATE="Plating34HotConectState";
///
/// Plating3_4通信故障
///
private const string PLATING3_4_COMM_ERR="Plating34CommErr";
///
/// Plating34 Function Block故障
///
private const string PLATING3_4_FB_ERR="Plating34FbErr";
///
/// SRD状态
///
private const string SRD_HOT_CONNECT_STATE="SrdHotConnectState";
///
/// SRD通信故障
///
private const string SRD_COMM_ERR="SrdCommErr";
///
/// SRD Function Block故障
///
private const string SRD_FB_ERR="SrdFbErr";
#endregion
#region restart
///
/// Run stop
///
private const string RUN_STOP="RunStop";
///
/// Err ack
///
private const string ERR_ACK="ErrAck";
///
/// VPW Estop restart
///
private const string VPW_STO_ESTOP_RESTART="VpwStoEstopRestart";
///
/// Res12 Estop restart
///
private const string RES1_2_STO_ESTOP_RESTART="Res12StoEstopRestart";
///
/// Res34 Estop restart
///
private const string RES3_4_STO_ESTOP_RESTART="Res34StoEstopRestart";
///
/// Plating cell 1_2 sto1 estop restart
///
private const string PLATING_CELL1_2_STO1_ESTOP_RESTART="PlatingCell12Sto1EstopRestart";
///
/// Plating cell 1_2 sto2 estop restart
///
private const string PLATING_CELL1_2_STO2_ESTOP_RESTART="PlatingCell12Sto2EstopRestart";
///
/// Plating cell 3_4 sto1 estop restart
///
private const string PLATING_CELL3_4_STO1_ESTOP_RESTART="PlatingCell34Sto1EstopRestart";
///
/// Plating cell 3_4 sto2 estop restart
///
private const string PLATING_CELL3_4_STO2_ESTOP_RESTART="PlatingCell34Sto2EstopRestart";
///
/// Facility sto estop restart
///
private const string FACILITY_STO_ESTOP_RESTART="FacilityStoEstopRestart";
///
/// SRD Sto Estop restart
///
private const string SRD_STO_ESTOP_RESTART="SrdStoEstopRestart";
#endregion
#region Doors Lock
///
/// Req Aligner Door Lock
///
private const string REQ_ALIGNER_DOOR_LOCK="ReqAlignerDoorLock";
///
/// Req cassette doors lock
///
private const string REQ_CASSETE_DOORS_LOCK="ReqCassetteDoorsLock";
///
/// Req cell 1_2 doors lock
///
private const string REQ_CELL1_2_DOORS_LOCK="ReqCell12DoorsLock";
///
/// Req cell doors lock
///
private const string REQ_CELL3_4_DOORS_LOCK="ReqCell34DoorsLock";
#endregion
#region locked
///
/// Cell1_2 Doors Left Locked
///
private const string CELL1_2_DOORS_LEFT_LOCKED="Cell12DoorsLeftLocked";
///
/// Cell1_2 Doors Right Locked
///
private const string CELL1_2_DOORS_RIGHT_LOCKED="Cell12DoorsRightLocked";
///
/// Cell3_4 Doors Left Locked
///
private const string CELL3_4_DOORS_LEFT_LOCKED="Cell34DoorsLeftLocked";
///
/// Cell3_4 Doors Right Locked
///
private const string CELL3_4_DOORS_RIGHT_LOCKED="Cell34DoorsRightLocked";
///
/// Cassette Doors Left locked
///
private const string CASSETE_DOORS_LEFT_LOCKED="CassetteDoorsLeftLocked";
///
/// Cassette Doors Right locked
///
private const string CASSETE_DOORS_RIGHT_LOCKED="CassetteDoorsRightLocked";
#endregion
#region Closed
///
/// Aligner Door closed
///
private const string ALIGNER_DOOR_CLOSED="AlignerDoorClosed";
private const string RES1_2_DOOR_CLOED="Res12DoorClosed";
private const string RES3_4_DOOR_CLOED="Res34DoorClosed";
private const string VPW_DOOR_CLOSED="VpwDoorClosed";
private const string PLUMBLING_DOOR_CLOSED="PlumblingDoorClosed";
private const string SRD_PANEL_CLOSED="SrdPanelClosed";
#endregion
#region Halt
private const string HALT_BUTTON_LEFT="HaltButtonLeft";
private const string HALT_BUTTON_RIGHT="HaltButtonRight";
#endregion
private const string SAFETY_DATA="SafetyData";
#endregion
#region 内部变量
///
/// Reset Routine
///
private SafetyResetRoutine _resetRoutine;
///
/// All Switch On Routine
///
private SafetyAllOnRoutine _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");
}
}
return true;
}
///
/// 初始化
///
///
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 SafetyAllOnRoutine(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}.LockAlignerDoor", (cmd,args) => LockAlignerDoor());
OP.Subscribe($"{Module}.UnlockAlignerDoor", (cmd, args) => UnlockAlignerDoor());
OP.Subscribe($"{Module}.LockCasseteDoor", (cmd, args) => LockCasseteDoor());
OP.Subscribe($"{Module}.UnlockCasseteDoor", (cmd, args) => UnlockCasseteDoor());
OP.Subscribe($"{Module}.LockCell12Door", (cmd, args) => LockCell12Door());
OP.Subscribe($"{Module}.UnlockCell12Door", (cmd, args) => UnlockCell12Door());
OP.Subscribe($"{Module}.LockCell34Door", (cmd, args) => LockCell34Door());
OP.Subscribe($"{Module}.UnlockCell34Door", (cmd, args) => UnlockCell34Door());
OP.Subscribe($"{Module}.ResetOperation",(cmd, args) => ResetOperation());
OP.Subscribe($"{Module}.AllOnOperation", (cmd, args) => AllOnOperation());
}
///
/// 订阅变量数值发生变化
///
private void SubscribeValueAction()
{
BeckhoffIoSubscribeUpdateVariable( MAIN_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( TWINCAT_STATE);
BeckhoffIoSubscribeUpdateVariable( MAIN_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( VPW_HOTCONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( VPW_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( VPW_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( FACILITY_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( FACILITY_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( FACILITY_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( RES1_2_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( RES1_2_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( RES1_2_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( RES3_4_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( RES3_4_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( RES3_4_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( PLATING1_2_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( PLATING1_2_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( PLATING1_2_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( PLATING3_4_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( PLATING3_4_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( PLATING3_4_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( SRD_HOT_CONNECT_STATE);
BeckhoffIoSubscribeUpdateVariable( SRD_COMM_ERR);
BeckhoffIoSubscribeUpdateVariable( SRD_FB_ERR);
BeckhoffIoSubscribeUpdateVariable( ERR_ACK);
BeckhoffIoSubscribeUpdateVariable( RUN_STOP);
BeckhoffIoSubscribeUpdateVariable( CASSETE_DOORS_LEFT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( CASSETE_DOORS_RIGHT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( CELL1_2_DOORS_LEFT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( CELL1_2_DOORS_RIGHT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( CELL3_4_DOORS_LEFT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( CELL3_4_DOORS_RIGHT_LOCKED);
BeckhoffIoSubscribeUpdateVariable( VPW_DOOR_CLOSED);
BeckhoffIoSubscribeUpdateVariable( SRD_PANEL_CLOSED);
BeckhoffIoSubscribeUpdateVariable( ALIGNER_DOOR_CLOSED);
BeckhoffIoSubscribeUpdateVariable( PLUMBLING_DOOR_CLOSED);
BeckhoffIoSubscribeUpdateVariable( SRD_PANEL_CLOSED);
BeckhoffIoSubscribeUpdateVariable( FACILITY_STO_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( PLATING_CELL1_2_STO1_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( PLATING_CELL1_2_STO2_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( PLATING_CELL3_4_STO1_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( PLATING_CELL3_4_STO2_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( RES1_2_STO_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( RES3_4_STO_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( SRD_STO_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( VPW_STO_ESTOP_RESTART);
BeckhoffIoSubscribeUpdateVariable( REQ_ALIGNER_DOOR_LOCK);
BeckhoffIoSubscribeUpdateVariable( REQ_CASSETE_DOORS_LOCK);
BeckhoffIoSubscribeUpdateVariable( REQ_CELL1_2_DOORS_LOCK);
BeckhoffIoSubscribeUpdateVariable( REQ_CELL3_4_DOORS_LOCK);
BeckhoffIoSubscribeUpdateVariable(HALT_BUTTON_LEFT);
BeckhoffIoSubscribeUpdateVariable(HALT_BUTTON_RIGHT);
}
///
/// 订阅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;
}
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 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 Aligner Door
///
///
public bool LockAlignerDoor()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_ALIGNER_DOOR_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// Unlock Process Door
///
///
public bool UnlockAlignerDoor()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_ALIGNER_DOOR_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, false);
}
///
/// Lock cassete Door
///
///
public bool LockCasseteDoor()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CASSETE_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// Unlock cassete Door
///
///
public bool UnlockCasseteDoor()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CASSETE_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, false);
}
///
/// Lock Cell1_2 Door
///
///
public bool LockCell12Door()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CELL1_2_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// Unlock Cell1_2 Door
///
///
public bool UnlockCell12Door()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CELL1_2_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, false);
}
///
/// Lock Cell3_4 Door
///
///
public bool LockCell34Door()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CELL3_4_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// Unlock Cell3_4 Door
///
///
public bool UnlockCell34Door()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{REQ_CELL3_4_DOORS_LOCK}");
return IOModuleManager.Instance.WriteIoValue(ioName, false);
}
#endregion
public void Monitor()
{
}
public void Reset()
{
}
public void Terminate()
{
_periodicJob.Stop(false);
}
}
}