StandardHotPHRoutine.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Routine;
  3. using Aitex.Core.RT.SCCore;
  4. using CyberX8_Core;
  5. using MECF.Framework.Common.Routine;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace CyberX8_RT.Devices.Reservoir
  12. {
  13. public class StandardHotPHRoutine : RoutineBase, IRoutine
  14. {
  15. private enum PHDetectStep
  16. {
  17. PHValveOn,
  18. DelayFlowTime,
  19. PHValveOff,
  20. DelayStableTime,
  21. Sample,
  22. SampleRecord,
  23. AverageSample,
  24. End
  25. }
  26. #region 内部变量
  27. private StandardHotReservoirDevice _device;
  28. private int _phFlowTime;
  29. private int _phStabilizationTime;
  30. private int _phSampleTime;
  31. private int _phSamples;
  32. private DateTime _sampTime=DateTime.Now;
  33. private List<double> _sampleLst = new List<double>();
  34. private int _sampleInterval = 0;
  35. private DateTime _startSampleTime = DateTime.Now;
  36. #endregion
  37. /// <summary>
  38. /// 构造函数
  39. /// </summary>
  40. /// <param name="module"></param>
  41. public StandardHotPHRoutine(string module) : base(module)
  42. {
  43. }
  44. /// <summary>
  45. /// 中止
  46. /// </summary>
  47. public void Abort()
  48. {
  49. Runner.Stop("Manual Abort");
  50. }
  51. /// <summary>
  52. /// 监控
  53. /// </summary>
  54. /// <returns></returns>
  55. public RState Monitor()
  56. {
  57. Runner.Run(PHDetectStep.PHValveOn, _device.PHValveOn, _delay_1ms)
  58. .Delay(PHDetectStep.DelayFlowTime, _phFlowTime)
  59. .Run(PHDetectStep.PHValveOff, _device.PHValveOff, _delay_1ms)
  60. .Delay(PHDetectStep.DelayStableTime, _phStabilizationTime)
  61. .Run(PHDetectStep.Sample, StartSample, _delay_1ms)
  62. .Run(PHDetectStep.SampleRecord, RecordSample, CheckSampleComplete, _phSampleTime + _delay_2m)
  63. .Run(PHDetectStep.AverageSample, AverageSample)
  64. .End(PHDetectStep.End, NullFun, _delay_1ms);
  65. return Runner.Status;
  66. }
  67. /// <summary>
  68. /// 启动采样
  69. /// </summary>
  70. /// <returns></returns>
  71. private bool StartSample()
  72. {
  73. _sampTime = DateTime.Now;
  74. _startSampleTime = DateTime.Now;
  75. return true;
  76. }
  77. /// <summary>
  78. /// 记录采样
  79. /// </summary>
  80. private bool RecordSample()
  81. {
  82. if(DateTime.Now.Subtract(_sampTime).TotalMilliseconds>=_sampleInterval)
  83. {
  84. _sampleLst.Add(_device.ReservoirData.PHValue);
  85. _sampTime = DateTime.Now;
  86. }
  87. return true;
  88. }
  89. /// <summary>
  90. /// 检验采样是否结束
  91. /// </summary>
  92. /// <returns></returns>
  93. private bool CheckSampleComplete()
  94. {
  95. if (_sampleLst.Count>= _phSamples)
  96. {
  97. return true;
  98. }
  99. if (DateTime.Now.Subtract(_startSampleTime).TotalMilliseconds >= _phSampleTime)
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /// <summary>
  106. /// 计算平均值
  107. /// </summary>
  108. /// <returns></returns>
  109. private bool AverageSample()
  110. {
  111. double total=0.0;
  112. for (int i = 0; i < _sampleLst.Count; i++)
  113. {
  114. total += _sampleLst[i];
  115. }
  116. double avarage = Math.Round(total / _sampleLst.Count,2);
  117. _device.AveragePH = avarage;
  118. return true;
  119. }
  120. /// <summary>
  121. /// 启动
  122. /// </summary>
  123. /// <param name="objs"></param>
  124. /// <returns></returns>
  125. public RState Start(params object[] objs)
  126. {
  127. _sampleLst.Clear();
  128. _device = DEVICE.GetDevice<StandardHotReservoirDevice>(Module);
  129. _phFlowTime =(int)Math.Round(SC.GetValue<double>("Reservoir.PHFlowTime")*1000,0);
  130. _phStabilizationTime = (int)Math.Round(SC.GetValue<double>("Reservoir.PHStabilizationTime")*1000,0);
  131. _phSampleTime =(int)(Math.Round(SC.GetValue<double>("Reservoir.PHSampleTime")*1000,0));
  132. _phSamples = SC.GetValue<int>("Reservoir.PHSamples");
  133. _sampleInterval = (int)(Math.Round((double)_phFlowTime / _phSamples,0));
  134. return Runner.Start(Module, "Start PH Detection");
  135. }
  136. }
  137. }