IoLift2.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.RT.SCCore;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Event;
  12. namespace Aitex.Core.RT.Device.Unit
  13. {
  14. /// <summary>
  15. /// 支持Alarm机制
  16. ///
  17. /// DI open
  18. /// DI close
  19. ///
  20. /// DO open
  21. /// DO close
  22. ///
  23. /// </summary>
  24. ///
  25. public class IoLift2 : BaseDevice, IDevice
  26. {
  27. public LiftStateEnum State
  28. {
  29. get
  30. {
  31. if (_diUp.Value && _diDown.Value)
  32. return LiftStateEnum.Error;
  33. if (_diUp.Value && !_diDown.Value)
  34. return LiftStateEnum.Up;
  35. if (!_diUp.Value && _diDown.Value)
  36. return LiftStateEnum.Down;
  37. return LiftStateEnum.Unknown;
  38. }
  39. }
  40. enum DeviceState
  41. {
  42. Idle,
  43. MovingUp,
  44. MovingDown,
  45. Error,
  46. }
  47. private DIAccessor _diUp;
  48. private DIAccessor _diDown;
  49. private DOAccessor _doUp;
  50. private DOAccessor _doDown;
  51. private DeviceState _state = DeviceState.Idle;
  52. private DeviceTimer _timer = new DeviceTimer();
  53. private SCConfigItem _scTimeout;
  54. public bool IsUp { get { return !_diDown.Value && _diUp.Value; } }
  55. public bool IsDown { get { return _diDown.Value && !_diUp.Value; } }
  56. public AlarmEventItem AlarmMoveTimeout { get; set; }
  57. public AlarmEventItem AlarmSignalAbnormal { get; set; }
  58. private object _lockerState = new object();
  59. public IoLift2(string module, XmlElement node, string ioModule = "")
  60. {
  61. var attrModule = node.GetAttribute("module");
  62. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  63. base.Name = node.GetAttribute("id");
  64. base.Display = node.GetAttribute("display");
  65. base.DeviceID = node.GetAttribute("schematicId");
  66. string scBasePath = node.GetAttribute("scBasePath");
  67. if (string.IsNullOrEmpty(scBasePath))
  68. scBasePath = $"{Module}.{Name}";
  69. else
  70. {
  71. scBasePath = scBasePath.Replace("{module}", Module);
  72. }
  73. _diUp = ParseDiNode("diUp", node, ioModule);
  74. _diDown = ParseDiNode("diDown", node, ioModule);
  75. _doUp = ParseDoNode("doUp", node, ioModule);
  76. _doDown = ParseDoNode("doDown", node, ioModule);
  77. _scTimeout = ParseScNode("scTimeout", node, ioModule, $"{scBasePath}.{Name}.MoveTimeout");
  78. }
  79. public bool Initialize()
  80. {
  81. _state = DeviceState.Idle;
  82. AlarmMoveTimeout = SubscribeAlarm($"{Module}.{Name}.MoveTimeout", "", null);
  83. AlarmSignalAbnormal = SubscribeAlarm($"{Module}.{Name}.SignalAbnormal", $"{Name} Up&Down signal trigger at same time", ResetCheckSignalAbnormal);
  84. DATA.Subscribe($"{Module}.{Name}.State", () => State.ToString());
  85. DATA.Subscribe($"{Module}.{Name}.UpFeedback", ()=> _diUp.Value && !_diDown.Value);
  86. DATA.Subscribe($"{Module}.{Name}.DownFeedback", () => _diDown.Value && !_diUp.Value);
  87. OP.Subscribe($"{Module}.{Name}.MoveUp", (out string reason, int time, object[] param) =>
  88. {
  89. reason = "";
  90. return MoveUp(out reason);
  91. });
  92. OP.Subscribe($"{Module}.{Name}.MoveDown", (out string reason, int time, object[] param) =>
  93. {
  94. reason = "";
  95. return MoveDown(out reason);
  96. });
  97. return true;
  98. }
  99. public void Monitor()
  100. {
  101. lock (_lockerState)
  102. {
  103. switch (_state)
  104. {
  105. case DeviceState.MovingUp:
  106. if (IsUp)
  107. {
  108. _state = DeviceState.Idle;
  109. }
  110. else if (_timer.IsTimeout())
  111. {
  112. if (!_doUp.SetValue(false, out string reason))
  113. {
  114. LOG.Error($"{Module} reset DO failed, {reason}");
  115. }
  116. AlarmMoveTimeout.Description = $"Can not move {Name} up in {_scTimeout.IntValue} seconds";
  117. AlarmMoveTimeout.Set();
  118. _state = DeviceState.Error;
  119. }
  120. break;
  121. case DeviceState.MovingDown:
  122. if (IsDown)
  123. {
  124. _state = DeviceState.Idle;
  125. }
  126. else if (_timer.IsTimeout())
  127. {
  128. if (!_doUp.SetValue(true, out string reason))
  129. {
  130. LOG.Error($"{Module} reset DO failed, {reason}");
  131. }
  132. AlarmMoveTimeout.Description = $"Can not move {Name} down in {_scTimeout.IntValue} seconds";
  133. AlarmMoveTimeout.Set();
  134. _state = DeviceState.Error;
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. if (_diUp.Value && _diDown.Value)
  142. {
  143. AlarmSignalAbnormal.Set();
  144. }
  145. }
  146. public void Terminate()
  147. {
  148. }
  149. public bool Move(bool up, out string reason)
  150. {
  151. if (up)
  152. return MoveUp(out reason);
  153. return MoveDown(out reason);
  154. }
  155. public bool MoveUp(out string reason)
  156. {
  157. lock (_lockerState)
  158. {
  159. if (!_doDown.SetValue(false, out reason))
  160. {
  161. return false;
  162. }
  163. if (!_doUp.SetValue(true, out reason))
  164. {
  165. return false;
  166. }
  167. _timer.Start(_scTimeout.IntValue * 1000);
  168. _state = DeviceState.MovingUp;
  169. }
  170. return true;
  171. }
  172. public bool MoveDown(out string reason)
  173. {
  174. lock (_lockerState)
  175. {
  176. if (!_doUp.SetValue(false, out reason))
  177. {
  178. return false;
  179. }
  180. if (!_doDown.SetValue(true, out reason))
  181. {
  182. return false;
  183. }
  184. _timer.Start(_scTimeout.IntValue * 1000);
  185. _state = DeviceState.MovingDown;
  186. }
  187. return true;
  188. }
  189. public bool ResetCheckSignalAbnormal()
  190. {
  191. return !(_diUp.Value && _diDown.Value);
  192. }
  193. public void Reset()
  194. {
  195. AlarmMoveTimeout.Reset();
  196. AlarmSignalAbnormal.Reset();
  197. }
  198. }
  199. }