ReservoirDosingRoutine.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using Aitex.Core.RT.SCCore;
  5. using PunkHPX8_Core;
  6. using PunkHPX8_RT.Devices.Reservoir;
  7. using MECF.Framework.Common.Persistent.Reservoirs;
  8. using MECF.Framework.Common.RecipeCenter;
  9. using MECF.Framework.Common.Routine;
  10. using System;
  11. namespace PunkHPX8_RT.Modules.Reservoir
  12. {
  13. public class ReservoirDosingRoutine : RoutineBase, IRoutine
  14. {
  15. private enum DosingStep
  16. {
  17. Dosing_Check_Status,
  18. Dosing_Dispensing,
  19. Dosing_Wait_Complete,
  20. Dosing_Done,
  21. End
  22. }
  23. #region 内部变量
  24. /// <summary>
  25. /// 模块名称
  26. /// </summary>
  27. private string _moduleName;
  28. /// <summary>
  29. /// Replen名称
  30. /// </summary>
  31. private string _replenName;
  32. /// <summary>
  33. /// Replen ID
  34. /// </summary>
  35. private int _replenId;
  36. /// <summary>
  37. /// 本次需要补充的液体体积(mL)
  38. /// </summary>
  39. private double _targetVolume;
  40. /// <summary>
  41. /// RDS Recipe
  42. /// </summary>
  43. private RdsRecipe _rdsRecipe;
  44. /// <summary>
  45. /// 补液开始时间(ms)
  46. /// </summary>
  47. private int _dosingStartTime;
  48. /// <summary>
  49. /// 补液需要消耗的时间(ms)
  50. /// </summary>
  51. private int _dosingTime;
  52. /// <summary>
  53. /// InitialDosingSpeed(mL/min)
  54. /// </summary>
  55. private double _initialDosingSpeed;
  56. /// <summary>
  57. /// Replen Persistent Value
  58. /// </summary>
  59. private ReplenPersistentValue _persistentValue;
  60. /// <summary>
  61. /// 是否为Auto模式
  62. /// </summary>
  63. private bool _isAutoMode;
  64. /// <summary>
  65. /// Dosing超时时间
  66. /// </summary>
  67. private int _dosingOutTime;
  68. #endregion
  69. #region 属性
  70. /// <summary>
  71. /// 状态
  72. /// </summary>
  73. public string State { get { return Runner.CurrentStep.ToString(); } }
  74. #endregion
  75. /// <summary>
  76. /// 构造函数
  77. /// </summary>
  78. /// <param name="name"></param>
  79. public ReservoirDosingRoutine(string module, string replenName) : base(module)
  80. {
  81. _moduleName = module;
  82. _replenName = replenName;
  83. _replenId = int.Parse(_replenName.Substring(6, 1));
  84. }
  85. public void Abort()
  86. {
  87. Runner.Stop($"{_moduleName}_{_replenName} Stop Dosing");
  88. }
  89. public RState Monitor()
  90. {
  91. Runner.Run(DosingStep.Dosing_Check_Status, CheckStatus, _delay_1ms)
  92. .Run(DosingStep.Dosing_Dispensing, Dispensing, _delay_1ms)
  93. .WaitWithStopCondition(DosingStep.Dosing_Wait_Complete, WaitCompleteEndStatus, WaitCompleteErrorStatus, _dosingTime + _dosingOutTime)
  94. .End(DosingStep.Dosing_Done, DoneOperation, _delay_1ms);
  95. return Runner.Status;
  96. }
  97. /// <summary>
  98. /// 启动
  99. /// </summary>
  100. /// <param name="objs"></param>
  101. /// <returns></returns>
  102. public RState Start(params object[] objs)
  103. {
  104. _targetVolume = (double)objs[0];
  105. _isAutoMode = (bool)objs[1];
  106. //加载持久化文件
  107. _persistentValue = ReplenPersistentManager.Instance.GetReplenPersistentValue(_moduleName, _replenName);
  108. if (_persistentValue == null)
  109. {
  110. LOG.WriteLog(eEvent.ERR_RESERVOIR, _moduleName, "Replen Persistent Value Object is not exist");
  111. return RState.Failed;
  112. }
  113. //手动模式下无需Recipe
  114. if (_isAutoMode && _rdsRecipe == null)
  115. {
  116. LOG.WriteLog(eEvent.ERR_RESERVOIR, _moduleName, "RDS recipe is null");
  117. return RState.Failed;
  118. }
  119. if (SC.ContainsItem($"Reservoir.{_moduleName}.DosingOutTime"))
  120. {
  121. _dosingOutTime = (int)SC.GetValue<double>($"Reservoir.{_moduleName}.DosingOutTime") * 60 * 1000;
  122. }
  123. return Runner.Start(_moduleName +"_"+_replenName, "Dosing");
  124. }
  125. #region Operation
  126. /// <summary>
  127. /// Check Status
  128. /// </summary>
  129. /// <param name="param"></param>
  130. /// <returns></returns>
  131. private bool CheckStatus()
  132. {
  133. return true;
  134. }
  135. /// <summary>
  136. /// Dispensing
  137. /// </summary>
  138. /// <param name="param"></param>
  139. /// <returns></returns>
  140. private bool Dispensing()
  141. {
  142. //计算Dosing时间
  143. if (SC.ContainsItem($"Reservoir.{_moduleName}.InitialDosingSpeed"))
  144. {
  145. _initialDosingSpeed = SC.GetValue<double>($"Reservoir.{_moduleName}.InitialDosingSpeed");
  146. double pumpFactor = _persistentValue.ReplenPumpFactor;
  147. double adjustSpeed = pumpFactor * _initialDosingSpeed;
  148. SCConfigItem item = SC.GetConfigItem($"Reservoir.{Module}.InitialDosingSpeed");
  149. double speedMax = double.Parse(item.Max);
  150. if(adjustSpeed > speedMax)
  151. {
  152. _dosingTime = (int)((pumpFactor * _targetVolume / speedMax) * 60 * 1000);
  153. _initialDosingSpeed = speedMax / pumpFactor;
  154. }
  155. else
  156. {
  157. _dosingTime = (int)((_targetVolume / _initialDosingSpeed) * 60 * 1000);
  158. }
  159. }
  160. //记录Dosing开始时刻
  161. _dosingStartTime = Environment.TickCount;
  162. //打开Replen Pump
  163. Object[] args = new object[1];
  164. args[0] = _replenName;
  165. return true;
  166. }
  167. /// <summary>
  168. /// WaitComplete End
  169. /// </summary>
  170. /// <returns></returns>
  171. private bool WaitCompleteEndStatus()
  172. {
  173. int tick = Environment.TickCount - _dosingStartTime;
  174. //更新实时DosingVolume
  175. _persistentValue.CurrentDosingVolume = Math.Round(tick * (_initialDosingSpeed / 1000 / 60), 2);
  176. ReplenPersistentManager.Instance.UpdatePersistentValue(_moduleName, _replenName);
  177. if (tick >= _dosingTime)
  178. {
  179. //关闭Replen Pump
  180. Object[] args = new object[1];
  181. args[0] = _replenName;
  182. return true;
  183. }
  184. return false;
  185. }
  186. /// <summary>
  187. /// WaitComplete Error
  188. /// </summary>
  189. /// <returns></returns>
  190. private bool WaitCompleteErrorStatus()
  191. {
  192. return false;
  193. }
  194. /// <summary>
  195. /// Done Operation
  196. /// </summary>
  197. /// <param name="param"></param>
  198. /// <returns></returns>
  199. private bool DoneOperation()
  200. {
  201. //数据清零
  202. _persistentValue.CurrentDosingVolume = 0;
  203. _persistentValue.IsDosingRunning = false;
  204. _persistentValue.AutoDosingStartAmpHour = 0;
  205. _persistentValue.AutoDosingStartTime = DateTime.MinValue;
  206. _persistentValue.RemainDosingVolume = Math.Round(_persistentValue.RemainDosingVolume - _targetVolume, 2);
  207. ReplenPersistentManager.Instance.UpdatePersistentValue(_moduleName, _replenName);
  208. _targetVolume = 0;
  209. _dosingTime = 0;
  210. _dosingStartTime = 0;
  211. return true;
  212. }
  213. #endregion
  214. }
  215. }