RoutineRunner.cs 13 KB

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