RoutineRunner.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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.WriteLog(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. static public class HOFs
  1033. {
  1034. public static Func<R> Apply<T1, R>(this Func<T1, R> func, T1 t1)
  1035. => () => func(t1);
  1036. public static Func<R> Apply<T1, T2, R>(this Func<T1, T2, R> func, T1 t1, T2 t2)
  1037. => () => func(t1, t2);
  1038. public static Func<R> Apply<T1, T2, T3, R>(this Func<T1, T2, T3, R> func, T1 t1, T2 t2, T3 t3)
  1039. => () => func(t1, t2, t3);
  1040. public static Func<bool> WrapAction(this Action action)
  1041. => () => { action(); return true; };
  1042. public static Func<bool> WrapAction<T1>(this Action<T1> action, T1 t1)
  1043. => () => { action(t1); return true; };
  1044. public static Func<bool> WrapAction<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
  1045. => () => { action(t1, t2); return true; };
  1046. public static Func<bool> WrapAction<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
  1047. => () => { action(t1, t2, t3); return true; };
  1048. }
  1049. }