IoGasValve.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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($"{_uniqueName}.DeviceData", () => DeviceData);
  105. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVTurnValve}", InvokeOpenCloseValve);
  106. DEVICE.Register($"{Module}.{Name}.{AITValveOperation.GVTurnValve}", (out string reason, int time, object[] param) =>
  107. {
  108. bool bOn = Convert.ToBoolean((string)param[0]);
  109. bool ret = TurnValve(bOn, out reason);
  110. if (ret)
  111. {
  112. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  113. return true;
  114. }
  115. return false;
  116. });
  117. //for recipe
  118. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  119. {
  120. bool bOn = Convert.ToBoolean((string)param[0]);
  121. bool ret = TurnValve(bOn, out reason);
  122. if (ret)
  123. {
  124. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  125. return true;
  126. }
  127. return false;
  128. });
  129. return true;
  130. }
  131. public void Terminate()
  132. {
  133. TurnValve(_isDefaultOpen, out string reason);
  134. }
  135. private bool InvokeOpenCloseValve(string method, object[] args)
  136. {
  137. bool op = (bool)args[0];
  138. string name = op ? "Open" : "Close";
  139. if (!TurnValve(op, out string reason))
  140. {
  141. EV.PostWarningLog(Module, $"Can not {name} valve {Module}.{Name}, {reason}");
  142. return false;
  143. }
  144. EV.PostInfoLog(Module, $"{name} valve {Module}.{Name}");
  145. return true;
  146. }
  147. public void Monitor()
  148. {
  149. try
  150. {
  151. lock (_lockerOperation)
  152. {
  153. if (_timer.IsTimeout())
  154. {
  155. _timer.Stop();
  156. if (Status != _operation)
  157. {
  158. if (_operation)
  159. {
  160. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  161. $"{Display} open: valve keep closed" :
  162. $"{Display} Fail to open due to interlock {reason}");
  163. }
  164. else
  165. {
  166. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  167. $"{Display} Close : Valve keep open" :
  168. $"{Display} Close : Failed for interlock {reason}");
  169. }
  170. }
  171. _operation = SetPoint;
  172. }
  173. else if (_timer.IsIdle())
  174. {
  175. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  176. if (eventTrigger.Q)
  177. {
  178. if (_operation)
  179. {
  180. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  181. $"Valve {Display} was Close,Reason PLC kept" :
  182. $"Valve {Display} was Close,Reason {reason}");
  183. }
  184. else
  185. {
  186. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  187. $"Valve {Display} was Open,Reason PLC Kept" :
  188. $"Valve {Display} was Open,Reason:{reason}");
  189. }
  190. _operation = SetPoint;
  191. }
  192. }
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. LOG.Write(ex);
  198. }
  199. }
  200. public bool TurnValve(bool bOperation, out string reason)
  201. {
  202. lock (_lockerOperation)
  203. {
  204. bool bValue = _isNc ? bOperation : !bOperation;
  205. reason = "";
  206. SetPoint = bValue;
  207. _operation = bOperation;
  208. _timer.Start(2000); //2 seconds to monitor
  209. }
  210. return true;
  211. }
  212. public void Reset()
  213. {
  214. eventTrigger.RST = true;
  215. }
  216. }
  217. }