IoLid.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.IOCore;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Device.Bases;
  9. namespace Aitex.Core.RT.Device.Unit
  10. {
  11. public class IoLid : BaseDevice, IDevice
  12. {
  13. private DIAccessor _diOpened;
  14. private DIAccessor _diClosed;
  15. private DOAccessor _doOpen;
  16. private DOAccessor _doClose;
  17. private LidState _operation;
  18. private DeviceTimer _timer = new DeviceTimer();
  19. private R_TRIG _trigReset = new R_TRIG();
  20. private R_TRIG _trigError = new R_TRIG();
  21. public int SetPoint
  22. {
  23. get
  24. {
  25. if (_doOpen.Value && _doClose.Value) return (int) LidState.Error;
  26. if (_doOpen.Value && !_doClose.Value) return (int) LidState.Open;
  27. if (!_doOpen.Value && _doClose.Value) return (int) LidState.Close;
  28. if (!_doOpen.Value && !_doClose.Value) return (int) LidState.Unknown;
  29. return (int) LidState.Unknown;
  30. }
  31. }
  32. public int Status
  33. {
  34. get
  35. {
  36. if (_diOpened.Value && _diClosed.Value)
  37. return (int)LidState.Error;
  38. if (_diOpened.Value && !_diClosed.Value)
  39. return (int)LidState.Open;
  40. if (!_diOpened.Value && _diClosed.Value)
  41. return (int)LidState.Close;
  42. if (!_diOpened.Value && !_diClosed.Value)
  43. return (int)LidState.Unknown;
  44. return (int)LidState.Unknown;
  45. }
  46. }
  47. public IoLid(string module, XmlElement node, string ioModule = "")
  48. {
  49. base.Module = module;
  50. base.Name = node.GetAttribute("id");
  51. base.Display = node.GetAttribute("display");
  52. base.DeviceID = node.GetAttribute("schematicId");
  53. _diOpened = ParseDiNode("diOpen", node, ioModule);
  54. _diClosed = ParseDiNode("diClose", node, ioModule);
  55. _doOpen = ParseDoNode("doOpen", node, ioModule);
  56. _doClose = ParseDoNode("doClose", node, ioModule);
  57. }
  58. public bool Initialize()
  59. {
  60. DEVICE.Register(String.Format("{0}.{1}", Name, AITLidOperation.OpenLid), (out string reason, int time, object[] param) =>
  61. {
  62. bool ret = SetLid(true, out reason);
  63. if (ret)
  64. {
  65. reason = string.Format("Open Lid {0}", Name);
  66. return true;
  67. }
  68. return false;
  69. });
  70. DEVICE.Register(String.Format("{0}.{1}", Name, AITLidOperation.CloseLid), (out string reason, int time, object[] param) =>
  71. {
  72. bool ret = SetLid(false, out reason);
  73. if (ret)
  74. {
  75. reason = string.Format("Close Lid {0}", Name);
  76. return true;
  77. }
  78. return false;
  79. });
  80. return true;
  81. }
  82. public void Terminate()
  83. {
  84. _doOpen.Value = false;
  85. _doClose.Value = false;
  86. }
  87. public bool SetLid(bool isOpen, out string reason)
  88. {
  89. if (!_doOpen.Check(isOpen, out reason))
  90. return false;
  91. if (!_doClose.Check(!isOpen, out reason))
  92. return false;
  93. if (!_doOpen.SetValue(isOpen, out reason))
  94. return false;
  95. if (!_doClose.SetValue(!isOpen, out reason))
  96. return false;
  97. _operation = isOpen ? LidState.Open : LidState.Close;
  98. _timer.Start(1000 * 60 * 3);
  99. return true;
  100. }
  101. public void Monitor()
  102. {
  103. try
  104. {
  105. if (_timer.IsTimeout())
  106. {
  107. _timer.Stop();
  108. if (Status != (int)_operation)
  109. {
  110. if (_operation == LidState.Open)
  111. {
  112. string reason;
  113. if (!_doOpen.Check(true, out reason))
  114. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Open lid Failed for interlock, " + reason);
  115. else
  116. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Lid hold close status");
  117. }
  118. else
  119. {
  120. string reason;
  121. if (!_doOpen.Check(false, out reason))
  122. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Close lid Failed for interlock, " + reason);
  123. else
  124. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Lid hold open status");
  125. }
  126. }
  127. _operation = (LidState)SetPoint;
  128. }
  129. else if (_timer.IsIdle())
  130. {
  131. _trigReset.CLK = SetPoint != (int)_operation; // fire event only check at first, SetPoint set by interlock
  132. if (_trigReset.Q)
  133. {
  134. if (_operation == LidState.Open)
  135. {
  136. string reason;
  137. if (!_doOpen.Check(true, out reason))
  138. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Lid {0} was {1},Reason:{2}", Display, "Close", reason));
  139. else
  140. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Lid {0} was {1},Reason {2}", Display, "Close", "PLC kept"));
  141. }
  142. else
  143. {
  144. string reason;
  145. if (!_doOpen.Check(false, out reason))
  146. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Lid {0} was {1},Reason:{2}", Display, "Open", reason));
  147. else
  148. EV.PostMessage(Module, EventEnum.SwInterlock, Module, string.Format("Lid {0} was {1},Reason {2}", Display, "Open", "PLC Kept"));
  149. }
  150. _operation = (LidState)SetPoint;
  151. }
  152. }
  153. _trigError.CLK = Status == (int) LidState.Error;
  154. if (_trigError.Q)
  155. {
  156. EV.PostMessage(Module, EventEnum.DefaultAlarm, "Lid in error status");
  157. }
  158. if ((SetPoint == Status) && (SetPoint == (int)LidState.Open || SetPoint == (int)LidState.Close))
  159. {
  160. _doClose.Value = false;
  161. _doOpen.Value = false;
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. LOG.Write(ex);
  167. }
  168. }
  169. public void Reset()
  170. {
  171. _trigReset.RST = true;
  172. _trigError.RST = true;
  173. }
  174. }
  175. }