IoLid.cs 8.1 KB

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