IoCylinder.cs 12 KB

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