using Aitex.Core.RT.Device; using Aitex.Core.RT.Routine; using Aitex.Core.RT.SCCore; using CyberX8_Core; using MECF.Framework.Common.Routine; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CyberX8_RT.Devices.Reservoir { public class StandardHotPHRoutine : RoutineBase, IRoutine { private enum PHDetectStep { PHValveOn, DelayFlowTime, PHValveOff, DelayStableTime, Sample, SampleRecord, AverageSample, End } #region 内部变量 private StandardHotReservoirDevice _device; private int _phFlowTime; private int _phStabilizationTime; private int _phSampleTime; private int _phSamples; private DateTime _sampTime=DateTime.Now; private List _sampleLst = new List(); private int _sampleInterval = 0; private DateTime _startSampleTime = DateTime.Now; #endregion /// /// 构造函数 /// /// public StandardHotPHRoutine(string module) : base(module) { } /// /// 中止 /// public void Abort() { Runner.Stop("Manual Abort"); } /// /// 监控 /// /// public RState Monitor() { Runner.Run(PHDetectStep.PHValveOn, _device.PHValveOn, _delay_1ms) .Delay(PHDetectStep.DelayFlowTime, _phFlowTime) .Run(PHDetectStep.PHValveOff, _device.PHValveOff, _delay_1ms) .Delay(PHDetectStep.DelayStableTime, _phStabilizationTime) .Run(PHDetectStep.Sample, StartSample, _delay_1ms) .Run(PHDetectStep.SampleRecord, RecordSample, CheckSampleComplete, _phSampleTime + _delay_2m) .Run(PHDetectStep.AverageSample, AverageSample) .End(PHDetectStep.End, NullFun, _delay_1ms); return Runner.Status; } /// /// 启动采样 /// /// private bool StartSample() { _sampTime = DateTime.Now; _startSampleTime = DateTime.Now; return true; } /// /// 记录采样 /// private bool RecordSample() { if(DateTime.Now.Subtract(_sampTime).TotalMilliseconds>=_sampleInterval) { _sampleLst.Add(_device.ReservoirData.PHValue); _sampTime = DateTime.Now; } return true; } /// /// 检验采样是否结束 /// /// private bool CheckSampleComplete() { if (_sampleLst.Count>= _phSamples) { return true; } if (DateTime.Now.Subtract(_startSampleTime).TotalMilliseconds >= _phSampleTime) { return true; } return false; } /// /// 计算平均值 /// /// private bool AverageSample() { double total=0.0; for (int i = 0; i < _sampleLst.Count; i++) { total += _sampleLst[i]; } double avarage = Math.Round(total / _sampleLst.Count,2); _device.AveragePH = avarage; return true; } /// /// 启动 /// /// /// public RState Start(params object[] objs) { _sampleLst.Clear(); _device = DEVICE.GetDevice(Module); _phFlowTime =(int)Math.Round(SC.GetValue("Reservoir.PHFlowTime")*1000,0); _phStabilizationTime = (int)Math.Round(SC.GetValue("Reservoir.PHStabilizationTime")*1000,0); _phSampleTime =(int)(Math.Round(SC.GetValue("Reservoir.PHSampleTime")*1000,0)); _phSamples = SC.GetValue("Reservoir.PHSamples"); _sampleInterval = (int)(Math.Round((double)_phFlowTime / _phSamples,0)); return Runner.Start(Module, "Start PH Detection"); } } }