RoutineRunner.cs 13 KB

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