RoutineRunner.cs 14 KB

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