ReservoirDiReplenHelper.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using CyberX8_RT.Devices.Facilities;
  5. using MECF.Framework.Common.Alarm;
  6. using MECF.Framework.Common.Persistent.Reservoirs;
  7. using MECF.Framework.Common.RecipeCenter;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace CyberX8_RT.Devices.Reservoir
  15. {
  16. public class ReservoirDiReplenHelper
  17. {
  18. #region 内部变量
  19. /// <summary>
  20. /// 模块名称
  21. /// </summary>
  22. private string _module;
  23. /// <summary>
  24. /// 持久化对象
  25. /// </summary>
  26. private ReservoirsPersistentValue _persistentValue;
  27. /// <summary>
  28. /// 锁
  29. /// </summary>
  30. private object _locker = new object();
  31. #endregion
  32. /// <summary>
  33. /// 构造函数
  34. /// </summary>
  35. /// <param name="module"></param>
  36. /// <param name="persistentValue"></param>
  37. /// <param name="resRecipe"></param>
  38. public ReservoirDiReplenHelper(string module, ReservoirsPersistentValue persistentValue)
  39. {
  40. _module = module;
  41. _persistentValue = persistentValue;
  42. }
  43. /// <summary>
  44. /// 监控
  45. /// </summary>
  46. public void MonitorPeriodTime(Action<bool> timeOutAction)
  47. {
  48. double levelHysteresis = SC.GetValue<double>("Reservoir.LevelHysteresis");
  49. double diValveMaxOnTimePeriod = SC.GetValue<double>($"Reservoir.{_module}.DIValveMaxOnTimePeriod");
  50. //没有在注水
  51. if (_persistentValue != null && !_persistentValue.IsDiReplenOn)
  52. {
  53. //超过时间
  54. if (DateTime.Now.Subtract(_persistentValue.PeriodStartTime).TotalMinutes >= diValveMaxOnTimePeriod * 60)
  55. {
  56. LOG.WriteLog(eEvent.INFO_RESERVOIR, _module, $"Time is over config DIValveMaxOnTimePeriod:{diValveMaxOnTimePeriod} hour. Restart timer");
  57. timeOutAction(false);
  58. _persistentValue.PeriodStartTime = DateTime.Now;
  59. _persistentValue.TotalReplen = 0;
  60. _persistentValue.LastTotalReplen = 0;
  61. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// 监控手动注水
  67. /// </summary>
  68. public bool MonitorManualDiReplenComplete(int replenSecond, Func<string, object[], bool> direplenOffAction, Action<bool> timeOutAction)
  69. {
  70. lock (_locker)
  71. {
  72. _persistentValue.TotalReplen = _persistentValue.LastTotalReplen + (int)DateTime.Now.Subtract(_persistentValue.DiReplenTime).TotalSeconds;
  73. }
  74. //周期内累计补水超时
  75. double diValveMaxOnTime = SC.GetValue<double>($"Reservoir.{_module}.DIValveMaxOnTime");
  76. if (_persistentValue.TotalReplen >= diValveMaxOnTime * 60)
  77. {
  78. bool result = direplenOffAction("", null);
  79. if (result)
  80. {
  81. timeOutAction(true);
  82. LOG.WriteLog(eEvent.WARN_RESERVOIR, _module, $"Direplen time over config DIValveMaxOnTime:{diValveMaxOnTime} min");
  83. _persistentValue.LastTotalReplen = _persistentValue.TotalReplen;
  84. _persistentValue.IsDiReplenOn = false;
  85. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  86. }
  87. return result;
  88. }
  89. if (DateTime.Now.Subtract(_persistentValue.DiReplenTime).TotalSeconds >= replenSecond)
  90. {
  91. bool result = direplenOffAction("", null);
  92. if (result)
  93. {
  94. timeOutAction(true);
  95. _persistentValue.LastTotalReplen = _persistentValue.TotalReplen;
  96. _persistentValue.IsDiReplenOn = false;
  97. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  98. LOG.WriteLog(eEvent.INFO_RESERVOIR, _module, "Manual Direplen complete");
  99. }
  100. return result;
  101. }
  102. return false;
  103. }
  104. /// <summary>
  105. /// 自动注水超时
  106. /// </summary>
  107. /// <returns></returns>
  108. public (bool, DIReplenFaultType) AutoDiReplenMonitorTimeOut(Func<string, object[], bool> direplenOffAction, Action<bool> timeOutAction)
  109. {
  110. lock (_locker)
  111. {
  112. _persistentValue.TotalReplen = _persistentValue.LastTotalReplen + (int)DateTime.Now.Subtract(_persistentValue.DiReplenTime).TotalSeconds;
  113. }
  114. //单次自动补水超时
  115. double diValveMaxOnTimePerFill = SC.GetValue<double>($"Reservoir.{_module}.DIValveMaxOnTimePerFill");
  116. if (DateTime.Now.Subtract(_persistentValue.DiReplenTime).TotalSeconds >= diValveMaxOnTimePerFill * 60)
  117. {
  118. bool result = direplenOffAction("", null);
  119. if (result)
  120. {
  121. AlarmListManager.Instance.AddWarn(_module, $"", $"{_module} Direplen time over config DIValveMaxOnTimePerFill:{diValveMaxOnTimePerFill} min");
  122. LOG.WriteLog(eEvent.WARN_RESERVOIR, _module, $"Direplen time over conifg DIValveMaxOnTimePerFill:{diValveMaxOnTimePerFill} min");
  123. //补水超时关闭总的补水阀
  124. SystemFacilities systemFacilities = DEVICE.GetDevice<SystemFacilities>("System.Facilities");
  125. if (systemFacilities != null)
  126. {
  127. if (systemFacilities.DIReplenEnable) systemFacilities.DiReplenDisableOperation("DiReplenDisableOperation", null);
  128. }
  129. _persistentValue.LastTotalReplen = _persistentValue.TotalReplen;
  130. _persistentValue.IsDiReplenOn = false;
  131. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  132. }
  133. return (result, DIReplenFaultType.PerFillTimeOut);
  134. }
  135. //累计补水超时
  136. double diValveMaxOnTime = SC.GetValue<double>($"Reservoir.{_module}.DIValveMaxOnTime");
  137. if (_persistentValue.TotalReplen >= diValveMaxOnTime * 60)
  138. {
  139. bool result = direplenOffAction("", null);
  140. if (result)
  141. {
  142. timeOutAction(true);
  143. AlarmListManager.Instance.AddWarn(_module, $"", $"{_module} Direplen time over config DIValveMaxOnTime:{diValveMaxOnTime} min");
  144. LOG.WriteLog(eEvent.WARN_RESERVOIR, _module, $"Direplen time over conifg DIValveMaxOnTime:{diValveMaxOnTime} min");
  145. _persistentValue.LastTotalReplen = _persistentValue.TotalReplen;
  146. _persistentValue.IsDiReplenOn = false;
  147. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  148. }
  149. return (result, DIReplenFaultType.MaxTimeOut);
  150. }
  151. return (false, DIReplenFaultType.None);
  152. }
  153. /// <summary>
  154. /// 自动注水是否结束
  155. /// </summary>
  156. /// <param name="level"></param>
  157. /// <param name="recipeLevel"></param>
  158. /// <param name="direplenOffAction"></param>
  159. /// <returns></returns>
  160. public bool AutoDiReplenMonitorComplete(double level, double recipeLevel, bool replenEnable,
  161. int direplenTimeRate, int direplenCurrentRate, Func<string, object[], bool> direplenOffAction)
  162. {
  163. double levelHysteresis = SC.GetValue<double>("Reservoir.LevelHysteresis");
  164. //按液位补水
  165. if (replenEnable && direplenTimeRate == 0 && direplenCurrentRate == 0 &&
  166. level >= recipeLevel)
  167. {
  168. LOG.WriteLog(eEvent.INFO_RESERVOIR, _module, "Auto replen complete");
  169. bool result = direplenOffAction("", null);
  170. if (result)
  171. {
  172. _persistentValue.LastTotalReplen = _persistentValue.TotalReplen;
  173. _persistentValue.IsDiReplenOn = false;
  174. ReservoirsPersistentManager.Instance.UpdatePersistentValue(_module);
  175. return result;
  176. }
  177. }
  178. return false;
  179. }
  180. public enum DIReplenFaultType
  181. {
  182. None,
  183. MaxTimeOut,
  184. PerFillTimeOut
  185. }
  186. }
  187. }