using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.ToolLayout;
using MECF.Framework.Common.TwinCat;
using CyberX8_Core;
using CyberX8_RT.Devices.Metal;
using CyberX8_RT.Devices.Reservoir;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Modules.Reservoir
{
public class CAPumpOnRoutine : RoutineBase, IRoutine
{
private enum CAPumpStep
{
PumpSpeed,
PumpEnable,
Delay,
CheckRunning,
CAByPass,
CellsFlowValve,
FlowDelay,
CheckFlow,
End
}
#region 常量
private const string CA_PUMP_SPEED = "CAPumpSpeed";
private const string CA_PUMP_RUNNING = "CAPumpRunning";
private const string CA_HED_FLOW = "CAHedFlow";
private const string CA_PUMP_ENABLE = "CAPumpEnable";
private const string CA_BY_PASS = "CAByPass";
#endregion
#region 内部变量
private double _caPumpSpeed = 5000;
private int _flowFaultHoldOffTime = 10000;
private double _caMainFlowFaultLow = 5.0;
private double _caHedFlowLowLimit = 3.0;
private CompactMembranReservoirDevice _device;
private int _caOpenValveCount = 0;
#endregion
///
/// 构造函数
///
///
public CAPumpOnRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop("Manual abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Run(CAPumpStep.PumpSpeed, () => { return CAPumpSpeed(_caPumpSpeed); }, _delay_2s)
.Run(CAPumpStep.PumpEnable, CAPumpEnable, _delay_1ms)
.Delay(CAPumpStep.Delay, 500)
.Run(CAPumpStep.CheckRunning, CheckCAPumpRunning, _delay_1ms)
.Run(CAPumpStep.CAByPass,CAByPass,_delay_1ms)
.Run(CAPumpStep.CellsFlowValve,OpenAllCellsFlowValve,_delay_1ms)
.Delay(CAPumpStep.FlowDelay, _flowFaultHoldOffTime)
.Run(CAPumpStep.CheckFlow, CheckAllFlow, _delay_1ms)
.End(CAPumpStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
///
/// Pump Speed
///
///
///
private bool CAPumpSpeed(double speed)
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_PUMP_SPEED}");
return IOModuleManager.Instance.WriteIoValue(ioName, speed);
}
///
/// Pump Enable
///
///
///
private bool CAPumpEnable()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_PUMP_ENABLE}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// Pump disable
///
///
private bool CAPumpDisable()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_PUMP_ENABLE}");
return IOModuleManager.Instance.WriteIoValue(ioName, false);
}
///
/// 检验CA Running Sensor
///
///
private bool CheckCAPumpRunning()
{
if(!_device.ReservoirData.CAPumpRunning)
{
LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, "CA Pump Running Sensor is false");
return false;
}
return true;
}
///
/// CA Bypass
///
///
///
private bool CAByPass()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_BY_PASS}");
return IOModuleManager.Instance.WriteIoValue(ioName, true);
}
///
/// 检验所有流量
///
///
private bool OpenAllCellsFlowValve()
{
bool result = false;
ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
if (reservoirItem != null)
{
List metalItems = reservoirItem.MetalCells;
if (metalItems != null && metalItems.Count > 0)
{
foreach (MetalItem metalItem in metalItems)
{
if (metalItem.Installed)
{
CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
if (metalDevice != null&&metalDevice.IsAuto)
{
result= metalDevice.CellFlowValveOn("", null);
if(!result)
{
return false;
}
_caOpenValveCount++;
}
}
}
}
}
return true;
}
///
/// 关闭所有Cell的Flow Valve
///
///
private bool CloseAllCellsFlowValve()
{
ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
if (reservoirItem != null)
{
List metalItems = reservoirItem.MetalCells;
if (metalItems != null && metalItems.Count > 0)
{
foreach (MetalItem metalItem in metalItems)
{
if (metalItem.Installed)
{
CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
if (metalDevice != null&&metalDevice.IsAuto)
{
metalDevice.CellFlowValveOff("", null);
}
}
}
}
}
return true;
}
///
/// 检验所有流量
///
///
private bool CheckAllFlow()
{
if (_caOpenValveCount == 0)
{
return true;
}
double flow = 0.0;
ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module.ToString());
if (reservoirItem != null)
{
List metalItems = reservoirItem.MetalCells;
if (metalItems != null && metalItems.Count > 0)
{
foreach (MetalItem metalItem in metalItems)
{
if (metalItem.Installed)
{
CompactMembranMetalDevice metalDevice = DEVICE.GetDevice(metalItem.ModuleName);
if (metalDevice != null)
{
flow += metalDevice.MetalDeviceData.CellFlow;
}
}
}
}
}
if(flow<=_caMainFlowFaultLow)
{
CloseAllCellsFlowValve();
CAPumpDisable();
LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, $"total flow {flow} is not over {_caMainFlowFaultLow}");
return false;
}
else
{
LOG.WriteLog(eEvent.INFO_RESERVOIR, Module, $"total flow {flow} is over {_caMainFlowFaultLow}");
}
if(!CheckHedFlow())
{
return false;
}
return true;
}
///
/// 检验HED流量
///
///
private bool CheckHedFlow()
{
double hedFlow = _device.ReservoirData.CAHedFlow;
if(hedFlow<=_caHedFlowLowLimit)
{
CloseAllCellsFlowValve();
CAPumpDisable();
LOG.WriteLog(eEvent.ERR_RESERVOIR, Module, $"CA Hed Flow {hedFlow} is less than {_caHedFlowLowLimit}");
return false;
}
else
{
LOG.WriteLog(eEvent.INFO_RESERVOIR, Module, $"CA Hed Flow {hedFlow} is over than {_caHedFlowLowLimit}");
}
return true;
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_device = DEVICE.GetDevice(Module);
_caPumpSpeed = SC.GetValue("Reservoir.CADefaultPumpSpeed");
_flowFaultHoldOffTime = SC.GetValue($"Reservoir.{Module}.FlowFaultHoldOffTime");
_caMainFlowFaultLow = SC.GetValue($"Reservoir.{Module}.CAMainFlowFaultLow");
_caHedFlowLowLimit = SC.GetValue($"Reservoir.{Module}.HEDFlowLowLimit");
_caOpenValveCount = 0;
return Runner.Start(Module, "CA Pump On");
}
}
}