IoGasValve.cs 8.7 KB

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