IoGasValve.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.OperationCenter;
  9. using Aitex.Core.Util;
  10. using DocumentFormat.OpenXml.Wordprocessing;
  11. using Venus_Core;
  12. namespace Aitex.Core.RT.Device.Unit
  13. {
  14. public class IoValve : BaseDevice, IDevice
  15. {
  16. public string GVName { get { return Name; } }
  17. public string GVDeviceID { get { return DeviceID; } }
  18. public bool GVIsDefaultOpen { get { return _isDefaultOpen; } }
  19. [Subscription(AITValveDataPropertyName.SetPoint)]
  20. public bool SetPoint //True:open| False:close
  21. {
  22. get
  23. {
  24. return _isNc ? _doOpen.Value : !_doOpen.Value;
  25. }
  26. set
  27. {
  28. if (_doOpen != null)
  29. {
  30. _doOpen.Value = _isNc ? value : !value;
  31. }
  32. if (_doClose != null)
  33. {
  34. _doClose.Value = _isNc ? !value : value;
  35. }
  36. }
  37. }
  38. [Subscription(AITValveDataPropertyName.Status)]
  39. public bool Status //True:open | False:close
  40. {
  41. get
  42. {
  43. if (_diOpen != null)
  44. return _isNc ? _diOpen.Value : !_diOpen.Value;
  45. if (_doOpen != null)
  46. return _isNc ? _doOpen.Value : !_doOpen.Value;
  47. if (_doClose != null)
  48. return _isNc ? !_doClose.Value : _doClose.Value;
  49. return false;
  50. }
  51. }
  52. private AITValveData DeviceData
  53. {
  54. get
  55. {
  56. AITValveData data = new AITValveData()
  57. {
  58. UniqueName = _uniqueName,
  59. DeviceName = GVName,
  60. DefaultValue = GVIsDefaultOpen,
  61. DeviceSchematicId = DeviceID,
  62. DisplayName = Display,
  63. Feedback = Status,
  64. SetPoint = SetPoint,
  65. };
  66. return data;
  67. }
  68. }
  69. /// <summary>
  70. /// normal closed, 0 关闭,1打开
  71. /// </summary>
  72. public bool _isNc;
  73. /// <summary>
  74. /// default open
  75. /// </summary>
  76. public bool _isDefaultOpen;
  77. private DIAccessor _diOpenSensor;
  78. private DIAccessor _diCloseSensor;
  79. private DIAccessor _diOpen;
  80. private DOAccessor _doOpen;
  81. private DOAccessor _doClose;
  82. private bool _operation;
  83. private R_TRIG eventTrigger = new R_TRIG();
  84. private DeviceTimer _timer = new DeviceTimer();
  85. private string _uniqueName;
  86. private object _lockerOperation = new object();
  87. public IoValve(string module, XmlElement node, string ioModule = "")
  88. {
  89. var attrModule = node.GetAttribute("module");
  90. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  91. base.Name = node.GetAttribute("id");
  92. base.Display = node.GetAttribute("display");
  93. base.DeviceID = node.GetAttribute("schematicId");
  94. _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
  95. _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
  96. _diOpenSensor = ParseDiNode("diOpenSensor", node, ioModule);
  97. _diCloseSensor = ParseDiNode("diCloseSensor", node, ioModule);
  98. _doOpen = ParseDoNode("doOpen", node, ioModule);
  99. _diOpen = ParseDiNode("diOpen", node, ioModule);
  100. _doClose = ParseDoNode("doClose", node, ioModule);
  101. _uniqueName = $"{Module}.{Name}";
  102. }
  103. public bool Initialize()
  104. {
  105. DATA.Subscribe($"{Module}.{GVName}", () => DeviceData);
  106. DATA.Subscribe($"{Module}.{GVName}.IsOpen", () => Status,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  107. //DATA.Subscribe($"{_uniqueName}.DeviceData", () => DeviceData);
  108. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVTurnValve}", InvokeOpenCloseValve);
  109. DEVICE.Register($"{Module}.{Name}.{AITValveOperation.GVTurnValve}", (out string reason, int time, object[] param) =>
  110. {
  111. bool bOn = Convert.ToBoolean((string)param[0]);
  112. bool ret = TurnValve(bOn, out reason);
  113. if (ret)
  114. {
  115. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  116. return true;
  117. }
  118. return false;
  119. });
  120. //for recipe
  121. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  122. {
  123. bool bOn = Convert.ToBoolean((string)param[0]);
  124. bool ret = TurnValve(bOn, out reason);
  125. if (ret)
  126. {
  127. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  128. return true;
  129. }
  130. return false;
  131. });
  132. return true;
  133. }
  134. public void Terminate()
  135. {
  136. TurnValve(_isDefaultOpen, out string reason);
  137. }
  138. private bool InvokeOpenCloseValve(string method, object[] args)
  139. {
  140. bool op = (bool)args[0];
  141. string name = op ? "Open" : "Close";
  142. if (!TurnValve(op, out string reason))
  143. {
  144. LOG.Write(eEvent.WARN_IO_VALVE, Module, $"Can not {name} valve {Module}.{Name}, {reason}");
  145. return false;
  146. }
  147. //LOG.Write(eEvent.INFO_IO_VALVE, Module, $"{name} valve {Module}.{Name}");
  148. return true;
  149. }
  150. public void Monitor()
  151. {
  152. try
  153. {
  154. lock (_lockerOperation)
  155. {
  156. if (_timer.IsTimeout())
  157. {
  158. _timer.Stop();
  159. if (Status != _operation)
  160. {
  161. if (_operation)
  162. {
  163. LOG.Write(eEvent.ERR_IO_VALVE, Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  164. $"{Display} open: valve keep closed" :
  165. $"{Display} Fail to open due to interlock {reason}");
  166. }
  167. else
  168. {
  169. LOG.Write(eEvent.ERR_IO_VALVE, Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  170. $"{Display} Close : Valve keep open" :
  171. $"{Display} Close : Failed for interlock {reason}");
  172. }
  173. }
  174. _operation = SetPoint;
  175. }
  176. else if (_timer.IsIdle())
  177. {
  178. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  179. if (eventTrigger.Q)
  180. {
  181. if (_operation)
  182. {
  183. LOG.Write(eEvent.ERR_IO_VALVE, Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  184. $"Valve {Display} was Close,Reason PLC kept" :
  185. $"Valve {Display} was Close,Reason {reason}");
  186. }
  187. else
  188. {
  189. LOG.Write(eEvent.ERR_IO_VALVE, Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  190. $"Valve {Display} was Open,Reason PLC Kept" :
  191. $"Valve {Display} was Open,Reason:{reason}");
  192. }
  193. _operation = SetPoint;
  194. }
  195. }
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. LOG.WriteExeption(ex);
  201. }
  202. }
  203. public bool TurnValve(bool bOperation, out string reason)
  204. {
  205. lock (_lockerOperation)
  206. {
  207. bool bValue = _isNc ? bOperation : !bOperation;
  208. reason = "";
  209. SetPoint = bValue;
  210. if (Name == "ValveN2")
  211. {
  212. reason = Name;
  213. }
  214. //LOG.Write(eEvent.INFO_IO_VALVE, Module, $"{(bOperation ? "Open" : "Close")} Valve {Name}");
  215. _operation = bOperation;
  216. _timer.Start(2000); //2 seconds to monitor
  217. }
  218. return true;
  219. }
  220. public void Reset()
  221. {
  222. eventTrigger.RST = true;
  223. }
  224. }
  225. }