IoPump2.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Diagnostics;
  3. using System.Xml;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.Event;
  14. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps;
  15. namespace Aitex.Core.RT.Device.Unit
  16. {
  17. /// <summary>
  18. /// 泵2对应的IO要求:
  19. /// diRunning 【如果未定义,默认泵为常开】
  20. /// diOverloadAlarm【可以不定义】
  21. ///
  22. /// doResetError【可以不定义】
  23. /// doStartStop【可以不定义】
  24. /// doPowerOn【可以不定义,默认主电源常供】
  25. /// </summary>
  26. public class IoPump2 : BaseDevice, IDevice, IPump
  27. {
  28. public bool IsRunning
  29. {
  30. get
  31. {
  32. if (_diRunning != null)
  33. return _diRunning.Value;
  34. return true;
  35. }
  36. }
  37. public bool IsPumpOverloadAlarm
  38. {
  39. get
  40. {
  41. return _diOverloadAlarm != null && _diOverloadAlarm.Value;
  42. }
  43. }
  44. public bool ResetErrorSetPoint
  45. {
  46. get
  47. {
  48. return _doResetError != null && _doResetError.Value;
  49. }
  50. set
  51. {
  52. if (_doResetError!=null)
  53. _doResetError.Value = value;
  54. }
  55. }
  56. public bool StartSetPoint
  57. {
  58. get
  59. {
  60. return _doStart != null && _doStart.Value;
  61. }
  62. set
  63. {
  64. if (_doStart != null)
  65. _doStart.Value = value;
  66. }
  67. }
  68. public bool MainPowerOnSetPoint
  69. {
  70. get
  71. {
  72. return _doPowerOn != null && _doPowerOn.Value;
  73. }
  74. set
  75. {
  76. if (_doPowerOn != null)
  77. _doPowerOn.Value = value;
  78. }
  79. }
  80. private AITPumpData DeviceData
  81. {
  82. get
  83. {
  84. AITPumpData data = new AITPumpData()
  85. {
  86. DeviceName = Name,
  87. DeviceSchematicId = DeviceID,
  88. DisplayName = Display,
  89. DeviceModule = Module,
  90. Module = Module,
  91. IsOn = IsRunning,
  92. IsError = HasAlarm,
  93. IsOverLoad = IsPumpOverloadAlarm,
  94. };
  95. return data;
  96. }
  97. }
  98. private DIAccessor _diRunning = null;
  99. private DIAccessor _diOverloadAlarm;
  100. private DOAccessor _doStart;
  101. private DOAccessor _doPowerOn;
  102. private DOAccessor _doResetError;
  103. private R_TRIG _trigOverload = new R_TRIG();
  104. public AlarmEventItem AlarmOverload { get; set; }
  105. public AlarmEventItem AlarmFailedStartStop { get; set; }
  106. private Stopwatch _timerResetError = new Stopwatch();
  107. private Stopwatch _timerStartStop = new Stopwatch();
  108. private SCConfigItem _scStartTimeout;
  109. private SCConfigItem _scResetErrorTimeout;
  110. public IoPump2(string module, XmlElement node, string ioModule = "")
  111. {
  112. var attrModule = node.GetAttribute("module");
  113. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  114. base.Name = node.GetAttribute("id");
  115. base.Display = node.GetAttribute("display");
  116. base.DeviceID = node.GetAttribute("schematicId");
  117. _diRunning = ParseDiNode("diRunning", node, ioModule);
  118. _diOverloadAlarm = ParseDiNode("diOverloadAlarm", node, ioModule);
  119. _doStart = ParseDoNode("doStartStop", node, ioModule);
  120. _doPowerOn = ParseDoNode("doPowerOn", node, ioModule);
  121. _doResetError = ParseDoNode("doReset", node, ioModule);
  122. string scBasePath = node.GetAttribute("scBasePath");
  123. if (string.IsNullOrEmpty(scBasePath))
  124. scBasePath = $"{Module}.{Name}";
  125. else
  126. {
  127. scBasePath = scBasePath.Replace("{module}", Module);
  128. }
  129. _scStartTimeout = ParseScNode("", node, ioModule, $"{scBasePath}.{Name}.StartTimeout");
  130. _scResetErrorTimeout = ParseScNode("", node, ioModule, $"{scBasePath}.{Name}.ResetErrorTimeout");
  131. System.Diagnostics.Debug.Assert(_scResetErrorTimeout != null, "SC not defined");
  132. System.Diagnostics.Debug.Assert(_scStartTimeout != null, "SC not defined");
  133. }
  134. public bool Initialize()
  135. {
  136. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  137. DATA.Subscribe($"{Module}.{Name}.IsOverload", () => IsPumpOverloadAlarm);
  138. DATA.Subscribe($"{Module}.{Name}.IsRunning", () => IsRunning);
  139. DATA.Subscribe($"{Module}.{Name}.StartSetPoint", () => StartSetPoint);
  140. OP.Subscribe($"{Module}.{Name}.{AITPumpOperation.SetOnOff}" , SetPumpOnOff);
  141. OP.Subscribe($"{Module}.{Name}.{AITPumpOperation.PumpOn}", SetPumpOn);
  142. OP.Subscribe($"{Module}.{Name}.{AITPumpOperation.PumpOff}", SetPumpOff);
  143. AlarmOverload = SubscribeAlarm($"{Module}.{Name}.OverloadAlarm", "", ResetOverload);
  144. AlarmFailedStartStop = SubscribeAlarm($"{Module}.{Name}.FailedStartStopAlarm", "", null);
  145. return true;
  146. }
  147. private bool SetPumpOn(out string reason, int time, object[] param)
  148. {
  149. return SetPump(out reason, time, true);
  150. }
  151. private bool SetPumpOff(out string reason, int time, object[] param)
  152. {
  153. return SetPump(out reason, time, false);
  154. }
  155. private bool SetPumpOnOff(out string reason, int time, object[] param)
  156. {
  157. return SetPump(out reason, time, Convert.ToBoolean((string)param[0]));
  158. }
  159. public bool SetMainPowerOnOff(bool isOn, out string reason)
  160. {
  161. MainPowerOnSetPoint = isOn;
  162. reason = string.Empty;
  163. return true;
  164. }
  165. public bool ResetOverload()
  166. {
  167. if (IsPumpOverloadAlarm)
  168. {
  169. if (!ResetErrorSetPoint)
  170. {
  171. _timerResetError.Restart();
  172. ResetErrorSetPoint = true;
  173. }
  174. }
  175. return !IsPumpOverloadAlarm;
  176. }
  177. public bool SetPump(out string reason, int time, bool isOn)
  178. {
  179. if (HasAlarm)
  180. {
  181. reason = $"{Display} Has active alarm, reset error first.";
  182. return false;
  183. }
  184. reason = string.Empty;
  185. _timerStartStop.Restart();
  186. StartSetPoint = isOn;
  187. return true;
  188. }
  189. public void Terminate()
  190. {
  191. }
  192. public void Monitor()
  193. {
  194. try
  195. {
  196. if (StartSetPoint != IsRunning)
  197. {
  198. if (_timerStartStop.IsRunning &&
  199. _timerStartStop.ElapsedMilliseconds > _scStartTimeout.IntValue * 1000)
  200. {
  201. var onoff = StartSetPoint ? "start up" : "shut down";
  202. _timerStartStop.Stop();
  203. AlarmFailedStartStop.Description =
  204. $"{Display} can not {onoff} in {_scStartTimeout.IntValue} seconds";
  205. AlarmFailedStartStop.Set();
  206. StartSetPoint = IsRunning;
  207. }
  208. }
  209. _trigOverload.CLK = IsPumpOverloadAlarm;
  210. if (_trigOverload.Q)
  211. {
  212. AlarmOverload.Set("Pump Overload or Error");
  213. StartSetPoint = false;
  214. }
  215. if (ResetErrorSetPoint)
  216. {
  217. if (!IsPumpOverloadAlarm)
  218. {
  219. AlarmOverload.Reset();
  220. ResetErrorSetPoint = false;
  221. _timerResetError.Stop();
  222. }
  223. if (IsPumpOverloadAlarm && _timerResetError.IsRunning &&
  224. _timerResetError.ElapsedMilliseconds > _scResetErrorTimeout.IntValue * 1000)
  225. {
  226. _timerResetError.Stop();
  227. EV.PostWarningLog(Module, $"Can not reset {Display} error in {_scResetErrorTimeout.IntValue} seconds");
  228. ResetErrorSetPoint = false;
  229. }
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. LOG.Write(ex);
  235. }
  236. }
  237. public void Reset()
  238. {
  239. AlarmFailedStartStop.Reset();
  240. AlarmOverload.Reset();
  241. }
  242. }
  243. }