TotalReservoirDevice.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using CyberX8_RT.Modules;
  5. using CyberX8_RT.Modules.Reservoir;
  6. using MECF.Framework.Common.CommonData;
  7. using MECF.Framework.Common.IOCore;
  8. using MECF.Framework.Common.ToolLayout;
  9. using MECF.Framework.Common.TwinCat;
  10. using System.Collections.Generic;
  11. using System.Reflection;
  12. namespace CyberX8_RT.Devices.Reservoir
  13. {
  14. public class TotalReservoirDevice : BaseDevice, IDevice
  15. {
  16. #region 常量
  17. private const string DIREPLEN_FLOW = "DiReplenFlow";
  18. private const string COUNTER_VALUE = "CounterValue";
  19. private const string COUNTER_START = "Start";
  20. private const string COUNTER_STOP = "Stop";
  21. private const string COUNTER_RESET = "Reset";
  22. private const string EVAPORATOR_LEVEL = "EvaporatorLevel";
  23. private const string HIGH_LEVEL = "HighLevel";
  24. private const string STRATUS = "Stratus";
  25. #endregion
  26. #region 内部变量
  27. /// <summary>
  28. /// Di Replen Flow
  29. /// </summary>
  30. private CounterFlowData _diReplenFlow = new CounterFlowData();
  31. /// <summary>
  32. /// High Level
  33. /// </summary>
  34. private bool _highLevel;
  35. /// <summary>
  36. /// Evaporator Level
  37. /// </summary>
  38. private bool _evaporatorLevel;
  39. /// <summary>
  40. /// Counter字典
  41. /// </summary>
  42. private Dictionary<string, CounterFlowData> _nameCounterFlowData = new Dictionary<string, CounterFlowData>();
  43. /// <summary>
  44. /// 定时器
  45. /// </summary>
  46. private PeriodicJob _period;
  47. /// <summary>
  48. /// DiReplen Warning Dictionary
  49. /// </summary>
  50. private Dictionary<string, bool> _diReplenWarnDic = new Dictionary<string, bool>() {
  51. {"Reservoir1", false},
  52. {"Reservoir2", false},
  53. {"Reservoir3", false},
  54. {"Reservoir4", false},
  55. };
  56. #endregion
  57. #region 属性
  58. /// <summary>
  59. /// Di Replen Flow
  60. /// </summary>
  61. private CounterFlowData DiReplenFlow { get { return _diReplenFlow; } }
  62. /// <summary>
  63. /// High Level
  64. /// </summary>
  65. public bool HighLevel { get { return _highLevel; } }
  66. /// <summary>
  67. /// Evaporator Level
  68. /// </summary>
  69. public bool EvaporatorLevel { get { return _evaporatorLevel; } }
  70. #endregion
  71. /// <summary>
  72. /// 构造函数
  73. /// </summary>
  74. /// <param name="moduleName"></param>
  75. /// <param name="name"></param>
  76. public TotalReservoirDevice() : base("Reservoir", "Reservoir", "Reservoir", "Reservoir")
  77. {
  78. _period = new PeriodicJob(500, OnTimer, "Reservoir.OnTimer", true, true);
  79. }
  80. /// <summary>
  81. /// 初始化
  82. /// </summary>
  83. /// <returns></returns>
  84. public bool Initialize()
  85. {
  86. InitializeParameter();
  87. SubscribeValueAction();
  88. InitializeOperation();
  89. return true;
  90. }
  91. /// <summary>
  92. /// 加载参数
  93. /// </summary>
  94. private void InitializeParameter()
  95. {
  96. }
  97. /// <summary>
  98. /// 初始化操作
  99. /// </summary>
  100. private void InitializeOperation()
  101. {
  102. }
  103. /// <summary>
  104. /// 订阅变量数值发生变化
  105. /// </summary>
  106. private void SubscribeValueAction()
  107. {
  108. BeckhoffCounterSubscribeUpdateVariable(DIREPLEN_FLOW, DiReplenFlow);
  109. BeckhoffIoSubscribeUpdateVariable(EVAPORATOR_LEVEL);
  110. BeckhoffIoSubscribeUpdateVariable(HIGH_LEVEL);
  111. }
  112. /// <summary>
  113. /// 订阅Counter变量
  114. /// </summary>
  115. /// <param name="variable"></param>
  116. private void BeckhoffCounterSubscribeUpdateVariable(string variable, CounterFlowData counterFlowData)
  117. {
  118. _nameCounterFlowData[$"{Module}.{variable}"] = counterFlowData;
  119. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_VALUE, UpdateCounterVariableValue);
  120. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_START, UpdateCounterVariableValue);
  121. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_STOP, UpdateCounterVariableValue);
  122. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_RESET, UpdateCounterVariableValue);
  123. }
  124. /// <summary>
  125. /// 订阅IO变量
  126. /// </summary>
  127. /// <param name="variable"></param>
  128. private void BeckhoffIoSubscribeUpdateVariable(string variable)
  129. {
  130. IOModuleManager.Instance.SubscribeModuleVariable(Module, variable, UpdateIOVariableValue);
  131. }
  132. /// <summary>
  133. /// 更新变量数值
  134. /// </summary>
  135. /// <param name="variable"></param>
  136. /// <param name="value"></param>
  137. private void UpdateIOVariableValue(string variable, object value)
  138. {
  139. if (variable == HIGH_LEVEL)
  140. {
  141. _highLevel = (bool)value;
  142. }
  143. else if (variable == EVAPORATOR_LEVEL)
  144. {
  145. _evaporatorLevel = (bool)value;
  146. }
  147. }
  148. /// <summary>
  149. /// 更新变量数值
  150. /// </summary>
  151. /// <param name="variable"></param>
  152. /// <param name="value"></param>
  153. private void UpdateCounterVariableValue(string variable, object value)
  154. {
  155. string[] strAry = variable.Split('.');
  156. string lastVariable = strAry[strAry.Length - 1];
  157. PropertyInfo property = null;
  158. string key = variable.Replace($".{lastVariable}", "");
  159. if (_nameCounterFlowData.ContainsKey(key))
  160. {
  161. CounterFlowData counterFlowData = _nameCounterFlowData[key];
  162. property = counterFlowData.GetType().GetProperty(lastVariable);
  163. if (property != null)
  164. {
  165. property.SetValue(counterFlowData, value);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 定时器
  171. /// </summary>
  172. /// <returns></returns>
  173. private bool OnTimer()
  174. {
  175. List<string> reservoirs = ReservoirItemManager.Instance.InstalledModules;
  176. foreach (string module in reservoirs)
  177. {
  178. ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(module);
  179. ReservoirEntity reservoirEntity = Singleton<RouteManager>.Instance.GetModule<ReservoirEntity>(module);
  180. if (reservoirEntity != null && !reservoirEntity.IsError)
  181. {
  182. if (reservoirItem.SubType == STRATUS)
  183. {
  184. StandardHotReservoirDevice reservoirDevice = DEVICE.GetDevice<StandardHotReservoirDevice>(module);
  185. if (!reservoirEntity.IsInitialized || reservoirDevice.DiReplenMaxTimeOut
  186. || reservoirDevice.IsDireplenOn || reservoirDevice.IsDiReplenInFault)
  187. {
  188. continue;
  189. }
  190. if (reservoirDevice.NeedAutoDireplen)
  191. {
  192. if (!CheckFacilityDiReplenOn(reservoirDevice.TotalDIReplenOn, module))
  193. {
  194. continue;
  195. }
  196. reservoirDevice.AutoDireplen();
  197. }
  198. }
  199. }
  200. else
  201. {
  202. continue;
  203. }
  204. }
  205. return true;
  206. }
  207. private bool CheckFacilityDiReplenOn(bool diReplenOn, string module)
  208. {
  209. if (!diReplenOn)
  210. {
  211. if (!_diReplenWarnDic[module])
  212. {
  213. _diReplenWarnDic[module] = true;
  214. LOG.WriteLog(eEvent.WARN_RESERVOIR, module, $"SystemFacility DIReplen is off, {module} can't do auto DIReplen");
  215. }
  216. return false;
  217. }
  218. else
  219. {
  220. _diReplenWarnDic[module] = false;
  221. return true;
  222. }
  223. }
  224. public void Monitor()
  225. {
  226. }
  227. public void Reset()
  228. {
  229. }
  230. public void Terminate()
  231. {
  232. _period.Stop(false);
  233. }
  234. }
  235. }