IoLid.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.IOCore;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.Util;
  11. namespace Venus_RT.Devices.IODevices
  12. {
  13. public class IoLid : BaseDevice, IDevice
  14. {
  15. private DIAccessor _diOFF;
  16. private DOAccessor _doON;
  17. private DOAccessor _doOFF;
  18. private CylinderState _operation;
  19. private DeviceTimer _timer = new DeviceTimer();
  20. private R_TRIG _trigReset = new R_TRIG();
  21. private R_TRIG _trigError = new R_TRIG();
  22. public int SetPoint
  23. {
  24. get
  25. {
  26. return (int)CylinderState.Unknown;
  27. /*
  28. if (_doON.Value && _doOFF.Value) return (int)CylinderState.Error;
  29. if (_doON.Value && !_doOFF.Value) return (int)CylinderState.Open;
  30. if (!_doON.Value && _doOFF.Value) return (int)CylinderState.Close;
  31. if (!_doON.Value && !_doOFF.Value) return (int)CylinderState.Unknown;
  32. */
  33. }
  34. }
  35. public CylinderState State
  36. {
  37. get
  38. {
  39. return _diOFF.Value ? CylinderState.Close : CylinderState.Open;
  40. }
  41. }
  42. public bool OFFFeedback
  43. {
  44. get { return _diOFF.Value; }
  45. }
  46. public bool ONSetPoint
  47. {
  48. get
  49. {
  50. return _doON != null ? _doON.Value : false;
  51. }
  52. set
  53. {
  54. if (_doON != null && _doON.Check(value, out _))
  55. _doON.Value = value;
  56. }
  57. }
  58. public bool OFFSetPoint
  59. {
  60. get
  61. {
  62. return _doOFF != null ? _doON.Value : true;
  63. }
  64. set
  65. {
  66. if (_doOFF != null && _doOFF.Check(value, out _))
  67. _doOFF.Value = value;
  68. }
  69. }
  70. private AITCylinderData DeviceData
  71. {
  72. get
  73. {
  74. AITCylinderData deviceData = new AITCylinderData
  75. {
  76. Module = Module,
  77. DeviceName = Name,
  78. DeviceSchematicId = DeviceID,
  79. DisplayName = Display,
  80. CloseFeedback = OFFFeedback,
  81. };
  82. return deviceData;
  83. }
  84. }
  85. public IoLid(string module, XmlElement node, string ioModule = "")
  86. {
  87. base.Module = module;
  88. base.Name = node.GetAttribute("id");
  89. base.Display = node.GetAttribute("display");
  90. base.DeviceID = node.GetAttribute("schematicId");
  91. _diOFF = ParseDiNode("diOFF", node, ioModule);
  92. _doON = ParseDoNode("doON", node, ioModule);
  93. _doOFF = ParseDoNode("doOFF", node, ioModule);
  94. }
  95. public bool Initialize()
  96. {
  97. //DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  98. DATA.Subscribe($"{Module}.{Name}.IsClosed", () => OFFFeedback);
  99. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", InvokeOpenCylinder);
  100. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", InvokeCloseCylinder);
  101. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}", (out string reason, int time, object[] param) =>
  102. {
  103. bool ret = SetCylinder(true, out reason);
  104. if (ret)
  105. {
  106. reason = $"Open Cylinder {Name}";
  107. return true;
  108. }
  109. return false;
  110. });
  111. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}", (out string reason, int time, object[] param) =>
  112. {
  113. bool ret = SetCylinder(false, out reason);
  114. if (ret)
  115. {
  116. reason = $"Close Cylinder {Name}";
  117. return true;
  118. }
  119. return false;
  120. });
  121. return true;
  122. }
  123. private bool InvokeOpenCylinder(string arg1, object[] arg2)
  124. {
  125. if (!SetCylinder(true, out var reason))
  126. {
  127. EV.PostWarningLog(Module, $"Can not open cylinder {Module}.{Name}, {reason}");
  128. return false;
  129. }
  130. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Open cylinder {Module}.{Name}");
  131. return true;
  132. }
  133. private bool InvokeCloseCylinder(string arg1, object[] arg2)
  134. {
  135. if (!SetCylinder(false, out var reason))
  136. {
  137. EV.PostWarningLog(Module, $"Can not close cylinder {Module}.{Name}, {reason}");
  138. return false;
  139. }
  140. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Close cylinder {Module}.{Name}");
  141. return true;
  142. }
  143. public void Terminate()
  144. {
  145. OFFSetPoint = false;
  146. ONSetPoint = false;
  147. }
  148. public bool SetCylinder(bool isOpen, out string reason)
  149. {
  150. reason = "";
  151. ONSetPoint = isOpen;
  152. OFFSetPoint = !isOpen;
  153. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  154. _timer.Start(1000 * 60 * 3);
  155. return true;
  156. }
  157. public void Monitor()
  158. {
  159. try
  160. {
  161. if (_timer.IsTimeout())
  162. {
  163. _timer.Stop();
  164. if (State != _operation)
  165. {
  166. if (_operation == CylinderState.Open)
  167. {
  168. if (!_doON.Check(true, out var reason))
  169. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Open Cylinder Failed for interlock, " + reason);
  170. else
  171. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold close status");
  172. }
  173. else
  174. {
  175. if (!_doON.Check(false, out var reason))
  176. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Close Cylinder Failed for interlock, " + reason);
  177. else
  178. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold open status");
  179. }
  180. }
  181. _operation = (CylinderState)SetPoint;
  182. }
  183. else if (_timer.IsIdle())
  184. {
  185. return;
  186. }
  187. _trigError.CLK = State == CylinderState.Error;
  188. if (_trigError.Q)
  189. {
  190. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder in error status");
  191. }
  192. if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  193. {
  194. OFFSetPoint = false;
  195. ONSetPoint = false;
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. LOG.WriteExeption(ex);
  201. }
  202. }
  203. public void Reset()
  204. {
  205. _trigReset.RST = true;
  206. _trigError.RST = true;
  207. }
  208. }
  209. }