RoutineRunner.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. #pragma warning disable 0414
  8. namespace MECF.Framework.Common.Routine
  9. {
  10. public class RoutineRunner
  11. {
  12. public enum State
  13. {
  14. Init,
  15. Running,
  16. End,
  17. Failed,
  18. Timeout,
  19. }
  20. private State _subState = State.Init;
  21. private Stack<int> _step = new Stack<int>();
  22. private int _curStep = int.MaxValue;
  23. private State _runnerState = State.Init;
  24. Stopwatch _subStepTimer = new Stopwatch();
  25. public RoutineRunner()
  26. {
  27. _runnerState = State.Init;
  28. }
  29. public void Reset()
  30. {
  31. _runnerState = State.Init;
  32. _subState = State.Init;
  33. _step.Clear();
  34. }
  35. public RoutineRunner Start(int id, Func<bool> action, Func<bool> condition, int timeout)
  36. {
  37. if (!_step.Contains(id))
  38. {
  39. _runnerState = State.Running;
  40. startNewStep(id, action);
  41. }
  42. else
  43. {
  44. if (_subState == State.Running)
  45. {
  46. if(condition())
  47. {
  48. _subState = State.End;
  49. }
  50. else
  51. {
  52. if (_subStepTimer.ElapsedMilliseconds > timeout)
  53. {
  54. _runnerState = State.Failed;
  55. _subState = State.Timeout;
  56. }
  57. }
  58. }
  59. }
  60. return this;
  61. }
  62. public RoutineRunner Start(int id, Func<bool> action, int timeout)
  63. {
  64. if (!_step.Contains(id))
  65. {
  66. _runnerState = State.Running;
  67. startNewStep(id, action);
  68. }
  69. else
  70. {
  71. if (_subStepTimer.ElapsedMilliseconds > timeout)
  72. {
  73. _subState = State.End;
  74. }
  75. }
  76. return this;
  77. }
  78. public RoutineRunner Continue(int id, Func<bool> action, Func<bool> condition, int timeout)
  79. {
  80. if (_subState == State.End && !_step.Contains(id))
  81. {
  82. startNewStep(id, action);
  83. }
  84. else if(_curStep == id && _subState == State.Running)
  85. {
  86. if (condition())
  87. {
  88. _subState = State.End;
  89. }
  90. else
  91. {
  92. if (_subStepTimer.ElapsedMilliseconds > timeout)
  93. {
  94. _runnerState = State.Failed;
  95. _subState = State.Timeout;
  96. }
  97. }
  98. }
  99. return this;
  100. }
  101. public RoutineRunner Continue(int id, Func<bool> action, int timeout)
  102. {
  103. if (_subState == State.End && !_step.Contains(id))
  104. {
  105. startNewStep(id, action);
  106. }
  107. else if (_curStep == id && _subState == State.Running)
  108. {
  109. if (_subStepTimer.ElapsedMilliseconds > timeout)
  110. {
  111. _subState = State.End;
  112. }
  113. }
  114. return this;
  115. }
  116. public RoutineRunner End(int id, Func<bool> action, Func<bool> condition, int timeout)
  117. {
  118. if (_subState == State.End && !_step.Contains(id))
  119. {
  120. startNewStep(id, action);
  121. }
  122. else if (_curStep == id && _subState == State.Running)
  123. {
  124. if (condition())
  125. {
  126. _runnerState = State.End;
  127. _subState = State.End;
  128. }
  129. else
  130. {
  131. if (_subStepTimer.ElapsedMilliseconds > timeout)
  132. {
  133. _runnerState = State.Failed;
  134. _subState = State.Timeout;
  135. }
  136. }
  137. }
  138. return this;
  139. }
  140. public RoutineRunner End(int id, Func<bool> action, int timeout)
  141. {
  142. if (_subState == State.End && !_step.Contains(id))
  143. {
  144. startNewStep(id, action);
  145. }
  146. else if (_curStep == id && _subState == State.Running)
  147. {
  148. if (_subStepTimer.ElapsedMilliseconds > timeout)
  149. {
  150. _runnerState = State.End;
  151. _subState = State.End;
  152. }
  153. }
  154. return this;
  155. }
  156. private void startNewStep(int id, Func<bool> action)
  157. {
  158. if (action())
  159. {
  160. _step.Push(id);
  161. _curStep = id;
  162. _subState = State.Running;
  163. _subStepTimer.Restart();
  164. }
  165. else
  166. {
  167. _runnerState = State.Failed;
  168. }
  169. }
  170. }
  171. }