IoLid.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Aitex.Core.RT.Device.Unit
  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. //OpenSetPoint = ONSetPoint,
  82. //CloseSetPoint = OFFSetPoint
  83. };
  84. return deviceData;
  85. }
  86. }
  87. public IoLid(string module, XmlElement node, string ioModule = "")
  88. {
  89. base.Module = module;
  90. base.Name = node.GetAttribute("id");
  91. base.Display = node.GetAttribute("display");
  92. base.DeviceID = node.GetAttribute("schematicId");
  93. _diOFF = ParseDiNode("diOFF", node, ioModule);
  94. _doON = ParseDoNode("doON", node, ioModule);
  95. _doOFF = ParseDoNode("doOFF", node, ioModule);
  96. }
  97. public bool Initialize()
  98. {
  99. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  100. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", InvokeOpenCylinder);
  101. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", InvokeCloseCylinder);
  102. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}", (out string reason, int time, object[] param) =>
  103. {
  104. bool ret = SetCylinder(true, out reason);
  105. if (ret)
  106. {
  107. reason = $"Open Cylinder {Name}";
  108. return true;
  109. }
  110. return false;
  111. });
  112. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}", (out string reason, int time, object[] param) =>
  113. {
  114. bool ret = SetCylinder(false, out reason);
  115. if (ret)
  116. {
  117. reason = $"Close Cylinder {Name}";
  118. return true;
  119. }
  120. return false;
  121. });
  122. return true;
  123. }
  124. private bool InvokeOpenCylinder(string arg1, object[] arg2)
  125. {
  126. if (!SetCylinder(true, out var reason))
  127. {
  128. EV.PostWarningLog(Module, $"Can not open cylinder {Module}.{Name}, {reason}");
  129. return false;
  130. }
  131. EV.PostInfoLog(Module, $"Open cylinder {Module}.{Name}");
  132. return true;
  133. }
  134. private bool InvokeCloseCylinder(string arg1, object[] arg2)
  135. {
  136. if (!SetCylinder(false, out var reason))
  137. {
  138. EV.PostWarningLog(Module, $"Can not close cylinder {Module}.{Name}, {reason}");
  139. return false;
  140. }
  141. EV.PostInfoLog(Module, $"Close cylinder {Module}.{Name}");
  142. return true;
  143. }
  144. public void Terminate()
  145. {
  146. OFFSetPoint = false;
  147. ONSetPoint = false;
  148. }
  149. public bool SetCylinder(bool isOpen, out string reason)
  150. {
  151. reason = "";
  152. ONSetPoint = isOpen;
  153. OFFSetPoint = !isOpen;
  154. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  155. _timer.Start(1000 * 60 * 3);
  156. return true;
  157. }
  158. public void Monitor()
  159. {
  160. try
  161. {
  162. if (_timer.IsTimeout())
  163. {
  164. _timer.Stop();
  165. if (State != _operation)
  166. {
  167. if (_operation == CylinderState.Open)
  168. {
  169. if (!_doON.Check(true, out var reason))
  170. EV.PostAlarmLog(Module, "Open Cylinder Failed for interlock, " + reason);
  171. else
  172. EV.PostAlarmLog(Module, "Cylinder hold close status");
  173. }
  174. else
  175. {
  176. if (!_doON.Check(false, out var reason))
  177. EV.PostAlarmLog(Module, "Close Cylinder Failed for interlock, " + reason);
  178. else
  179. EV.PostAlarmLog(Module, "Cylinder hold open status");
  180. }
  181. }
  182. _operation = (CylinderState)SetPoint;
  183. }
  184. else if (_timer.IsIdle())
  185. {
  186. return;
  187. /*
  188. _trigReset.CLK = SetPoint != (int)_operation; // fire event only check at first, SetPoint set by interlock
  189. if (_trigReset.Q)
  190. {
  191. if (_operation == CylinderState.Open)
  192. {
  193. string reason;
  194. if (!_doON.Check(true, out reason))
  195. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  196. $"Cylinder {Display} was {"Close"},Reason:{reason}");
  197. else
  198. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  199. $"Cylinder {Display} was {"Close"},Reason {"PLC kept"}");
  200. }
  201. else
  202. {
  203. string reason;
  204. if (!_doON.Check(false, out reason))
  205. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  206. $"Cylinder {Display} was {"Open"},Reason:{reason}");
  207. else
  208. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  209. $"Cylinder {Display} was {"Open"},Reason {"PLC Kept"}");
  210. }
  211. _operation = (CylinderState)SetPoint;
  212. }
  213. */
  214. }
  215. _trigError.CLK = State == CylinderState.Error;
  216. if (_trigError.Q)
  217. {
  218. EV.PostAlarmLog(Module, "Cylinder in error status");
  219. }
  220. if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  221. {
  222. OFFSetPoint = false;
  223. ONSetPoint = false;
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. LOG.Write(ex);
  229. }
  230. }
  231. public void Reset()
  232. {
  233. _trigReset.RST = true;
  234. _trigError.RST = true;
  235. }
  236. }
  237. }