IoLid.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.Value;
  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.Value;
  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. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", InvokeOpenCylinder);
  99. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", InvokeCloseCylinder);
  100. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}", (out string reason, int time, object[] param) =>
  101. {
  102. bool ret = SetCylinder(true, out reason);
  103. if (ret)
  104. {
  105. reason = $"Open Cylinder {Name}";
  106. return true;
  107. }
  108. return false;
  109. });
  110. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}", (out string reason, int time, object[] param) =>
  111. {
  112. bool ret = SetCylinder(false, out reason);
  113. if (ret)
  114. {
  115. reason = $"Close Cylinder {Name}";
  116. return true;
  117. }
  118. return false;
  119. });
  120. return true;
  121. }
  122. private bool InvokeOpenCylinder(string arg1, object[] arg2)
  123. {
  124. if (!SetCylinder(true, out var reason))
  125. {
  126. EV.PostWarningLog(Module, $"Can not open cylinder {Module}.{Name}, {reason}");
  127. return false;
  128. }
  129. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Open cylinder {Module}.{Name}");
  130. return true;
  131. }
  132. private bool InvokeCloseCylinder(string arg1, object[] arg2)
  133. {
  134. if (!SetCylinder(false, out var reason))
  135. {
  136. EV.PostWarningLog(Module, $"Can not close cylinder {Module}.{Name}, {reason}");
  137. return false;
  138. }
  139. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Close cylinder {Module}.{Name}");
  140. return true;
  141. }
  142. public void Terminate()
  143. {
  144. OFFSetPoint = false;
  145. ONSetPoint = false;
  146. }
  147. public bool SetCylinder(bool isOpen, out string reason)
  148. {
  149. reason = "";
  150. ONSetPoint = isOpen;
  151. OFFSetPoint = !isOpen;
  152. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  153. _timer.Start(1000 * 60 * 3);
  154. return true;
  155. }
  156. public void Monitor()
  157. {
  158. try
  159. {
  160. if (_timer.IsTimeout())
  161. {
  162. _timer.Stop();
  163. if (State != _operation)
  164. {
  165. if (_operation == CylinderState.Open)
  166. {
  167. if (!_doON.Check(true, out var reason))
  168. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Open Cylinder Failed for interlock, " + reason);
  169. else
  170. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold close status");
  171. }
  172. else
  173. {
  174. if (!_doON.Check(false, out var reason))
  175. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Close Cylinder Failed for interlock, " + reason);
  176. else
  177. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder hold open status");
  178. }
  179. }
  180. _operation = (CylinderState)SetPoint;
  181. }
  182. else if (_timer.IsIdle())
  183. {
  184. return;
  185. }
  186. _trigError.CLK = State == CylinderState.Error;
  187. if (_trigError.Q)
  188. {
  189. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "Cylinder in error status");
  190. }
  191. if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  192. {
  193. OFFSetPoint = false;
  194. ONSetPoint = false;
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. //LOG.Write(ex);
  200. }
  201. }
  202. public void Reset()
  203. {
  204. _trigReset.RST = true;
  205. _trigError.RST = true;
  206. }
  207. }
  208. }