IoCylinder.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 VirgoRT.Devices
  12. {
  13. public class IoCylinder : BaseDevice, IDevice
  14. {
  15. private readonly DIAccessor _diON;
  16. private readonly DIAccessor _diOFF;
  17. private readonly DOAccessor _doON;
  18. private readonly DOAccessor _doOFF;
  19. private CylinderState _operation;
  20. private readonly DeviceTimer _timer = new DeviceTimer();
  21. private readonly R_TRIG _trigReset = new R_TRIG();
  22. private readonly R_TRIG _trigError = new R_TRIG();
  23. public bool EnableOpen { get; set; }
  24. public bool EnableClose { get; set; }
  25. private string _error = null;
  26. public int SetPoint
  27. {
  28. get
  29. {
  30. if (_doON.Value && _doOFF.Value) return (int)CylinderState.Error;
  31. if (_doON.Value && !_doOFF.Value) return (int)CylinderState.Open;
  32. if (!_doON.Value && _doOFF.Value) return (int)CylinderState.Close;
  33. if (!_doON.Value && !_doOFF.Value) return (int)CylinderState.Unknown;
  34. return (int)CylinderState.Unknown;
  35. }
  36. }
  37. public CylinderState State
  38. {
  39. get
  40. {
  41. if (_diON != null && _diOFF != null)
  42. {
  43. if (ONFeedback && _diOFF.Value)
  44. return CylinderState.Error;
  45. if (ONFeedback && !_diOFF.Value)
  46. return CylinderState.Open;
  47. if (!ONFeedback && _diOFF.Value)
  48. return CylinderState.Close;
  49. if (!ONFeedback && !_diOFF.Value)
  50. return CylinderState.Unknown;
  51. }
  52. return CylinderState.Unknown;
  53. }
  54. }
  55. public bool ONFeedback
  56. {
  57. get { return _diON != null && _diON.Value; }
  58. }
  59. public bool OFFFeedback
  60. {
  61. get { return _diOFF != null && _diOFF.Value; }
  62. }
  63. public bool ONSetPoint
  64. {
  65. get
  66. {
  67. return _doON != null && _doON.Value;
  68. }
  69. private set
  70. {
  71. if (_doON != null )
  72. {
  73. if (!_doON.Check(value, out _error))
  74. {
  75. LOG.Write(_error);
  76. }
  77. else
  78. {
  79. _doON.Value = value;
  80. }
  81. }
  82. }
  83. }
  84. public bool OFFSetPoint
  85. {
  86. get
  87. {
  88. return _doOFF != null && _doOFF.Value;
  89. }
  90. private set
  91. {
  92. if (_doOFF != null )
  93. {
  94. if (!_doOFF.Check(value, out _error))
  95. {
  96. LOG.Write(_error);
  97. }
  98. else
  99. {
  100. _doOFF.Value = value;
  101. }
  102. }
  103. }
  104. }
  105. private AITCylinderData DeviceData
  106. {
  107. get
  108. {
  109. return new AITCylinderData
  110. {
  111. Module = Module,
  112. DeviceName = Name,
  113. DeviceSchematicId = DeviceID,
  114. DisplayName = Display,
  115. OpenFeedback = ONFeedback,
  116. CloseFeedback = OFFFeedback,
  117. OpenSetPoint = ONSetPoint,
  118. CloseSetPoint = OFFSetPoint
  119. };
  120. }
  121. }
  122. private object _locker = new object();
  123. public IoCylinder(string module, XmlElement node, string ioModule = "")
  124. {
  125. base.Module = module;
  126. base.Name = node.GetAttribute("id");
  127. base.Display = node.GetAttribute("display");
  128. base.DeviceID = node.GetAttribute("schematicId");
  129. _operation = CylinderState.Unknown;
  130. _diON = ParseDiNode("diON", node, ioModule);
  131. _diOFF = ParseDiNode("diOFF", node, ioModule);
  132. _doON = ParseDoNode("doON", node, ioModule);
  133. _doOFF = ParseDoNode("doOFF", node, ioModule);
  134. }
  135. public bool Initialize()
  136. {
  137. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  138. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Open}", (s, objects) => SetCylinder(true, out _));
  139. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.Close}", (s, objects) => SetCylinder(false, out _));
  140. OP.Subscribe($"{Module}.{Name}.{AITCylinderOperation.SetState}", (out string reason, int time, object[] param) =>
  141. {
  142. bool isUp = (string)param[0] == "Up" ? true : false;
  143. SetCylinder(isUp, out reason);
  144. return true;
  145. });
  146. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Open}",
  147. (out string reason, int time, object[] param) => SetCylinder(true, out reason));
  148. DEVICE.Register($"{Module}.{Name}.{AITCylinderOperation.Close}",
  149. (out string reason, int time, object[] param) => SetCylinder(false, out reason));
  150. return true;
  151. }
  152. public void Terminate()
  153. {
  154. OFFSetPoint = false;
  155. ONSetPoint = false;
  156. }
  157. public bool SetCylinder(bool isOpen, out string reason)
  158. {
  159. reason = "";
  160. //if (isOpen && State == CylinderState.Open)
  161. //{
  162. // EV.PostInfoLog(Module, $"检查到气缸 {Name} 已经开");
  163. // return true;
  164. //}
  165. //if (!isOpen && State == CylinderState.Close)
  166. //{
  167. // EV.PostInfoLog(Module, $"检查到气缸 {Name} 已经关");
  168. // return true;
  169. //}
  170. lock (_locker)
  171. {
  172. _timer.Start(5 * 1000);
  173. ONSetPoint = isOpen;
  174. reason += _error;
  175. reason += " ";
  176. OFFSetPoint = !isOpen;
  177. reason += _error;
  178. _operation = isOpen ? CylinderState.Open : CylinderState.Close;
  179. }
  180. EV.PostInfoLog(Module, $"{(isOpen ? "打开" : "关闭")} 气缸 {Name}");
  181. return true;
  182. }
  183. public void Monitor()
  184. {
  185. try
  186. {
  187. lock (_locker)
  188. {
  189. if (_timer.IsTimeout())
  190. {
  191. _timer.Stop();
  192. if (State != _operation)
  193. {
  194. if (_operation == CylinderState.Open)
  195. {
  196. if (!_doON.Check(true, out var reason))
  197. EV.PostAlarmLog(Module, $"{Name} 气缸信号无法打开, interlock, " + reason);
  198. else
  199. EV.PostAlarmLog(Module, $"{Name} 气缸信号仍然关闭");
  200. }
  201. else
  202. {
  203. if (!_doON.Check(false, out var reason))
  204. EV.PostAlarmLog(Module, $"{Name} 气缸信号无法关闭, interlock, " + reason);
  205. else
  206. EV.PostAlarmLog(Module, $"{Name} 气缸信号仍然打开");
  207. }
  208. }
  209. _operation = (CylinderState)SetPoint;
  210. }
  211. else if (_timer.IsIdle())
  212. {
  213. _trigReset.CLK = SetPoint != (int)_operation; // fire event only check at first, SetPoint set by interlock
  214. if (_trigReset.Q)
  215. {
  216. if (_operation == CylinderState.Open)
  217. {
  218. EV.PostAlarmLog(Module, !_doON.Check(true, out var reason)
  219. ? $"气缸信号 {Display} was Close,Reason:{reason}"
  220. : $"气缸信号 {Display} was Close,Reason PLC kept");
  221. }
  222. else
  223. {
  224. EV.PostAlarmLog(Module, !_doON.Check(false, out var reason)
  225. ? $"气缸信号 {Display} was Open,Reason:{reason}"
  226. : $"气缸信号 {Display} was Open,Reason PLC Kept");
  227. }
  228. _operation = (CylinderState)SetPoint;
  229. }
  230. }
  231. }
  232. _trigError.CLK = State == CylinderState.Error;
  233. if (_trigError.Q)
  234. {
  235. EV.PostAlarmLog(Module, $"{Name} 气缸状态错误");
  236. }
  237. //if ((SetPoint == (int)State) && (SetPoint == (int)CylinderState.Open || SetPoint == (int)CylinderState.Close))
  238. //{
  239. // OFFSetPoint = false;
  240. // ONSetPoint = false;
  241. //}
  242. }
  243. catch (Exception ex)
  244. {
  245. LOG.Write(ex);
  246. }
  247. }
  248. public void Reset()
  249. {
  250. _trigReset.RST = true;
  251. _trigError.RST = true;
  252. }
  253. }
  254. }