using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.CommonData.Prewet;
using MECF.Framework.Common.CommonData.Vpw;
using MECF.Framework.Common.IOCore;
using MECF.Framework.Common.Persistent.Prewet;
using MECF.Framework.Common.Persistent.VpwMain;
using PunkHPX8_RT.Modules;
using PunkHPX8_RT.Modules.VpwMain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Devices.VpwMain
{
public class VpwMainDevice : BaseDevice, IDevice
{
#region 常量
private const string COMMON_DATA = "CommonData";
private const string PERSISTENT_VALUE = "PersistentValue";
private const string CHAMBER_CLOSED = "ChamberClosed";
private const string CHAMBER_OPENED = "ChamberOpened";
private const string CHAMBER_CLOSE = "ChamberClose";
private const string LEAK_DETECTED = "LeakDetected";
private const string VACUUM_PUMP_PRESSURE = "VacuumPumpPressure";
private const string VACUUM_PUMP_POWER = "VacuumPumpPower";
private const string VACUUM_PUMP_ENABLE = "VacuumPumpEnable";
private const string VACUUM_PUMP_SPEED_ENABLE = "VacuumPumpSpeedEnable";
private const string VACUUM_PUMP_SPEED = "VacuumPumpSpeed";
private const string BOOSTER_PUMP_SPEED = "BoosterPumpSpeed";
private const string BOOSTER_PUMP_CURRENT = "BoosterPumpCurrent";
private const string BOOSTER_PUMP_ENABLE = "BoosterPumpEnable";
private const string BOOSTER_PUMP_STATUS = "BoosterPumpStatus";
private const string DIW_ENABLE = "DiwEnable";
private const string DIW_PROCESS = "DiwProcess";
private const string DIW_DEGAS = "DiwDegas";
private const string DIW_TOTAL_FLOW = "DiwTotalFlow";
private const string DIW_PRESSURE = "DiwPressure";
private const string DEGAS_ADJUST = "DegasAdjust";
private const string DEGAS_PURGE = "DegasPurge";
private const string DEGAS_PUMP_ENABLE = "DegasPumpEnable";
private const string DEGAS_PUMP_PRESSURE = "DegasPumpPressure";
#endregion
#region 内部变量
///
/// 变量是否初始化字典
///
private Dictionary _variableInitializeDic = new Dictionary();
///
/// 数据
///
private VpwMainCommonData _commonData=new VpwMainCommonData();
///
/// 持久性数值
///
private VpwMainPersistentValue _vpwMainPersistentValue;
///
/// 上一次Booster泵速
///
private short _lastBoosterPumpSpeed = 0;
///
/// 定时器任务
///
private PeriodicJob _periodicJob;
private bool _isDataInitialized;
#endregion
#region 属性
///
/// 数据
///
public VpwMainCommonData CommonData { get { return _commonData; } }
#endregion
///
/// 构造函数
///
///
public VpwMainDevice(string moduleName) : base(moduleName, moduleName, moduleName, moduleName)
{
}
#region 初始化
///
/// 初始化
///
///
public bool Initialize()
{
InitializeParameter();
InitializeRoutine();
SubscribeData();
SubscribeValueAction();
InitializeOperation();
return true;
}
///
/// 初始化参数
///
private void InitializeParameter()
{
_vpwMainPersistentValue = VpwMainPersistentManager.Instance.GetPersistentValue(Module);
if (_vpwMainPersistentValue != null)
{
_lastBoosterPumpSpeed = _vpwMainPersistentValue.Speed;
}
else
{
LOG.WriteLog(eEvent.ERR_PREWET, Module, "Persistent Value Object is not exist");
}
_commonData.BoosterPumpPressureData = new MECF.Framework.Common.CommonData.CommonLimitData();
}
///
/// 初始化Routine
///
private void InitializeRoutine()
{
}
///
/// 订阅
///
private void SubscribeData()
{
DATA.Subscribe($"{Module}.{PERSISTENT_VALUE}", () => _vpwMainPersistentValue, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.{COMMON_DATA}", () => CommonData, SubscriptionAttribute.FLAG.IgnoreSaveDB);
}
///
/// 订阅数据
///
private void SubscribeValueAction()
{
IoSubscribeUpdateVariable(CHAMBER_CLOSED);
IoSubscribeUpdateVariable(CHAMBER_OPENED);
IoSubscribeUpdateVariable(BOOSTER_PUMP_STATUS);
IoSubscribeUpdateVariable(BOOSTER_PUMP_SPEED);
IoSubscribeUpdateVariable(BOOSTER_PUMP_ENABLE);
IoSubscribeUpdateVariable(BOOSTER_PUMP_CURRENT);
IoSubscribeUpdateVariable(DEGAS_PURGE);
IoSubscribeUpdateVariable(DEGAS_ADJUST);
IoSubscribeUpdateVariable(DEGAS_PUMP_PRESSURE);
IoSubscribeUpdateVariable(DEGAS_PUMP_ENABLE);
IoSubscribeUpdateVariable(DIW_ENABLE);
IoSubscribeUpdateVariable(DIW_PROCESS);
IoSubscribeUpdateVariable(DIW_PRESSURE);
IoSubscribeUpdateVariable(DIW_DEGAS);
IoSubscribeUpdateVariable(DIW_TOTAL_FLOW);
IoSubscribeUpdateVariable(VACUUM_PUMP_ENABLE);
IoSubscribeUpdateVariable(VACUUM_PUMP_POWER);
IoSubscribeUpdateVariable(VACUUM_PUMP_PRESSURE);
IoSubscribeUpdateVariable(VACUUM_PUMP_SPEED);
IoSubscribeUpdateVariable(VACUUM_PUMP_SPEED_ENABLE);
IoSubscribeUpdateVariable(LEAK_DETECTED);
}
///
/// 初始化变量
///
///
private void IoSubscribeUpdateVariable(string variable)
{
_variableInitializeDic[variable] = false;
IOModuleManager.Instance.SubscribeModuleVariable(Module, variable, UpdateVariableValue);
}
///
/// 更新变量数值
///
///
///
private void UpdateVariableValue(string variable, object value)
{
if (!_commonData.IsDataInitialized)
{
_commonData.IsDataInitialized = true;
_commonData.BoosterPumpModel = "Manual";
}
PropertyInfo property = _commonData.GetType().GetProperty(variable);
if (property != null)
{
property.SetValue(_commonData, value);
}
if (_variableInitializeDic.ContainsKey(variable) && !_variableInitializeDic[variable])
{
_variableInitializeDic[variable] = true;
}
switch (variable)
{
case BOOSTER_PUMP_STATUS:
string statusContent = _commonData.BoosterPumpStatus ? "On" : "Off";
_commonData.BoosterPumpStatusContent = $"{_commonData.BoosterPumpModel}: {statusContent}";
break;
case DIW_PRESSURE:
if (double.TryParse(value.ToString(), out var pressure))
{
_commonData.BoosterPumpPressureData.Value = pressure;
}
break;
}
}
///
/// 初始化OP
///
private void InitializeOperation()
{
OP.Subscribe($"{Module}.VacuumPumpPowerOn", (cmd,para)=> { return VacuumPumpPowerOn(); });
OP.Subscribe($"{Module}.VacuumPumpPowerOff", (cmd, para) => { return VacuumPumpPowerOff(); });
OP.Subscribe($"{Module}.VacuumPumpEnable", (cmd, para) => { return VacuumPumpEnable(); });
OP.Subscribe($"{Module}.VacuumPumpDisable", (cmd, para) => { return VacuumPumpDisable(); });
OP.Subscribe($"{Module}.VacuumPumpSpeedEnable", (cmd, para) => { return VacuumSpeedEnable(); });
OP.Subscribe($"{Module}.VacuumPumpSpeedDisable", (cmd, para) => { return VacuumSpeedDisable(); });
OP.Subscribe($"{Module}.VacuumPumpSpeed", (cmd, para) => { return BoosterPumpSpeedKeyDownOperation(cmd, para); });
OP.Subscribe($"{Module}.DegasPumpEnable", (cmd, para) => { return DegasPumpEnable(); });
OP.Subscribe($"{Module}.DegasPumpDisable", (cmd, para) => { return DegasPumpDisable(); });
OP.Subscribe($"{Module}.DegasAdjustOn", (cmd, para) => { return DegasAdjustOn(); });
OP.Subscribe($"{Module}.DegasAdjustOff", (cmd, para) => { return DegasAdjustOff(); });
OP.Subscribe($"{Module}.DiwDegasValveOn", (cmd, para) => { return DiwDegasValveOn(); });
OP.Subscribe($"{Module}.DiwDegasValveOff", (cmd, para) => { return DiwDegasValveOff(); });
OP.Subscribe($"{Module}.BoosterPumpEnable", (cmd, para) => { return BoosterPumpEnable(); });
OP.Subscribe($"{Module}.BoosterPumpDisable", (cmd, para) => { return BoosterPumpDisable(); });
OP.Subscribe($"{Module}.BoosterPumpSpeed", (cmd, para) => {
short speed = short.Parse(para[0].ToString());
return BoosterPumpSpeed(speed);
});
OP.Subscribe($"{Module}.ChamberUp", (cmd, para) => { return ChamberUp(); });
OP.Subscribe($"{Module}.ChamberDown", (cmd, para) => { return ChamberDown(); });
OP.Subscribe($"{Module}.BoosterPumpSpeedAuto", (cmd, para) => { return BoosterPumpSpeedAutoOperation(); });
OP.Subscribe($"{Module}.BoosterPumpSpeedManual", (cmd, para) => { return BoosterPumpSpeedManualOperation(); });
OP.Subscribe($"{Module}.DisabledAction", DisabledOperation);
OP.Subscribe($"{Module}.ManualAction", ManualOperation);
OP.Subscribe($"{Module}.AutoAction", AutoOperation);
OP.Subscribe($"{Module}.EngineeringModeAction", EngineeringModeOperation);
OP.Subscribe($"{Module}.ProductionModeAction", ProductionModeOperation);
}
#endregion
#region Action
#region Vacum Pump
///
/// Pump Power On
///
///
public bool VacuumPumpPowerOn()
{
return WriteVariableValue(VACUUM_PUMP_POWER, true);
}
///
/// Pump Power Off
///
///
public bool VacuumPumpPowerOff()
{
return WriteVariableValue(VACUUM_PUMP_POWER, false);
}
///
/// Pump Enable
///
///
public bool VacuumPumpEnable()
{
return WriteVariableValue(VACUUM_PUMP_ENABLE, true);
}
///
/// Pump Disable
///
///
public bool VacuumPumpDisable()
{
return WriteVariableValue(VACUUM_PUMP_ENABLE, false);
}
///
/// Speed Enable
///
///
public bool VacuumSpeedEnable()
{
return WriteVariableValue(VACUUM_PUMP_SPEED_ENABLE, true);
}
///
/// Speed disable
///
///
public bool VacuumSpeedDisable()
{
return WriteVariableValue(VACUUM_PUMP_SPEED_ENABLE, false);
}
///
/// 写入速度
///
///
///
public bool WriteVacuumSpeed(short speed)
{
return WriteVariableValue(VACUUM_PUMP_SPEED, speed);
}
#endregion
#region Degas Pump
///
/// Degas Pump Enable
///
///
public bool DegasPumpEnable()
{
return WriteVariableValue(DEGAS_PUMP_ENABLE, true);
}
///
/// Degas Pump disable
///
///
public bool DegasPumpDisable()
{
return WriteVariableValue(DEGAS_PUMP_ENABLE, false);
}
///
/// Degas Adjust On
///
///
private bool DegasAdjustOn()
{
return WriteVariableValue(DEGAS_ADJUST, true);
}
///
/// Degas Adjust Off
///
///
private bool DegasAdjustOff()
{
return WriteVariableValue(DEGAS_ADJUST, false);
}
///
/// DIW Degas Valve On
///
///
private bool DiwDegasValveOn()
{
return WriteVariableValue(DIW_DEGAS, true);
}
///
/// DIW Degas Valve Off
///
///
private bool DiwDegasValveOff()
{
return WriteVariableValue(DIW_DEGAS, false);
}
#endregion
#region Booster Pump
///
/// Booster Pump enable
///
///
public bool BoosterPumpEnable()
{
return WriteVariableValue(BOOSTER_PUMP_ENABLE, true);
}
///
/// Booster Pump Disable
///
///
public bool BoosterPumpDisable()
{
return WriteVariableValue(BOOSTER_PUMP_ENABLE, false);
}
///
/// 写入Booster泵速
///
///
///
public bool BoosterPumpSpeed(short speed)
{
return WriteVariableValue(BOOSTER_PUMP_SPEED, speed);
}
/// Booster Pump Speed回车操作
///
///
///
///
private bool BoosterPumpSpeedKeyDownOperation(string cmd, object[] param)
{
if (_commonData.BoosterPumpSpeedAuto)
{
LOG.WriteLog(eEvent.ERR_PREWET, Module, "Pump speed is auto,cannot change speed");
return false;
}
short speed = (short)param[0];
bool result = BoosterPumpSpeed(speed);
if (result)
{
_vpwMainPersistentValue.Speed = speed;
_lastBoosterPumpSpeed = speed;
PrewetPersistentManager.Instance.UpdatePersistentValue(Module);
}
return true;
}
#endregion
#region Chamber
///
/// 上升
///
///
public bool ChamberUp()
{
return WriteVariableValue(CHAMBER_CLOSE, false);
}
///
/// 下降
///
///
public bool ChamberDown()
{
return WriteVariableValue(CHAMBER_CLOSE, true);
}
///
/// Pump Speed手动模式
///
///
///
///
private bool BoosterPumpSpeedManualOperation()
{
_commonData.BoosterPumpSpeedAuto = true;
_commonData.BoosterPumpModel = "Manual";
string statusContent = _commonData.BoosterPumpStatus ? "On" : "Off";
_commonData.BoosterPumpStatusContent = $"{_commonData.BoosterPumpModel}: {statusContent}";
return true;
}
///
/// Pump Speed自动模式
///
///
///
///
private bool BoosterPumpSpeedAutoOperation()
{
_commonData.BoosterPumpSpeedAuto = true;
_commonData.BoosterPumpModel = "Auto";
string statusContent = _commonData.BoosterPumpStatus ? "On" : "Off";
_commonData.BoosterPumpStatusContent = $"{_commonData.BoosterPumpModel}: {statusContent}";
return true;
}
///
/// DisabledAction
///
///
///
///
private bool DisabledOperation(string cmd, object[] args)
{
string currentOperation = "Disabled";
VpwMainEntity vpwMainEntity = Singleton.Instance.GetModule(Module);
if (vpwMainEntity != null && _vpwMainPersistentValue != null && _vpwMainPersistentValue.OperatingMode != currentOperation)
{
string preOperation = _vpwMainPersistentValue.OperatingMode;
if (vpwMainEntity.IsBusy)
{
LOG.WriteLog(eEvent.ERR_VPWMAIN, Module, $"{Module} is Busy, can't switch to Disabled mode");
return false;
}
vpwMainEntity.EnterInit();
_vpwMainPersistentValue.OperatingMode = currentOperation;
LOG.WriteLog(eEvent.INFO_VPWMAIN, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");
}
VpwMainPersistentManager.Instance.UpdatePersistentValue(Module);
return true;
}
///
/// ManualAction
///
///
///
///
private bool ManualOperation(string cmd, object[] args)
{
string currentOperation = "Manual";
VpwMainEntity vpwMainEntity = Singleton.Instance.GetModule(Module);
if (vpwMainEntity != null && _vpwMainPersistentValue != null && _vpwMainPersistentValue.OperatingMode != currentOperation)
{
string preOperation = _vpwMainPersistentValue.OperatingMode;
if (vpwMainEntity.IsBusy)
{
LOG.WriteLog(eEvent.ERR_VPWMAIN, Module, $"{Module} is Busy, can't switch to Manual mode");
return false;
}
vpwMainEntity.EnterInit();
_vpwMainPersistentValue.OperatingMode = currentOperation;
LOG.WriteLog(eEvent.INFO_VPWMAIN, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");
}
VpwMainPersistentManager.Instance.UpdatePersistentValue(Module);
return true;
}
///
/// AutoAction
///
///
///
///
private bool AutoOperation(string cmd, object[] args)
{
string currentOperation = "Auto";
VpwMainEntity vpwMainEntity = Singleton.Instance.GetModule(Module);
if (vpwMainEntity != null && _vpwMainPersistentValue != null && _vpwMainPersistentValue.OperatingMode != currentOperation)
{
string preOperation = _vpwMainPersistentValue.OperatingMode;
if (vpwMainEntity.IsBusy)
{
LOG.WriteLog(eEvent.ERR_VPWMAIN, Module, $"{Module} is Busy, can't switch to Auto mode");
return false;
}
vpwMainEntity.EnterInit();
_vpwMainPersistentValue.OperatingMode = currentOperation;
LOG.WriteLog(eEvent.INFO_VPWMAIN, Module, $"Operating mode is switched from {preOperation} to {currentOperation}");
}
VpwMainPersistentManager.Instance.UpdatePersistentValue(Module);
return true;
}
///
/// EngineeringModeAction
///
///
///
///
private bool EngineeringModeOperation(string cmd, object[] args)
{
string currentRecipeOperation = "Engineering";
if (_vpwMainPersistentValue != null)
{
_vpwMainPersistentValue.RecipeOperatingMode = currentRecipeOperation;
}
VpwMainPersistentManager.Instance.UpdatePersistentValue(Module);
return true;
}
///
/// ProductionAction
///
///
///
///
private bool ProductionModeOperation(string cmd, object[] args)
{
string currentRecipeOperation = "Production";
if (_vpwMainPersistentValue != null)
{
_vpwMainPersistentValue.RecipeOperatingMode = currentRecipeOperation;
}
VpwMainPersistentManager.Instance.UpdatePersistentValue(Module);
return true;
}
#endregion
///
/// 写变量
///
///
///
///
private bool WriteVariableValue(string variable, object value)
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{variable}");
return IOModuleManager.Instance.WriteIoValue(ioName, value);
}
#endregion
///
/// 定时器
///
///
public bool OnTimer()
{
_commonData.BoosterPumpPressureData.MinError = SC.GetValue($"VPWMain.PumpPressure.Error_Min");
_commonData.BoosterPumpPressureData.MinWarning = SC.GetValue($"VPWMain.PumpPressure.Warning_Min");
_commonData.BoosterPumpPressureData.MaxError = SC.GetValue($"VPWMain.PumpPressure.Error_Max");
_commonData.BoosterPumpPressureData.MaxWarning = SC.GetValue($"VPWMain.PumpPressure.Warning_Max");
_commonData.PressureTarget = SC.GetValue($"VPWMain.PressureTarget");
return true;
}
///
/// 监控
///
public void Monitor()
{
}
public void Reset()
{
}
public void Terminate()
{
}
}
}