IoLift.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.SCCore;
  9. using Aitex.Core.Util;
  10. namespace Aitex.Core.RT.Device.Unit
  11. {
  12. public enum LiftStateEnum
  13. {
  14. Unknown,
  15. Up,
  16. Down,
  17. Error,
  18. }
  19. public class IoLift : BaseDevice, IDevice
  20. {
  21. public LiftStateEnum State
  22. {
  23. get
  24. {
  25. if (_diUp.Value && _diDown.Value)
  26. return LiftStateEnum.Error;
  27. if (_diUp.Value && !_diDown.Value)
  28. return LiftStateEnum.Up;
  29. if (!_diUp.Value && _diDown.Value)
  30. return LiftStateEnum.Down;
  31. return LiftStateEnum.Unknown;
  32. }
  33. }
  34. enum DeviceState
  35. {
  36. Idle,
  37. MovingUp,
  38. MovingDown,
  39. Error,
  40. }
  41. private DIAccessor _diUp;
  42. private DIAccessor _diDown;
  43. private DIAccessor _diMotionEnable;
  44. private DOAccessor _doUp;
  45. private DOAccessor _doDown;
  46. private DeviceState _state = DeviceState.Idle;
  47. private DeviceTimer _timer = new DeviceTimer();
  48. private SCConfigItem _scTimeout;
  49. public bool IsUp { get { return !_diDown.Value && _diUp.Value; } }
  50. public bool IsDown { get { return _diDown.Value && !_diUp.Value; } }
  51. public IoLift(string module, XmlElement node, string ioModule = "")
  52. {
  53. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  54. base.Name = node.GetAttribute("id");
  55. base.Display = node.GetAttribute("display");
  56. base.DeviceID = node.GetAttribute("schematicId");
  57. _diUp = ParseDiNode("diUp", node, ioModule);
  58. _diDown = ParseDiNode("diDown", node, ioModule);
  59. _diMotionEnable = ParseDiNode("diMotionEnable", node, ioModule);
  60. _doUp = ParseDoNode("doUp", node, ioModule);
  61. _doDown = ParseDoNode("doDown", node, ioModule);
  62. _scTimeout = ParseScNode("scTimeout", node);
  63. }
  64. public bool Initialize()
  65. {
  66. _state = DeviceState.Idle;
  67. DATA.Subscribe($"{Module}.{Name}.UpFeedback", ()=>_diUp.Value);
  68. DATA.Subscribe($"{Module}.{Name}.DownFeedback", () => _diDown.Value);
  69. DATA.Subscribe($"{Module}.{Name}.UpEnable", () => _diMotionEnable.Value);
  70. DATA.Subscribe($"{Module}.{Name}.DownEnable", () => _diMotionEnable.Value);
  71. DEVICE.Register($"{Module}.{Name}.MoveUp", (out string reason, int time, object[] param) =>
  72. {
  73. reason = "";
  74. return MoveUp(out reason);
  75. });
  76. DEVICE.Register($"{Module}.{Name}.MoveDown", (out string reason, int time, object[] param) =>
  77. {
  78. reason = "";
  79. return MoveDown(out reason);
  80. });
  81. return true;
  82. }
  83. public void Monitor()
  84. {
  85. switch (_state)
  86. {
  87. case DeviceState.MovingUp:
  88. if (IsUp)
  89. {
  90. if (!_doUp.SetValue(false, out string reason))
  91. {
  92. LOG.Error($"{Module} reset DO failed, {reason}");
  93. }
  94. _state = DeviceState.Idle;
  95. }
  96. else if (_timer.IsTimeout())
  97. {
  98. if (!_doUp.SetValue(false, out string reason))
  99. {
  100. LOG.Error($"{Module} reset DO failed, {reason}");
  101. }
  102. EV.PostAlarmLog(Module, $"{Module} {Name} Can not move up in {_scTimeout.IntValue} seconds");
  103. _state = DeviceState.Error;
  104. }
  105. break;
  106. case DeviceState.MovingDown:
  107. if (IsDown)
  108. {
  109. if (!_doDown.SetValue(false, out string reason))
  110. {
  111. LOG.Error($"{Module} reset DO failed, {reason}");
  112. }
  113. _state = DeviceState.Idle;
  114. }
  115. else if (_timer.IsTimeout())
  116. {
  117. if (!_doDown.SetValue(false, out string reason))
  118. {
  119. LOG.Error($"{Module} reset DO failed, {reason}");
  120. }
  121. EV.PostAlarmLog(Module, $"{Module} {Name} Can not move down in {_scTimeout.IntValue} seconds");
  122. _state = DeviceState.Error;
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. public void Terminate()
  130. {
  131. _doDown.SetValue(false, out _);
  132. _doUp.SetValue(false, out _);
  133. }
  134. public bool Move(bool up, out string reason)
  135. {
  136. if (up)
  137. return MoveUp(out reason);
  138. return MoveDown(out reason);
  139. }
  140. public bool MoveUp(out string reason)
  141. {
  142. if (!_diMotionEnable.Value)
  143. {
  144. reason = "Motion enable interlock triggered";
  145. return false;
  146. }
  147. if (!_doDown.SetValue(false, out reason) || !_doUp.SetValue(true, out reason))
  148. {
  149. _doDown.SetValue(false, out _);
  150. _doUp.SetValue(false, out _);
  151. return false;
  152. }
  153. _timer.Start(_scTimeout.IntValue * 1000);
  154. _state = DeviceState.MovingUp;
  155. return true;
  156. }
  157. public bool MoveDown(out string reason)
  158. {
  159. if (!_diMotionEnable.Value)
  160. {
  161. reason = "Motion enable interlock triggered";
  162. return false;
  163. }
  164. if (!_doDown.SetValue(true, out reason) || !_doUp.SetValue(false, out reason))
  165. {
  166. _doDown.SetValue(false, out _);
  167. _doUp.SetValue(false, out _);
  168. return false;
  169. }
  170. _timer.Start(_scTimeout.IntValue * 1000);
  171. _state = DeviceState.MovingDown;
  172. return true;
  173. }
  174. public void Reset()
  175. {
  176. }
  177. }
  178. }