RoutineRunner.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using Aitex.Core.RT.Log;
  6. using MECF.Framework.Common.Equipment;
  7. using Venus_Core;
  8. namespace MECF.Framework.Common.Routine
  9. {
  10. public class RoutineRunner
  11. {
  12. public RState Status => _runnerState;
  13. private bool IsSubStepRunning(int id) => _runnerState == RState.Running && _subState == RState.Running && _curStep == id;
  14. private bool bAllowSubStepStart(int id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode == false;
  15. private bool bAllowLoopSubStepStart(int id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode;
  16. public int LoopCounter => _loopCounter;
  17. public long StepElapsedMS => _subStepTimer.ElapsedMilliseconds;
  18. private RState _subState = RState.Init;
  19. private Stack<int> _steps = new Stack<int>();
  20. private Stack<int> _loopSteps = new Stack<int>();
  21. private int _curStep = int.MaxValue;
  22. private RState _runnerState = RState.Init;
  23. private string _name;
  24. private string _loopName;
  25. private ModuleName _module;
  26. // 缺省最大超时 10 分钟, delay 5 秒
  27. private const int _defaultTimeout = 600000;
  28. private const int _defaultDelay = 5000;
  29. Stopwatch _subStepTimer = new Stopwatch();
  30. private bool _isLoopMode = false;
  31. private int _loopCounterSP = 0;
  32. private int _loopCounter = 0;
  33. static private int _RunnerToken = 0;
  34. public RoutineRunner()
  35. {
  36. _runnerState = RState.Init;
  37. }
  38. public void Reset()
  39. {
  40. _runnerState = RState.Init;
  41. _subState = RState.Init;
  42. _isLoopMode = false;
  43. _steps.Clear();
  44. _loopSteps.Clear();
  45. _subStepTimer.Reset();
  46. }
  47. public RState Start(ModuleName module, string name)
  48. {
  49. Reset();
  50. _module = module;
  51. _name = $"{name} (Token:{_RunnerToken++})";
  52. _runnerState = RState.Running;
  53. _subState = RState.End;
  54. Notify("开始");
  55. return _runnerState;
  56. }
  57. public void Stop(string reason)
  58. {
  59. Alarm(reason);
  60. _runnerState = _subState = RState.Failed;
  61. }
  62. public RoutineRunner Run(int id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  63. {
  64. if (bAllowSubStepStart(id))
  65. {
  66. startNewStep(id, action);
  67. }
  68. else if(IsSubStepRunning(id))
  69. {
  70. if (condition())
  71. {
  72. _subState = RState.End;
  73. }
  74. else if(_subStepTimer.ElapsedMilliseconds >= timeout)
  75. {
  76. Alarm($"Step:{id} 超时");
  77. _runnerState = _subState = RState.Timeout;
  78. _subStepTimer.Reset();
  79. }
  80. }
  81. return this;
  82. }
  83. public RoutineRunner Run(int id, Func<bool> action, int delayMS = _defaultDelay)
  84. {
  85. if (bAllowSubStepStart(id))
  86. {
  87. startNewStep(id, action);
  88. }
  89. else if (IsSubStepRunning(id))
  90. {
  91. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  92. {
  93. _subState = RState.End;
  94. }
  95. }
  96. return this;
  97. }
  98. public RoutineRunner End(int id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  99. {
  100. if (bAllowSubStepStart(id))
  101. {
  102. startNewStep(id, action);
  103. }
  104. else if (IsSubStepRunning(id))
  105. {
  106. if (condition())
  107. {
  108. Notify($"结束");
  109. _runnerState = RState.End;
  110. _subState = RState.End;
  111. }
  112. else
  113. {
  114. if (_subStepTimer.ElapsedMilliseconds > timeout)
  115. {
  116. Alarm($"Step:{id} 超时");
  117. _runnerState = RState.Failed;
  118. _subState = RState.Timeout;
  119. }
  120. }
  121. }
  122. return this;
  123. }
  124. public RoutineRunner End(int id, Func<bool> action, int delayMS = _defaultDelay)
  125. {
  126. if (bAllowSubStepStart(id))
  127. {
  128. startNewStep(id, action);
  129. }
  130. else if (IsSubStepRunning(id))
  131. {
  132. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  133. {
  134. Notify($"结束");
  135. _runnerState = RState.End;
  136. _subState = RState.End;
  137. }
  138. }
  139. return this;
  140. }
  141. public RoutineRunner Delay(int id, int delayMS = _defaultDelay)
  142. {
  143. return Run(id, () => { return true; }, delayMS);
  144. }
  145. public RoutineRunner Wait(int id, Func<bool> condition, int timeout = _defaultTimeout)
  146. {
  147. return Run(id, () => { return true; }, condition, timeout);
  148. }
  149. public RoutineRunner LoopStart(int id, string name, int cycleCount, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  150. {
  151. if(bAllowSubStepStart(id))
  152. {
  153. _loopName = name;
  154. _loopCounterSP = cycleCount;
  155. _loopCounter = 0;
  156. _isLoopMode = true;
  157. startNewLoopStep(id, action);
  158. }
  159. else if(bAllowLoopSubStepStart(id))
  160. {
  161. startNewLoopStep(id, action);
  162. }
  163. else if(IsSubStepRunning(id))
  164. {
  165. if (condition())
  166. {
  167. _subState = RState.End;
  168. }
  169. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  170. {
  171. Alarm($"Step:{id} 超时");
  172. _runnerState = _subState = RState.Timeout;
  173. _subStepTimer.Reset();
  174. }
  175. }
  176. return this;
  177. }
  178. public RoutineRunner LoopStart(int id, string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  179. {
  180. if (bAllowSubStepStart(id))
  181. {
  182. _loopName = name;
  183. _loopCounterSP = cycleCount;
  184. _loopCounter = 0;
  185. _isLoopMode = true;
  186. startNewLoopStep(id, action);
  187. }
  188. else if (bAllowLoopSubStepStart(id))
  189. {
  190. startNewLoopStep(id, action);
  191. }
  192. else if (IsSubStepRunning(id))
  193. {
  194. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  195. {
  196. _subState = RState.End;
  197. }
  198. }
  199. return this;
  200. }
  201. public RoutineRunner LoopRun(int id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  202. {
  203. if (bAllowLoopSubStepStart(id))
  204. {
  205. startNewLoopStep(id, action);
  206. }
  207. else if (IsSubStepRunning(id))
  208. {
  209. if (condition())
  210. {
  211. _subState = RState.End;
  212. }
  213. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  214. {
  215. Alarm($"Step:{id} 超时");
  216. _runnerState = _subState = RState.Timeout;
  217. _subStepTimer.Reset();
  218. }
  219. }
  220. return this;
  221. }
  222. public RoutineRunner LoopRun(int id, Func<bool> action, int delayMS = _defaultDelay)
  223. {
  224. if (bAllowLoopSubStepStart(id))
  225. {
  226. startNewLoopStep(id, action);
  227. }
  228. else if (IsSubStepRunning(id))
  229. {
  230. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  231. {
  232. _subState = RState.End;
  233. }
  234. }
  235. return this;
  236. }
  237. public RoutineRunner LoopEnd(int id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  238. {
  239. if (bAllowLoopSubStepStart(id))
  240. {
  241. startNewLoopStep(id, action);
  242. }
  243. else if (IsSubStepRunning(id))
  244. {
  245. if (condition())
  246. {
  247. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  248. _subState = RState.End;
  249. _loopCounter++;
  250. if (_loopCounter >= _loopCounterSP)
  251. {
  252. foreach (var lid in _loopSteps)
  253. _steps.Push(lid);
  254. _loopSteps.Clear();
  255. _isLoopMode = false;
  256. }
  257. }
  258. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  259. {
  260. Alarm($"Step:{id} 超时");
  261. _runnerState = _subState = RState.Timeout;
  262. _subStepTimer.Reset();
  263. }
  264. }
  265. return this;
  266. }
  267. public RoutineRunner LoopEnd(int id, Func<bool> action, int delayMS = _defaultDelay)
  268. {
  269. if (bAllowLoopSubStepStart(id))
  270. {
  271. startNewLoopStep(id, action);
  272. }
  273. else if (IsSubStepRunning(id))
  274. {
  275. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  276. {
  277. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  278. _subState = RState.End;
  279. _loopCounter++;
  280. if (_loopCounter >= _loopCounterSP)
  281. {
  282. foreach (var lid in _loopSteps)
  283. _steps.Push(lid);
  284. _loopSteps.Clear();
  285. _isLoopMode = false;
  286. }
  287. }
  288. }
  289. return this;
  290. }
  291. public RoutineRunner LoopDelay(int id, int delayMS)
  292. {
  293. return LoopRun(id, () => { return true; }, delayMS);
  294. }
  295. public RoutineRunner LoopWait(int id, Func<bool> condition, int timeout = _defaultTimeout)
  296. {
  297. return LoopRun(id, () => { return true; }, condition, timeout);
  298. }
  299. private void startNewStep(int id, Func<bool> action)
  300. {
  301. if (action())
  302. {
  303. Notify($"Step: {id} start ---");
  304. _steps.Push(id);
  305. _curStep = id;
  306. _subState = RState.Running;
  307. _subStepTimer.Restart();
  308. }
  309. else
  310. {
  311. Alarm($"Step: {id} failed ");
  312. _runnerState = RState.Failed;
  313. }
  314. }
  315. private void startNewLoopStep(int id, Func<bool> action)
  316. {
  317. if (action())
  318. {
  319. Notify($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} start ---");
  320. if (!_loopSteps.Contains(id))
  321. _loopSteps.Push(id);
  322. _curStep = id;
  323. _subState = RState.Running;
  324. _subStepTimer.Restart();
  325. }
  326. else
  327. {
  328. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  329. _runnerState = RState.Failed;
  330. }
  331. }
  332. protected void Notify(string message)
  333. {
  334. LOG.Write(eEvent.EV_ROUTINE_NOTIFY, _module, _name, message);
  335. }
  336. protected void Alarm(string message)
  337. {
  338. LOG.Write(eEvent.ERR_ROUTINE_FAILED, _module, _name, message);
  339. }
  340. }
  341. static public class HOFs
  342. {
  343. public static Func<R> Apply<T1, R>(this Func<T1, R> func, T1 t1)
  344. => () => func(t1);
  345. public static Func<R> Apply<T1, T2, R>(this Func<T1, T2, R> func, T1 t1, T2 t2)
  346. => () => func(t1, t2);
  347. public static Func<R> Apply<T1, T2, T3, R>(this Func<T1, T2, T3, R> func, T1 t1, T2 t2, T3 t3)
  348. => () => func(t1, t2, t3);
  349. public static Func<bool> WrapAction(this Action action)
  350. => () => { action(); return true; };
  351. public static Func<bool> WrapAction<T1>(this Action<T1> action, T1 t1)
  352. => () => { action(t1); return true; };
  353. public static Func<bool> WrapAction<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
  354. => () => { action(t1, t2); return true; };
  355. public static Func<bool> WrapAction<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
  356. => () => { action(t1, t2, t3); return true; };
  357. }
  358. }