IoCylinder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 class IoCylinder : BaseDevice, IDevice
  13. {
  14. private DIAccessor _diOpened;
  15. private DIAccessor _diClosed;
  16. private DOAccessor _doOpen;
  17. private DOAccessor _doClose;
  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 bool EnableOpen { get; set; }
  23. public bool EnableClose { get; set; }
  24. public int SetPoint
  25. {
  26. get
  27. {
  28. if (_doOpen.Value && _doClose.Value) return (int) CylinderState.Error;
  29. if (_doOpen.Value && !_doClose.Value) return (int) CylinderState.Open;
  30. if (!_doOpen.Value && _doClose.Value) return (int) CylinderState.Close;
  31. if (!_doOpen.Value && !_doClose.Value) return (int) CylinderState.Unknown;
  32. return (int) CylinderState.Unknown;
  33. }
  34. }
  35. public CylinderState State
  36. {
  37. get
  38. {
  39. if (_diOpened != null && _diClosed != null)
  40. {
  41. if (OpenFeedback && _diClosed.Value)
  42. return CylinderState.Error;
  43. if (OpenFeedback && !_diClosed.Value)
  44. return CylinderState.Open;
  45. if (!OpenFeedback && _diClosed.Value)
  46. return CylinderState.Close;
  47. if (!OpenFeedback && !_diClosed.Value)
  48. return CylinderState.Unknown;
  49. }
  50. return CylinderState.Unknown;
  51. }
  52. }
  53. public bool OpenFeedback
  54. {
  55. get { return _diOpened != null && _diOpened.Value; }
  56. }
  57. public bool CloseFeedback
  58. {
  59. get { return _diClosed != null && _diClosed.Value; }
  60. }
  61. public bool OpenSetPoint
  62. {
  63. get { return _doOpen != null && _doOpen.Value; }
  64. set
  65. {
  66. if (_doOpen != null && _doOpen.Check(value, out _))
  67. _doOpen.Value = value;
  68. }
  69. }
  70. public bool CloseSetPoint
  71. {
  72. get { return _doClose != null && _doClose.Value; }
  73. set
  74. {
  75. if (_doClose != null && _doClose.Check(value, out _))
  76. _doClose.Value = value;
  77. }
  78. }
  79. private AITCylinderData DeviceData
  80. {
  81. get
  82. {
  83. AITCylinderData deviceData = new AITCylinderData
  84. {
  85. Module = Module,
  86. DeviceName = Name,
  87. DeviceSchematicId = DeviceID,
  88. DisplayName = Display,
  89. OpenFeedback = OpenFeedback,
  90. CloseFeedback = CloseFeedback,
  91. OpenSetPoint = OpenSetPoint,
  92. CloseSetPoint = CloseSetPoint
  93. };
  94. return deviceData;
  95. }
  96. }
  97. //public int Status
  98. //{
  99. // get
  100. // {
  101. // if (OpenFeedback && _diClosed.Value)
  102. // return (int)CylinderState.Error;
  103. // if (OpenFeedback && !_diClosed.Value)
  104. // return (int)CylinderState.Open;
  105. // if (!OpenFeedback && _diClosed.Value)
  106. // return (int)CylinderState.Close;
  107. // if (!OpenFeedback && !_diClosed.Value)
  108. // return (int)CylinderState.Unknown;
  109. // return (int)CylinderState.Unknown;
  110. // }
  111. //}
  112. public IoCylinder(string module, XmlElement node, string ioModule = "")
  113. {
  114. base.Module = node.GetAttribute("module");
  115. base.Name = node.GetAttribute("id");
  116. base.Display = node.GetAttribute("display");
  117. base.DeviceID = node.GetAttribute("schematicId");
  118. _diOpened = ParseDiNode("diOpen", node, ioModule);
  119. _diClosed = ParseDiNode("diClose", node, ioModule);
  120. _doOpen = ParseDoNode("doOpen", node, ioModule);
  121. _doClose = ParseDoNode("doClose", node, ioModule);
  122. }
  123. public bool Initialize()
  124. {
  125. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  126. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", InvokeOpenCylinder);
  127. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", InvokeCloseCylinder);
  128. DEVICE.Register($"{Name}.{AITCylinderOperation.Open}", (out string reason, int time, object[] param) =>
  129. {
  130. bool ret = SetCylinder(true, out reason);
  131. if (ret)
  132. {
  133. reason = string.Format("Open Cylinder {0}", Name);
  134. return true;
  135. }
  136. return false;
  137. });
  138. DEVICE.Register($"{Name}.{AITCylinderOperation.Close}", (out string reason, int time, object[] param) =>
  139. {
  140. bool ret = SetCylinder(false, out reason);
  141. if (ret)
  142. {
  143. reason = string.Format("Close Cylinder {0}", Name);
  144. return true;
  145. }
  146. return false;
  147. });
  148. return true;
  149. }
  150. private bool InvokeOpenCylinder(string arg1, object[] arg2)
  151. {
  152. string reason;
  153. if (!SetCylinder(true, out reason))
  154. {
  155. EV.PostWarningLog(Module, $"Can not open cylinder {Module}.{Name}, {reason}");
  156. return false;
  157. }
  158. EV.PostInfoLog(Module, $"Open cylinder {Module}.{Name}");
  159. return true;
  160. }
  161. private bool InvokeCloseCylinder(string arg1, object[] arg2)
  162. {
  163. string reason;
  164. if (!SetCylinder(false, out reason))
  165. {
  166. EV.PostWarningLog(Module, $"Can not close cylinder {Module}.{Name}, {reason}");
  167. return false;
  168. }
  169. EV.PostInfoLog(Module, $"Close cylinder {Module}.{Name}");
  170. return true;
  171. }
  172. public void Terminate()
  173. {
  174. CloseSetPoint = false;
  175. OpenSetPoint = false;
  176. }
  177. public bool SetCylinder(bool isOpen, out string reason)
  178. {
  179. if (_doOpen != null)
  180. {
  181. if (!_doOpen.Check(isOpen, out reason))
  182. {
  183. return false;
  184. }
  185. }
  186. if (_doClose != null)
  187. {
  188. if (!_doClose.Check(!isOpen, out reason))
  189. {
  190. return false;
  191. }
  192. }
  193. reason = "";
  194. OpenSetPoint = isOpen;
  195. CloseSetPoint = !isOpen;
  196. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  197. _timer.Start(1000 * 60 * 3);
  198. return true;
  199. }
  200. public void Monitor()
  201. {
  202. try
  203. {
  204. if (_diOpened != null && _diClosed != null)
  205. {
  206. MonitorSetAndResetValue();
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. LOG.Write(ex);
  212. }
  213. }
  214. private void MonitorSetAndResetValue()
  215. {
  216. if (_timer.IsTimeout())
  217. {
  218. _timer.Stop();
  219. if (State != _operation)
  220. {
  221. if (_operation == CylinderState.Open)
  222. {
  223. string reason;
  224. if (!_doOpen.Check(true, out reason))
  225. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Open Cylinder Failed for interlock, " + reason);
  226. else
  227. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Cylinder hold close status");
  228. }
  229. else
  230. {
  231. string reason;
  232. if (!_doOpen.Check(false, out reason))
  233. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Close Cylinder Failed for interlock, " + reason);
  234. else
  235. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Cylinder hold open status");
  236. }
  237. }
  238. _operation = (CylinderState) SetPoint;
  239. }
  240. else if (_timer.IsIdle())
  241. {
  242. _trigReset.CLK = SetPoint != (int) _operation; // fire event only check at first, SetPoint set by interlock
  243. if (_trigReset.Q)
  244. {
  245. if (_operation == CylinderState.Open)
  246. {
  247. string reason;
  248. if (!_doOpen.Check(true, out reason))
  249. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  250. string.Format("Cylinder {0} was {1},Reason:{2}", Display, "Close", reason));
  251. else
  252. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  253. string.Format("Cylinder {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
  254. }
  255. else
  256. {
  257. string reason;
  258. if (!_doOpen.Check(false, out reason))
  259. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  260. string.Format("Cylinder {0} was {1},Reason:{2}", Display, "Open", reason));
  261. else
  262. EV.PostMessage(Module, EventEnum.SwInterlock, Module,
  263. string.Format("Cylinder {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
  264. }
  265. _operation = (CylinderState) SetPoint;
  266. }
  267. }
  268. _trigError.CLK = State == CylinderState.Error;
  269. if (_trigError.Q)
  270. {
  271. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Cylinder in error status");
  272. }
  273. if ((SetPoint == (int) State) && (SetPoint == (int) CylinderState.Open || SetPoint == (int) CylinderState.Close))
  274. {
  275. CloseSetPoint = false;
  276. OpenSetPoint = false;
  277. }
  278. }
  279. public void Reset()
  280. {
  281. _trigReset.RST = true;
  282. _trigError.RST = true;
  283. }
  284. }
  285. }