TotalReservoirDevice.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #endregion
  48. #region 属性
  49. /// <summary>
  50. /// Di Replen Flow
  51. /// </summary>
  52. private CounterFlowData DiReplenFlow { get { return _diReplenFlow; } }
  53. /// <summary>
  54. /// High Level
  55. /// </summary>
  56. public bool HighLevel { get { return _highLevel; } }
  57. /// <summary>
  58. /// Evaporator Level
  59. /// </summary>
  60. public bool EvaporatorLevel { get { return _evaporatorLevel; } }
  61. #endregion
  62. /// <summary>
  63. /// 构造函数
  64. /// </summary>
  65. /// <param name="moduleName"></param>
  66. /// <param name="name"></param>
  67. public TotalReservoirDevice() : base("Reservoir", "Reservoir", "Reservoir", "Reservoir")
  68. {
  69. _period = new PeriodicJob(500, OnTimer, "Reservoir.OnTimer", true, true);
  70. }
  71. /// <summary>
  72. /// 初始化
  73. /// </summary>
  74. /// <returns></returns>
  75. public bool Initialize()
  76. {
  77. InitializeParameter();
  78. SubscribeValueAction();
  79. InitializeOperation();
  80. return true;
  81. }
  82. /// <summary>
  83. /// 加载参数
  84. /// </summary>
  85. private void InitializeParameter()
  86. {
  87. }
  88. /// <summary>
  89. /// 初始化操作
  90. /// </summary>
  91. private void InitializeOperation()
  92. {
  93. }
  94. /// <summary>
  95. /// 订阅变量数值发生变化
  96. /// </summary>
  97. private void SubscribeValueAction()
  98. {
  99. BeckhoffCounterSubscribeUpdateVariable(DIREPLEN_FLOW, DiReplenFlow);
  100. BeckhoffIoSubscribeUpdateVariable(EVAPORATOR_LEVEL);
  101. BeckhoffIoSubscribeUpdateVariable(HIGH_LEVEL);
  102. }
  103. /// <summary>
  104. /// 订阅Counter变量
  105. /// </summary>
  106. /// <param name="variable"></param>
  107. private void BeckhoffCounterSubscribeUpdateVariable(string variable, CounterFlowData counterFlowData)
  108. {
  109. _nameCounterFlowData[$"{Module}.{variable}"] = counterFlowData;
  110. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_VALUE, UpdateCounterVariableValue);
  111. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_START, UpdateCounterVariableValue);
  112. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_STOP, UpdateCounterVariableValue);
  113. BeckhoffCounterManager.Instance.SubscribeModuleVariable($"{Module}.{variable}", COUNTER_RESET, UpdateCounterVariableValue);
  114. }
  115. /// <summary>
  116. /// 订阅IO变量
  117. /// </summary>
  118. /// <param name="variable"></param>
  119. private void BeckhoffIoSubscribeUpdateVariable(string variable)
  120. {
  121. IOModuleManager.Instance.SubscribeModuleVariable(Module, variable, UpdateIOVariableValue);
  122. }
  123. /// <summary>
  124. /// 更新变量数值
  125. /// </summary>
  126. /// <param name="variable"></param>
  127. /// <param name="value"></param>
  128. private void UpdateIOVariableValue(string variable, object value)
  129. {
  130. if (variable == HIGH_LEVEL)
  131. {
  132. _highLevel = (bool)value;
  133. }
  134. else if (variable == EVAPORATOR_LEVEL)
  135. {
  136. _evaporatorLevel = (bool)value;
  137. }
  138. }
  139. /// <summary>
  140. /// 更新变量数值
  141. /// </summary>
  142. /// <param name="variable"></param>
  143. /// <param name="value"></param>
  144. private void UpdateCounterVariableValue(string variable, object value)
  145. {
  146. string[] strAry = variable.Split('.');
  147. string lastVariable = strAry[strAry.Length - 1];
  148. PropertyInfo property = null;
  149. string key = variable.Replace($".{lastVariable}", "");
  150. if (_nameCounterFlowData.ContainsKey(key))
  151. {
  152. CounterFlowData counterFlowData = _nameCounterFlowData[key];
  153. property = counterFlowData.GetType().GetProperty(lastVariable);
  154. if (property != null)
  155. {
  156. property.SetValue(counterFlowData, value);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 定时器
  162. /// </summary>
  163. /// <returns></returns>
  164. private bool OnTimer()
  165. {
  166. List<string> reservoirs = ReservoirItemManager.Instance.InstalledModules;
  167. foreach (string module in reservoirs)
  168. {
  169. ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(module);
  170. ReservoirEntity reservoirEntity = Singleton<RouteManager>.Instance.GetModule<ReservoirEntity>(module);
  171. if (reservoirEntity != null && !reservoirEntity.IsError)
  172. {
  173. if (reservoirItem.SubType == STRATUS)
  174. {
  175. StandardHotReservoirDevice reservoirDevice = DEVICE.GetDevice<StandardHotReservoirDevice>(module);
  176. if (!reservoirEntity.IsInitialized || reservoirDevice.IsDIReplenPerfillTimeOut
  177. || reservoirDevice.IsDireplenOn || reservoirDevice.IsDIReplenMaxTimeOut)
  178. {
  179. continue;
  180. }
  181. if (reservoirDevice.NeedAutoDireplen && !reservoirDevice.IsDireplenOn)
  182. {
  183. reservoirDevice.AutoDireplen();
  184. }
  185. }
  186. }
  187. else
  188. {
  189. continue;
  190. }
  191. }
  192. return true;
  193. }
  194. public void Monitor()
  195. {
  196. }
  197. public void Reset()
  198. {
  199. }
  200. public void Terminate()
  201. {
  202. _period.Stop(false);
  203. }
  204. }
  205. }