RoutineRunner.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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 Aitex.Core.RT.Routine;
  8. using System.Threading;
  9. using PunkHPX8_Core;
  10. namespace MECF.Framework.Common.Routine
  11. {
  12. public class RoutineRunner
  13. {
  14. public RState Status => _runnerState;
  15. private bool IsSubStepRunning(Enum id) => _runnerState == RState.Running && _subState == RState.Running && (_curStep != null && _curStep.GetHashCode() == id.GetHashCode());
  16. private bool bAllowSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode == false;
  17. private bool bAllowLoopSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode;
  18. public int LoopCounter => _loopCounter;
  19. public long StepElapsedMS => _subStepTimer.ElapsedMilliseconds;
  20. private RState _subState = RState.Init;
  21. private Stack<Enum> _steps = new Stack<Enum>();
  22. private Stack<Enum> _loopSteps = new Stack<Enum>();
  23. enum InvalidStep { Invalid = 0x7FFFFFFF, };
  24. public Enum CurrentStep => _curStep;
  25. private Enum _curStep = InvalidStep.Invalid;
  26. private RState _runnerState = RState.Init;
  27. private string _name;
  28. private string _loopName;
  29. private string _module;
  30. // 缺省最大超时 10 分钟, delay 5 秒
  31. private const int _defaultTimeout = 600000;
  32. private const int _defaultDelay = 200;
  33. private const int _defaultDelay_5s = 5000;
  34. //用于计时
  35. Stopwatch _subStepTimer = new Stopwatch();
  36. private bool _isLoopMode = false;
  37. private int _loopCounterSP = 0;
  38. private int _loopCounter = 0;
  39. private int _subRoutineIndex = -1;
  40. private IRoutine _subRoutine = null;
  41. static private int _RunnerToken = 0;
  42. private Stopwatch _stopwatch = new Stopwatch();
  43. private string _errorMsg = "";
  44. public string ErrorMsg { get{ return _errorMsg; } set { _errorMsg = value; } }
  45. /// <summary>
  46. /// 完成时长
  47. /// </summary>
  48. public long ElapsedMS =>_stopwatch.ElapsedMilliseconds;
  49. public RoutineRunner()
  50. {
  51. _runnerState = RState.Init;
  52. }
  53. public void Reset()
  54. {
  55. _runnerState = RState.Init;
  56. _subState = RState.Init;
  57. _isLoopMode = false;
  58. _steps.Clear();
  59. _loopSteps.Clear();
  60. _stopwatch.Reset();
  61. _subStepTimer.Reset();
  62. _errorMsg = "";
  63. }
  64. public RState Start(ModuleName module, string name)
  65. {
  66. Reset();
  67. _module = module.ToString();
  68. _name = $"{name} (Token:{_RunnerToken++})";
  69. _runnerState = RState.Running;
  70. _subState = RState.End;
  71. _stopwatch.Restart();
  72. Notify("Start");
  73. return _runnerState;
  74. }
  75. public RState Start(string module, string name)
  76. {
  77. Reset();
  78. _module = module;
  79. _name = $"{name} (Token:{_RunnerToken++})";
  80. _runnerState = RState.Running;
  81. _subState = RState.End;
  82. _stopwatch.Restart();
  83. Notify("Start");
  84. return _runnerState;
  85. }
  86. /// <summary>
  87. /// 重试
  88. /// </summary>
  89. /// <param name="id"></param>
  90. /// <returns></returns>
  91. public RState Retry(Enum id,List<Enum> _preStepIds,string module, string name)
  92. {
  93. Reset();
  94. foreach (Enum item in _preStepIds)
  95. {
  96. _steps.Push(item);
  97. }
  98. _module = module;
  99. _name = $"{name} (Token:{_RunnerToken++})";
  100. _runnerState = RState.Running;
  101. _subState = RState.End;
  102. _stopwatch.Restart();
  103. Notify($"{id} Retry");
  104. _curStep = id;
  105. return _runnerState;
  106. }
  107. public void Stop(string reason,bool error=false)
  108. {
  109. if (Status == RState.Running)
  110. {
  111. Alarm(reason, error);
  112. }
  113. _runnerState = _subState = RState.Failed;
  114. _stopwatch.Stop();
  115. }
  116. public RoutineRunner Run(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout,bool error=true)
  117. {
  118. if (bAllowSubStepStart(id))
  119. {
  120. startNewStep(id, action,error);
  121. }
  122. else if(IsSubStepRunning(id))
  123. {
  124. if (condition())
  125. {
  126. _subState = RState.End;
  127. }
  128. else if(_subStepTimer.ElapsedMilliseconds >= timeout)
  129. {
  130. Alarm($"Step:{id} timeout",error);
  131. _runnerState = _subState = RState.Timeout;
  132. _subStepTimer.Reset();
  133. }
  134. }
  135. return this;
  136. }
  137. public RoutineRunner RunConditionSubRoutine(Enum id,Func<int> routeIndexAction, IRoutine[] routines, object[] objects)
  138. {
  139. if (bAllowSubStepStart(id))
  140. {
  141. StartNewStepConditionRoutine(id, routeIndexAction, routines, objects);
  142. }
  143. else if(IsSubStepRunning(id))
  144. {
  145. if(_subRoutine==null)
  146. {
  147. _subState = RState.End;
  148. }
  149. else
  150. {
  151. if (objects.Length > _subRoutineIndex && _subRoutineIndex != -1)
  152. {
  153. _subRoutine.Start(objects[_subRoutineIndex]);
  154. }
  155. else
  156. {
  157. _subRoutine.Start();
  158. }
  159. _subState = RState.End;
  160. }
  161. }
  162. return this;
  163. }
  164. public RoutineRunner WaitConditionSubRoutine(Enum id)
  165. {
  166. if(bAllowSubStepStart(id))
  167. {
  168. Notify($"Step: {id} start ---");
  169. _steps.Push(id);
  170. _curStep = id;
  171. _subState = RState.Running;
  172. _subStepTimer.Restart();
  173. }
  174. else if(IsSubStepRunning(id))
  175. {
  176. if(_subRoutine==null)
  177. {
  178. _subState= RState.End;
  179. _subRoutineIndex = -1;
  180. }
  181. else
  182. {
  183. _subState = _subRoutine.Monitor();
  184. if(_subState==RState.End||_subState==RState.Failed||_subState==RState.Timeout)
  185. {
  186. _subRoutine = null;
  187. _subRoutineIndex = -1;
  188. }
  189. }
  190. }
  191. return this;
  192. }
  193. public void StartNewStepConditionRoutine(Enum id,Func<int> routeIndexAction, IRoutine[] routines, object[] objects)
  194. {
  195. _subRoutineIndex = routeIndexAction();
  196. if (routines.Length>_subRoutineIndex&&_subRoutineIndex!=-1)
  197. {
  198. _subRoutine = routines[_subRoutineIndex];
  199. }
  200. else
  201. {
  202. _subRoutine = null;
  203. }
  204. Notify($"Step: {id} start ---");
  205. _steps.Push(id);
  206. _curStep = id;
  207. _subState = RState.Running;
  208. _subStepTimer.Restart();
  209. }
  210. public RoutineRunner Run(Enum id, Func<bool> action, Func<bool> condition,Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout,bool error=true)
  211. {
  212. if (bAllowSubStepStart(id))
  213. {
  214. startNewStep(id, action);
  215. }
  216. else if (IsSubStepRunning(id))
  217. {
  218. if (condition())
  219. {
  220. _subState = RState.End;
  221. }
  222. else if(failedCondition())
  223. {
  224. if (!skipToNext)
  225. {
  226. _runnerState = _subState = RState.Failed;
  227. }
  228. else
  229. {
  230. _subState = RState.End;
  231. }
  232. }
  233. else
  234. {
  235. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  236. {
  237. Alarm($"Step:{id} timeout", error);
  238. if (!skipToNext)
  239. {
  240. _runnerState = _subState = RState.Timeout;
  241. }
  242. else
  243. {
  244. _subState = RState.End;
  245. }
  246. _subStepTimer.Reset();
  247. }
  248. }
  249. }
  250. return this;
  251. }
  252. public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  253. {
  254. if(bRun)
  255. {
  256. return Run(id, action, condition, timeout);
  257. }
  258. return this;
  259. }
  260. public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout)
  261. {
  262. if (bRun)
  263. {
  264. return Run(id, action, condition, failedCondition,skipToNext,timeout);
  265. }
  266. return this;
  267. }
  268. public RoutineRunner Run(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  269. {
  270. if (bAllowSubStepStart(id))
  271. {
  272. startNewStep(id, action);
  273. }
  274. else if (IsSubStepRunning(id))
  275. {
  276. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  277. {
  278. _subState = RState.End;
  279. }
  280. }
  281. return this;
  282. }
  283. public RoutineRunner RunDelay(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  284. {
  285. if (bAllowSubStepStart(id))
  286. {
  287. startNewStep(id, () => { return true; });
  288. }
  289. else if (IsSubStepRunning(id))
  290. {
  291. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  292. {
  293. if (!action())
  294. {
  295. Alarm($"Step: {id} Failed ");
  296. _runnerState = _subState = RState.Failed;
  297. _subStepTimer.Reset();
  298. }
  299. else
  300. {
  301. _subState = RState.End;
  302. }
  303. }
  304. }
  305. return this;
  306. }
  307. public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  308. {
  309. if(bRun)
  310. {
  311. return Run(id, action, delayMS);
  312. }
  313. return this;
  314. }
  315. public RoutineRunner End(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  316. {
  317. if (bAllowSubStepStart(id))
  318. {
  319. startNewStep(id, action);
  320. }
  321. else if (IsSubStepRunning(id))
  322. {
  323. if (condition())
  324. {
  325. Notify($"End");
  326. _runnerState = RState.End;
  327. _subState = RState.End;
  328. }
  329. else
  330. {
  331. if (_subStepTimer.ElapsedMilliseconds > timeout)
  332. {
  333. Alarm($"Step:{id} timeout");
  334. _runnerState = RState.Failed;
  335. _subState = RState.Timeout;
  336. }
  337. }
  338. }
  339. return this;
  340. }
  341. public RoutineRunner End(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  342. {
  343. if (bAllowSubStepStart(id))
  344. {
  345. startNewStep(id, action);
  346. }
  347. else if (IsSubStepRunning(id))
  348. {
  349. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  350. {
  351. Notify($"End");
  352. _runnerState = RState.End;
  353. _subState = RState.End;
  354. _stopwatch.Stop();
  355. }
  356. }
  357. return this;
  358. }
  359. public RoutineRunner Delay(Enum id, int delayMS = _defaultDelay_5s)
  360. {
  361. return Run(id, () => { return true; }, delayMS);
  362. }
  363. public RoutineRunner DelayIf(Enum id, bool bRun, int delayMS = _defaultDelay_5s)
  364. {
  365. if (bRun)
  366. {
  367. return Run(id, () => { return true; }, delayMS);
  368. }
  369. return this;
  370. }
  371. public RoutineRunner Wait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  372. {
  373. return Run(id, () => { return true; }, condition, timeout);
  374. }
  375. public RoutineRunner WaitIf (Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
  376. {
  377. if (bRun)
  378. {
  379. return Run(id, () => { return true; }, condition, timeout);
  380. }
  381. return this;
  382. }
  383. public RoutineRunner WaitWithStopCondition(Enum id, Func<bool> condition, Func<bool> stopCondition, bool skipToNext = false,int timeout=_defaultTimeout,bool error=true)
  384. {
  385. return Run(id, () => { return true; }, condition, stopCondition,skipToNext,timeout,error);
  386. }
  387. public RoutineRunner WaitWithStopConditionIf(Enum id, bool bRun, Func<bool> condition, Func<bool> stopCondition,bool skipToNext=false, int timeout = _defaultTimeout, bool error = true)
  388. {
  389. if (bRun)
  390. {
  391. return Run(id, () => { return true; }, condition, stopCondition,skipToNext, timeout, error);
  392. }
  393. return this;
  394. }
  395. public RoutineRunner LoopWait(Enum id,Func<bool> action,Func<bool> condition,Func<bool> fail)
  396. {
  397. if (bAllowSubStepStart(id))
  398. {
  399. startNewStep(id, action);
  400. }
  401. else if (IsSubStepRunning(id))
  402. {
  403. if(condition())
  404. {
  405. _subState = RState.End;
  406. }
  407. else if(fail())
  408. {
  409. Notify($"End");
  410. _runnerState = RState.End;
  411. _subState = RState.End;
  412. }
  413. }
  414. return this;
  415. }
  416. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  417. {
  418. if(bAllowSubStepStart(id))
  419. {
  420. _loopName = name;
  421. _loopCounterSP = cycleCount;
  422. _loopCounter = 0;
  423. _isLoopMode = true;
  424. startNewLoopStep(id, action);
  425. }
  426. else if(bAllowLoopSubStepStart(id))
  427. {
  428. startNewLoopStep(id, action);
  429. }
  430. else if(IsSubStepRunning(id))
  431. {
  432. if (condition())
  433. {
  434. _subState = RState.End;
  435. }
  436. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  437. {
  438. Alarm($"Step:{id} timeout");
  439. _runnerState = _subState = RState.Timeout;
  440. _subStepTimer.Reset();
  441. }
  442. }
  443. return this;
  444. }
  445. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  446. {
  447. if (bAllowSubStepStart(id))
  448. {
  449. _loopName = name;
  450. _loopCounterSP = cycleCount;
  451. _loopCounter = 0;
  452. _isLoopMode = true;
  453. startNewLoopStep(id, action);
  454. }
  455. else if (bAllowLoopSubStepStart(id))
  456. {
  457. startNewLoopStep(id, action);
  458. }
  459. else if (IsSubStepRunning(id))
  460. {
  461. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  462. {
  463. _subState = RState.End;
  464. }
  465. }
  466. return this;
  467. }
  468. public RoutineRunner LoopRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  469. {
  470. if (bAllowLoopSubStepStart(id))
  471. {
  472. startNewLoopStep(id, action);
  473. }
  474. else if (IsSubStepRunning(id))
  475. {
  476. if (condition())
  477. {
  478. _subState = RState.End;
  479. }
  480. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  481. {
  482. Alarm($"Step:{id} timeout");
  483. _runnerState = _subState = RState.Timeout;
  484. _subStepTimer.Reset();
  485. }
  486. }
  487. return this;
  488. }
  489. public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  490. {
  491. if(bRun)
  492. {
  493. return LoopRun(id, action, condition, timeout);
  494. }
  495. return this;
  496. }
  497. public RoutineRunner LoopRunIfWithStopStatus(Enum id, bool bRun,Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  498. {
  499. if (bRun)
  500. {
  501. if (bAllowLoopSubStepStart(id))
  502. {
  503. startNewLoopStep(id, () => { return true; });
  504. }
  505. else if (IsSubStepRunning(id))
  506. {
  507. if (condition())
  508. {
  509. _subState = RState.End;
  510. }
  511. else if (failedCondition())
  512. {
  513. _runnerState = _subState = RState.Failed;
  514. }
  515. else
  516. {
  517. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  518. {
  519. Alarm($"Step:{id} timeout", true);
  520. _runnerState = _subState = RState.Timeout;
  521. _subStepTimer.Reset();
  522. }
  523. }
  524. }
  525. }
  526. return this;
  527. }
  528. public RoutineRunner LoopRun(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  529. {
  530. if (bAllowLoopSubStepStart(id))
  531. {
  532. startNewLoopStep(id, action);
  533. }
  534. else if (IsSubStepRunning(id))
  535. {
  536. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  537. {
  538. _subState = RState.End;
  539. }
  540. }
  541. return this;
  542. }
  543. public RoutineRunner LoopRunOnlyTimeOutFault(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  544. {
  545. if (bAllowLoopSubStepStart(id))
  546. {
  547. startNewLoopStep(id, () => { return true; });
  548. }
  549. else if (IsSubStepRunning(id))
  550. {
  551. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  552. {
  553. if (!action())
  554. {
  555. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  556. _runnerState = _subState = RState.Failed;
  557. _subStepTimer.Reset();
  558. }
  559. else
  560. {
  561. _subState = RState.End;
  562. }
  563. }
  564. }
  565. return this;
  566. }
  567. public RoutineRunner LoopRunIfOnlyTimeOutFault(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  568. {
  569. if (bRun)
  570. {
  571. return LoopRunOnlyTimeOutFault(id, action, delayMS);
  572. }
  573. return this;
  574. }
  575. public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  576. {
  577. if(bRun)
  578. {
  579. return LoopRun(id, action, delayMS);
  580. }
  581. return this;
  582. }
  583. public RoutineRunner LoopRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout)
  584. {
  585. if (bAllowLoopSubStepStart(id))
  586. {
  587. startNewLoopStep(id, () => { return true; });
  588. }
  589. else if (IsSubStepRunning(id))
  590. {
  591. if (condition())
  592. {
  593. _subState = RState.End;
  594. }
  595. else if (failedCondition())
  596. {
  597. if (!skipToNext)
  598. {
  599. _runnerState = _subState = RState.Failed;
  600. }
  601. else
  602. {
  603. _subState = RState.End;
  604. }
  605. }
  606. else
  607. {
  608. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  609. {
  610. Alarm($"Step:{id} timeout", true);
  611. if (!skipToNext)
  612. {
  613. _runnerState = _subState = RState.Timeout;
  614. }
  615. else
  616. {
  617. _subState = RState.End;
  618. }
  619. _subStepTimer.Reset();
  620. }
  621. }
  622. }
  623. return this;
  624. }
  625. public RoutineRunner LoopEnd(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  626. {
  627. if (bAllowLoopSubStepStart(id))
  628. {
  629. startNewLoopStep(id, action);
  630. }
  631. else if (IsSubStepRunning(id))
  632. {
  633. if (condition())
  634. {
  635. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  636. _subState = RState.End;
  637. _loopCounter++;
  638. if (_loopCounter >= _loopCounterSP)
  639. {
  640. foreach (var lid in _loopSteps)
  641. _steps.Push(lid);
  642. _loopSteps.Clear();
  643. _isLoopMode = false;
  644. }
  645. }
  646. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  647. {
  648. Alarm($"Step:{id} timeout");
  649. _runnerState = _subState = RState.Timeout;
  650. _subStepTimer.Reset();
  651. }
  652. }
  653. return this;
  654. }
  655. public RoutineRunner LoopEnd(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  656. {
  657. if (bAllowLoopSubStepStart(id))
  658. {
  659. startNewLoopStep(id, action);
  660. }
  661. else if (IsSubStepRunning(id))
  662. {
  663. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  664. {
  665. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  666. _subState = RState.End;
  667. _loopCounter++;
  668. if (_loopCounter >= _loopCounterSP)
  669. {
  670. foreach (var lid in _loopSteps)
  671. _steps.Push(lid);
  672. _loopSteps.Clear();
  673. _isLoopMode = false;
  674. }
  675. }
  676. }
  677. return this;
  678. }
  679. public RoutineRunner LoopDelay(Enum id, int delayMS)
  680. {
  681. return LoopRun(id, () => { return true; }, delayMS);
  682. }
  683. public RoutineRunner LoopDelayIf(Enum id, bool bRun, int delayMS)
  684. {
  685. if (bRun)
  686. {
  687. return LoopRun(id, () => { return true; }, delayMS);
  688. }
  689. return this;
  690. }
  691. public RoutineRunner LoopRunDelay(Enum id, Func<bool> action, int delayMS)
  692. {
  693. return LoopRun(id, action, delayMS);
  694. }
  695. public RoutineRunner LoopWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  696. {
  697. return LoopRun(id, () => { return true; }, condition, timeout);
  698. }
  699. public RoutineRunner LoopWaitIf(Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
  700. {
  701. if (bRun)
  702. {
  703. return LoopRun(id, () => { return true; }, condition, timeout);
  704. }
  705. return this;
  706. }
  707. public RoutineRunner LoopRetryStart(Enum id,string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  708. {
  709. if (bAllowSubStepStart(id))
  710. {
  711. _loopName = name;
  712. _loopCounterSP = cycleCount;
  713. _loopCounter = 0;
  714. _isLoopMode = true;
  715. _loopSteps.Clear();
  716. startNewLoopStep(id, action);
  717. }
  718. else if (bAllowLoopSubStepStart(id)&&!_loopSteps.Contains(id))
  719. {
  720. startNewLoopStep(id, action);
  721. }
  722. else if (IsSubStepRunning(id))
  723. {
  724. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  725. {
  726. _subState = RState.End;
  727. }
  728. }
  729. return this;
  730. }
  731. public RoutineRunner LoopRetrySecondRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  732. {
  733. if (_loopCounter > _loopCounterSP)
  734. {
  735. return this;
  736. }
  737. if (bAllowLoopSubStepStart(id)&&_loopCounter>=1&&_loopSteps.Count!=0&&!_loopSteps.Contains(id))
  738. {
  739. startNewLoopStep(id, action);
  740. }
  741. else if (IsSubStepRunning(id))
  742. {
  743. if (condition())
  744. {
  745. _subState = RState.End;
  746. }
  747. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  748. {
  749. Alarm($"Step:{id} timeout");
  750. _runnerState = _subState = RState.Timeout;
  751. _subStepTimer.Reset();
  752. }
  753. }
  754. return this;
  755. }
  756. public RoutineRunner LoopRetrySecondRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  757. {
  758. if (_loopCounter > _loopCounterSP)
  759. {
  760. return this;
  761. }
  762. if (bAllowLoopSubStepStart(id)&&_loopCounter>=1 && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  763. {
  764. startNewLoopStep(id, () => { return true; });
  765. }
  766. else if (IsSubStepRunning(id))
  767. {
  768. if (condition())
  769. {
  770. _subState = RState.End;
  771. }
  772. else if (failedCondition())
  773. {
  774. _runnerState = _subState = RState.Failed;
  775. }
  776. else
  777. {
  778. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  779. {
  780. Alarm($"Step:{id} timeout", true);
  781. _runnerState = _subState = RState.Timeout;
  782. _subStepTimer.Reset();
  783. }
  784. }
  785. }
  786. return this;
  787. }
  788. public RoutineRunner LoopRetryRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  789. {
  790. if (_loopCounter > _loopCounterSP)
  791. {
  792. return this;
  793. }
  794. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  795. {
  796. startNewLoopStep(id, action);
  797. }
  798. else if (IsSubStepRunning(id))
  799. {
  800. if (condition())
  801. {
  802. _subState = RState.End;
  803. }
  804. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  805. {
  806. Alarm($"Step:{id} timeout");
  807. _runnerState = _subState = RState.Timeout;
  808. _subStepTimer.Reset();
  809. }
  810. }
  811. return this;
  812. }
  813. public RoutineRunner LoopRetryRunBack(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  814. {
  815. if (_loopCounter > _loopCounterSP)
  816. {
  817. return this;
  818. }
  819. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  820. {
  821. startNewLoopStep(id, action);
  822. }
  823. else if (IsSubStepRunning(id))
  824. {
  825. if (condition())
  826. {
  827. _subState = RState.End;
  828. }
  829. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  830. {
  831. _subState = RState.End;
  832. _loopCounter++;
  833. if (_loopCounter <= _loopCounterSP)
  834. {
  835. _loopSteps.Clear();
  836. }
  837. _subStepTimer.Reset();
  838. }
  839. }
  840. return this;
  841. }
  842. public RoutineRunner LoopRetryWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  843. {
  844. if (_loopCounter > _loopCounterSP)
  845. {
  846. return this;
  847. }
  848. return LoopRetryRun(id, () => { return true; }, condition, timeout);
  849. }
  850. public RoutineRunner LoopRetryWaitBack(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  851. {
  852. if (_loopCounter > _loopCounterSP)
  853. {
  854. return this;
  855. }
  856. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  857. {
  858. startNewLoopStep(id, () => { return true; });
  859. }
  860. else if (IsSubStepRunning(id))
  861. {
  862. if (condition())
  863. {
  864. _subState = RState.End;
  865. }
  866. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  867. {
  868. _subState = RState.End;
  869. _loopCounter++;
  870. if (_loopCounter <= _loopCounterSP)
  871. {
  872. _loopSteps.Clear();
  873. }
  874. _subStepTimer.Reset();
  875. }
  876. }
  877. return this;
  878. }
  879. public RoutineRunner LoopRetryDelay(Enum id, int delayMS)
  880. {
  881. return LoopRetryRun(id, () => { return true; },()=> { return true; }, delayMS);
  882. }
  883. public RoutineRunner LoopRetryRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  884. {
  885. if (_loopCounter > _loopCounterSP)
  886. {
  887. return this;
  888. }
  889. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  890. {
  891. startNewLoopStep(id, () => { return true; });
  892. }
  893. else if (IsSubStepRunning(id))
  894. {
  895. if (condition())
  896. {
  897. _subState = RState.End;
  898. }
  899. else if (failedCondition())
  900. {
  901. _runnerState = _subState = RState.Failed;
  902. }
  903. else
  904. {
  905. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  906. {
  907. Alarm($"Step:{id} timeout", true);
  908. _runnerState = _subState = RState.Timeout;
  909. _subStepTimer.Reset();
  910. }
  911. }
  912. }
  913. return this;
  914. }
  915. public RoutineRunner LoopRetryRunWithStopStatusBack(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  916. {
  917. if (_loopCounter > _loopCounterSP)
  918. {
  919. return this;
  920. }
  921. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  922. {
  923. startNewLoopStep(id, () => { return true; });
  924. }
  925. else if (IsSubStepRunning(id))
  926. {
  927. if (condition())
  928. {
  929. _subState = RState.End;
  930. }
  931. else if (failedCondition())
  932. {
  933. _subState = RState.End;
  934. _loopCounter++;
  935. if (_loopCounter <= _loopCounterSP)
  936. {
  937. _loopSteps.Clear();
  938. }
  939. _subStepTimer.Reset();
  940. }
  941. else
  942. {
  943. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  944. {
  945. _subState = RState.End;
  946. _loopCounter++;
  947. if (_loopCounter <= _loopCounterSP)
  948. {
  949. _loopSteps.Clear();
  950. }
  951. _subStepTimer.Reset();
  952. }
  953. }
  954. }
  955. return this;
  956. }
  957. public RoutineRunner LoopRetryEnd(Enum id, int delayMS = _defaultDelay)
  958. {
  959. if (bAllowLoopSubStepStart(id)&& _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  960. {
  961. startNewLoopStep(id, () => { return true; });
  962. }
  963. else if (IsSubStepRunning(id))
  964. {
  965. foreach (var lid in _loopSteps)
  966. _steps.Push(lid);
  967. if (_loopCounter > _loopCounterSP)
  968. {
  969. Alarm($"Step:{id} Retry times over {_loopCounterSP}");
  970. _runnerState = _subState = RState.Timeout;
  971. _subStepTimer.Reset();
  972. }
  973. else
  974. {
  975. _subState = RState.End;
  976. }
  977. _loopSteps.Clear();
  978. _isLoopMode = false;
  979. }
  980. return this;
  981. }
  982. private void startNewStep(Enum id, Func<bool> action,bool error=true)
  983. {
  984. if (action())
  985. {
  986. Notify($"Step: {id} start ---");
  987. _steps.Push(id);
  988. _curStep = id;
  989. _subState = RState.Running;
  990. _subStepTimer.Restart();
  991. }
  992. else
  993. {
  994. Alarm($"Step: {id} failed ",error);
  995. _runnerState = RState.Failed;
  996. }
  997. }
  998. private void startNewLoopStep(Enum id, Func<bool> action)
  999. {
  1000. if (action())
  1001. {
  1002. Notify($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} start ---");
  1003. if (!_loopSteps.Contains(id))
  1004. _loopSteps.Push(id);
  1005. _curStep = id;
  1006. _subState = RState.Running;
  1007. _subStepTimer.Restart();
  1008. }
  1009. else
  1010. {
  1011. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  1012. _runnerState = RState.Failed;
  1013. }
  1014. }
  1015. protected void Notify(string message)
  1016. {
  1017. LOG.WriteBackgroundLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name, message);
  1018. }
  1019. protected void Alarm(string message,bool error=true)
  1020. {
  1021. if (error)
  1022. {
  1023. _errorMsg= message;
  1024. LOG.WriteLog(eEvent.ERR_ROUTINE_FAILED, _module,_name,message);
  1025. }
  1026. else
  1027. {
  1028. LOG.WriteLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name,message);
  1029. }
  1030. }
  1031. }
  1032. }