IoGasValve.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.UI.Control;
  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. }
  25. [Subscription(AITValveDataPropertyName.Status)]
  26. public bool Status //True:open | False:close
  27. {
  28. get
  29. {
  30. if (_diOpen != null)
  31. return _isNc ? _diOpen.Value : !_diOpen.Value;
  32. return _isNc ? _doOpen.Value : !_doOpen.Value;
  33. }
  34. }
  35. /// <summary>
  36. /// normal closed, 0 关闭,1打开
  37. /// </summary>
  38. public bool _isNc;
  39. /// <summary>
  40. /// default open
  41. /// </summary>
  42. public bool _isDefaultOpen;
  43. private DIAccessor _diOpenSensor;
  44. private DIAccessor _diCloseSensor;
  45. private DIAccessor _diOpen;
  46. private DOAccessor _doOpen;
  47. private bool _operation;
  48. private R_TRIG eventTrigger = new R_TRIG();
  49. DeviceTimer _timer = new DeviceTimer();
  50. public IoValve(string module, XmlElement node)
  51. {
  52. base.Module = module;
  53. base.Name = node.GetAttribute("id");
  54. base.Display = node.GetAttribute("display");
  55. base.DeviceID = node.GetAttribute("schematicId");
  56. _isNc = Convert.ToBoolean(node.GetAttribute("isNc"));
  57. _isDefaultOpen = Convert.ToBoolean(node.GetAttribute("isDefaultOpen"));
  58. _diOpenSensor = ParseDiNode("diOpenSensor", node);
  59. _diCloseSensor = ParseDiNode("diCloseSensor", node);
  60. _doOpen = ParseDoNode("doOpen", node);
  61. _diOpen = ParseDiNode("diOpen", node);
  62. }
  63. public bool Initialize()
  64. {
  65. DATA.Subscribe(string.Format("Device.{0}.{1}", Module , GVName), () =>
  66. {
  67. AITValveData data = new AITValveData()
  68. {
  69. DeviceName = GVName,
  70. DefaultValue = GVIsDefaultOpen,
  71. DeviceSchematicId = DeviceID,
  72. DisplayName = Display,
  73. Feedback = Status,
  74. SetPoint = SetPoint,
  75. };
  76. return data;
  77. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  78. DEVICE.Register(String.Format("{0}.{1}", Name, AITValveOperation.GVTurnValve), (out string reason, int time, object[] param) =>
  79. {
  80. bool bOn = Convert.ToBoolean((string)param[0]);
  81. bool ret = TurnValve(bOn, out reason);
  82. if (ret)
  83. {
  84. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  85. return true;
  86. }
  87. return false;
  88. });
  89. //for recipe
  90. DEVICE.Register(String.Format("{0}", Name), (out string reason, int time, object[] param) =>
  91. {
  92. bool bOn = Convert.ToBoolean((string)param[0]);
  93. bool ret = TurnValve(bOn, out reason);
  94. if (ret)
  95. {
  96. reason = string.Format("Valve {0}{1}", Name, bOn ? "Open" : "Close");
  97. return true;
  98. }
  99. return false;
  100. });
  101. return true;
  102. }
  103. public void Terminate()
  104. {
  105. string reason;
  106. TurnValve(_isDefaultOpen, out reason);
  107. }
  108. public void Monitor()
  109. {
  110. try
  111. {
  112. if (_timer.IsTimeout())
  113. {
  114. _timer.Stop();
  115. if (Status != _operation)
  116. {
  117. if (_operation)
  118. {
  119. string reason;
  120. if (!_doOpen.Check(_isNc ? true : false, out reason))
  121. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Failed for interlock " + reason);
  122. else
  123. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Open", ":Valve keep closed ");
  124. }
  125. else
  126. {
  127. string reason;
  128. if (!_doOpen.Check(_isNc ? true : false, out reason))
  129. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Failed for interlock " + reason);
  130. else
  131. EV.PostMessage(Module, EventEnum.ValveOperationFail, Module, Display, "Close", ":Valve keep open");
  132. }
  133. }
  134. _operation = SetPoint;
  135. }
  136. else if (_timer.IsIdle())
  137. {
  138. eventTrigger.CLK = SetPoint != _operation; // fire event only check at first, SetPoint set by interlock
  139. if (eventTrigger.Q)
  140. {
  141. if (_operation)
  142. {
  143. string reason;
  144. if (!_doOpen.Check(_isNc ? true : false, out reason))
  145. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Close", reason));
  146. else
  147. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
  148. }
  149. else
  150. {
  151. string reason;
  152. if (!_doOpen.Check(_isNc ? true : false, out reason))
  153. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason:{2}", Display, "Open", reason));
  154. else
  155. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Valve {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
  156. }
  157. _operation = SetPoint;
  158. }
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. LOG.Write(ex);
  164. throw ex;
  165. }
  166. }
  167. public bool TurnValve(bool bOperation, out string reason)
  168. {
  169. bool bValue = _isNc ? bOperation : !bOperation;
  170. if (!_doOpen.SetValue(bValue, out reason))
  171. return false;
  172. _operation = bOperation;
  173. _timer.Start(2000); //2 seconds to monitor
  174. return true;
  175. }
  176. public void Reset()
  177. {
  178. eventTrigger.RST = true;
  179. }
  180. }
  181. }