IoValve.cs 10 KB

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