IoGasValve.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. public IoValve(string module, XmlElement node, string ioModule = "")
  85. {
  86. var attrModule = node.GetAttribute("module");
  87. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  88. base.Name = node.GetAttribute("id");
  89. base.Display = node.GetAttribute("display");
  90. base.DeviceID = node.GetAttribute("schematicId");
  91. _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
  92. _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
  93. _diOpenSensor = ParseDiNode("diOpenSensor", node, ioModule);
  94. _diCloseSensor = ParseDiNode("diCloseSensor", node, ioModule);
  95. _doOpen = ParseDoNode("doOpen", node, ioModule);
  96. _diOpen = ParseDiNode("diOpen", node, ioModule);
  97. _doClose = ParseDoNode("doClose", node, ioModule);
  98. _uniqueName = $"{Module}.{Name}";
  99. }
  100. public bool Initialize()
  101. {
  102. DATA.Subscribe($"{Module}.{GVName}", () => DeviceData);
  103. //DATA.Subscribe($"{_uniqueName}.DeviceData", () => DeviceData);
  104. OP.Subscribe($"{_uniqueName}.{AITValveOperation.GVTurnValve}", InvokeOpenCloseValve);
  105. DEVICE.Register($"{Module}.{Name}.{AITValveOperation.GVTurnValve}", (out string reason, int time, object[] param) =>
  106. {
  107. bool bOn = Convert.ToBoolean((string)param[0]);
  108. bool ret = TurnValve(bOn, out reason);
  109. if (ret)
  110. {
  111. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  112. return true;
  113. }
  114. return false;
  115. });
  116. //for recipe
  117. DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>
  118. {
  119. bool bOn = Convert.ToBoolean((string)param[0]);
  120. bool ret = TurnValve(bOn, out reason);
  121. if (ret)
  122. {
  123. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  124. return true;
  125. }
  126. return false;
  127. });
  128. return true;
  129. }
  130. public void Terminate()
  131. {
  132. TurnValve(_isDefaultOpen, out string reason);
  133. }
  134. private bool InvokeOpenCloseValve(string method, object[] args)
  135. {
  136. bool op = (bool)args[0];
  137. string name = op ? "Open" : "Close";
  138. if (!TurnValve(op, out string reason))
  139. {
  140. EV.PostWarningLog(Module, $"Can not {name} valve {Module}.{Name}, {reason}");
  141. return false;
  142. }
  143. EV.PostInfoLog(Module, $"{name} valve {Module}.{Name}");
  144. return true;
  145. }
  146. public void Monitor()
  147. {
  148. try
  149. {
  150. if (_timer.IsTimeout())
  151. {
  152. _timer.Stop();
  153. if (Status != _operation)
  154. {
  155. if (_operation)
  156. {
  157. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  158. $"{Display} open: valve keep closed" :
  159. $"{Display} Fail to open due to interlock {reason}");
  160. }
  161. else
  162. {
  163. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  164. $"{Display} Close : Valve keep open" :
  165. $"{Display} Close : Failed for interlock {reason}");
  166. }
  167. }
  168. _operation = SetPoint;
  169. }
  170. else if (_timer.IsIdle())
  171. {
  172. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  173. if (eventTrigger.Q)
  174. {
  175. if (_operation)
  176. {
  177. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  178. $"Valve {Display} was Close,Reason PLC kept" :
  179. $"Valve {Display} was Close,Reason {reason}");
  180. }
  181. else
  182. {
  183. EV.PostAlarmLog(Module, _doOpen.Check(_isNc ? true : false, out string reason) ?
  184. $"Valve {Display} was Open,Reason PLC Kept" :
  185. $"Valve {Display} was Open,Reason:{reason}");
  186. }
  187. _operation = SetPoint;
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. LOG.Write(ex);
  194. throw ex;
  195. }
  196. }
  197. public bool TurnValve(bool bOperation, out string reason)
  198. {
  199. bool bValue = _isNc ? bOperation : !bOperation;
  200. reason = "";
  201. SetPoint = bValue;
  202. _operation = bOperation;
  203. _timer.Start(2000); //2 seconds to monitor
  204. return true;
  205. }
  206. public void Reset()
  207. {
  208. eventTrigger.RST = true;
  209. }
  210. }
  211. }