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.CommonData.Reservoir;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.ToolLayout;
using MECF.Framework.Common.TwinCat;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.Temperature;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Devices.Reservoir
{
public class CAPumpOnRoutine : RoutineBase, IRoutine
{
private enum CAPumpStep
{
PumpSpeed,
PumpEnable,
Delay,
CheckRunning,
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";
#endregion
#region 内部变量
private double _caPumpSpeed = 5000;
private int _flowFaultHoldOffTime = 10000;
private double _caMainFlowFaultLow = 5.0;
private ReservoirDevice _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)
.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 BeckhoffIOManager.Instance.WriteIoValue(ioName, speed);
}
///
/// Pump Enable
///
///
///
private bool CAPumpEnable()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_PUMP_ENABLE}");
return BeckhoffIOManager.Instance.WriteIoValue(ioName, true);
}
///
/// Pump disable
///
///
private bool CAPumpDisable()
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{CA_PUMP_ENABLE}");
return BeckhoffIOManager.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;
}
///
/// 检验所有流量
///
///
private bool CheckAllFlow()
{
double flow = _device.ReservoirData.CaFlow;
if(flow<=_caMainFlowFaultLow)
{
CAPumpDisable();
DisableHed();
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}");
}
return true;
}
private void DisableHed()
{
ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(Module);
if (!string.IsNullOrEmpty(reservoirItem.TCID))
{
TemperatureController temperatureController = DEVICE.GetDevice(reservoirItem.TCID);
temperatureController.DisableOperation("",null);
}
}
///
/// 启动
///
///
///
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");
_caOpenValveCount = 0;
return Runner.Start(Module, "CA Pump On");
}
}
}