RoutineRunner.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 CyberX8_Core;
  8. using Aitex.Core.RT.Routine;
  9. using System.Threading;
  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("开始");
  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("开始");
  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} 超时",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, 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. _runnerState= _subState = RState.Failed;
  225. }
  226. else
  227. {
  228. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  229. {
  230. Alarm($"Step:{id} 超时", error);
  231. _runnerState = _subState = RState.Timeout;
  232. _subStepTimer.Reset();
  233. }
  234. }
  235. }
  236. return this;
  237. }
  238. public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  239. {
  240. if(bRun)
  241. {
  242. return Run(id, action, condition, timeout);
  243. }
  244. return this;
  245. }
  246. public RoutineRunner Run(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  247. {
  248. if (bAllowSubStepStart(id))
  249. {
  250. startNewStep(id, action);
  251. }
  252. else if (IsSubStepRunning(id))
  253. {
  254. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  255. {
  256. _subState = RState.End;
  257. }
  258. }
  259. return this;
  260. }
  261. public RoutineRunner RunDelay(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  262. {
  263. if (bAllowSubStepStart(id))
  264. {
  265. startNewStep(id, () => { return true; });
  266. }
  267. else if (IsSubStepRunning(id))
  268. {
  269. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  270. {
  271. if (!action())
  272. {
  273. Alarm($"Step: {id} Failed ");
  274. _runnerState = _subState = RState.Failed;
  275. _subStepTimer.Reset();
  276. }
  277. else
  278. {
  279. _subState = RState.End;
  280. }
  281. }
  282. }
  283. return this;
  284. }
  285. public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  286. {
  287. if(bRun)
  288. {
  289. return Run(id, action, delayMS);
  290. }
  291. return this;
  292. }
  293. public RoutineRunner End(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  294. {
  295. if (bAllowSubStepStart(id))
  296. {
  297. startNewStep(id, action);
  298. }
  299. else if (IsSubStepRunning(id))
  300. {
  301. if (condition())
  302. {
  303. Notify($"结束");
  304. _runnerState = RState.End;
  305. _subState = RState.End;
  306. }
  307. else
  308. {
  309. if (_subStepTimer.ElapsedMilliseconds > timeout)
  310. {
  311. Alarm($"Step:{id} 超时");
  312. _runnerState = RState.Failed;
  313. _subState = RState.Timeout;
  314. }
  315. }
  316. }
  317. return this;
  318. }
  319. public RoutineRunner End(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  320. {
  321. if (bAllowSubStepStart(id))
  322. {
  323. startNewStep(id, action);
  324. }
  325. else if (IsSubStepRunning(id))
  326. {
  327. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  328. {
  329. Notify($"结束");
  330. _runnerState = RState.End;
  331. _subState = RState.End;
  332. _stopwatch.Stop();
  333. }
  334. }
  335. return this;
  336. }
  337. public RoutineRunner Delay(Enum id, int delayMS = _defaultDelay_5s)
  338. {
  339. return Run(id, () => { return true; }, delayMS);
  340. }
  341. public RoutineRunner DelayIf(Enum id, bool bRun, int delayMS = _defaultDelay_5s)
  342. {
  343. if (bRun)
  344. {
  345. return Run(id, () => { return true; }, delayMS);
  346. }
  347. return this;
  348. }
  349. public RoutineRunner Wait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  350. {
  351. return Run(id, () => { return true; }, condition, timeout);
  352. }
  353. public RoutineRunner WaitIf (Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
  354. {
  355. if (bRun)
  356. {
  357. return Run(id, () => { return true; }, condition, timeout);
  358. }
  359. return this;
  360. }
  361. public RoutineRunner WaitWithStopCondition(Enum id, Func<bool> condition,Func<bool> stopCondition,int timeout=_defaultTimeout,bool error=true)
  362. {
  363. return Run(id, () => { return true; }, condition, stopCondition,timeout,error);
  364. }
  365. public RoutineRunner WaitWithStopConditionIf(Enum id, bool bRun, Func<bool> condition, Func<bool> stopCondition, int timeout = _defaultTimeout, bool error = true)
  366. {
  367. if (bRun)
  368. {
  369. return Run(id, () => { return true; }, condition, stopCondition, timeout, error);
  370. }
  371. return this;
  372. }
  373. public RoutineRunner LoopWait(Enum id,Func<bool> action,Func<bool> condition,Func<bool> fail)
  374. {
  375. if (bAllowSubStepStart(id))
  376. {
  377. startNewStep(id, action);
  378. }
  379. else if (IsSubStepRunning(id))
  380. {
  381. if(condition())
  382. {
  383. _subState = RState.End;
  384. }
  385. else if(fail())
  386. {
  387. Notify($"结束");
  388. _runnerState = RState.End;
  389. _subState = RState.End;
  390. }
  391. }
  392. return this;
  393. }
  394. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  395. {
  396. if(bAllowSubStepStart(id))
  397. {
  398. _loopName = name;
  399. _loopCounterSP = cycleCount;
  400. _loopCounter = 0;
  401. _isLoopMode = true;
  402. startNewLoopStep(id, action);
  403. }
  404. else if(bAllowLoopSubStepStart(id))
  405. {
  406. startNewLoopStep(id, action);
  407. }
  408. else if(IsSubStepRunning(id))
  409. {
  410. if (condition())
  411. {
  412. _subState = RState.End;
  413. }
  414. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  415. {
  416. Alarm($"Step:{id} 超时");
  417. _runnerState = _subState = RState.Timeout;
  418. _subStepTimer.Reset();
  419. }
  420. }
  421. return this;
  422. }
  423. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  424. {
  425. if (bAllowSubStepStart(id))
  426. {
  427. _loopName = name;
  428. _loopCounterSP = cycleCount;
  429. _loopCounter = 0;
  430. _isLoopMode = true;
  431. startNewLoopStep(id, action);
  432. }
  433. else if (bAllowLoopSubStepStart(id))
  434. {
  435. startNewLoopStep(id, action);
  436. }
  437. else if (IsSubStepRunning(id))
  438. {
  439. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  440. {
  441. _subState = RState.End;
  442. }
  443. }
  444. return this;
  445. }
  446. public RoutineRunner LoopRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  447. {
  448. if (bAllowLoopSubStepStart(id))
  449. {
  450. startNewLoopStep(id, action);
  451. }
  452. else if (IsSubStepRunning(id))
  453. {
  454. if (condition())
  455. {
  456. _subState = RState.End;
  457. }
  458. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  459. {
  460. Alarm($"Step:{id} 超时");
  461. _runnerState = _subState = RState.Timeout;
  462. _subStepTimer.Reset();
  463. }
  464. }
  465. return this;
  466. }
  467. public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  468. {
  469. if(bRun)
  470. {
  471. return LoopRun(id, action, condition, timeout);
  472. }
  473. return this;
  474. }
  475. public RoutineRunner LoopRunIfWithStopStatus(Enum id, bool bRun,Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  476. {
  477. if (bRun)
  478. {
  479. if (bAllowLoopSubStepStart(id))
  480. {
  481. startNewLoopStep(id, () => { return true; });
  482. }
  483. else if (IsSubStepRunning(id))
  484. {
  485. if (condition())
  486. {
  487. _subState = RState.End;
  488. }
  489. else if (failedCondition())
  490. {
  491. _runnerState = _subState = RState.Failed;
  492. }
  493. else
  494. {
  495. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  496. {
  497. Alarm($"Step:{id} 超时", true);
  498. _runnerState = _subState = RState.Timeout;
  499. _subStepTimer.Reset();
  500. }
  501. }
  502. }
  503. }
  504. return this;
  505. }
  506. public RoutineRunner LoopRun(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  507. {
  508. if (bAllowLoopSubStepStart(id))
  509. {
  510. startNewLoopStep(id, action);
  511. }
  512. else if (IsSubStepRunning(id))
  513. {
  514. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  515. {
  516. _subState = RState.End;
  517. }
  518. }
  519. return this;
  520. }
  521. public RoutineRunner LoopRunOnlyTimeOutFault(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  522. {
  523. if (bAllowLoopSubStepStart(id))
  524. {
  525. startNewLoopStep(id, () => { return true; });
  526. }
  527. else if (IsSubStepRunning(id))
  528. {
  529. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  530. {
  531. if (!action())
  532. {
  533. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  534. _runnerState = _subState = RState.Failed;
  535. _subStepTimer.Reset();
  536. }
  537. else
  538. {
  539. _subState = RState.End;
  540. }
  541. }
  542. }
  543. return this;
  544. }
  545. public RoutineRunner LoopRunIfOnlyTimeOutFault(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  546. {
  547. if (bRun)
  548. {
  549. return LoopRunOnlyTimeOutFault(id, action, delayMS);
  550. }
  551. return this;
  552. }
  553. public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
  554. {
  555. if(bRun)
  556. {
  557. return LoopRun(id, action, delayMS);
  558. }
  559. return this;
  560. }
  561. public RoutineRunner LoopRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  562. {
  563. if (bAllowLoopSubStepStart(id))
  564. {
  565. startNewLoopStep(id, () => { return true; });
  566. }
  567. else if (IsSubStepRunning(id))
  568. {
  569. if (condition())
  570. {
  571. _subState = RState.End;
  572. }
  573. else if (failedCondition())
  574. {
  575. _runnerState = _subState = RState.Failed;
  576. }
  577. else
  578. {
  579. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  580. {
  581. Alarm($"Step:{id} 超时", true);
  582. _runnerState = _subState = RState.Timeout;
  583. _subStepTimer.Reset();
  584. }
  585. }
  586. }
  587. return this;
  588. }
  589. public RoutineRunner LoopEnd(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  590. {
  591. if (bAllowLoopSubStepStart(id))
  592. {
  593. startNewLoopStep(id, action);
  594. }
  595. else if (IsSubStepRunning(id))
  596. {
  597. if (condition())
  598. {
  599. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  600. _subState = RState.End;
  601. _loopCounter++;
  602. if (_loopCounter >= _loopCounterSP)
  603. {
  604. foreach (var lid in _loopSteps)
  605. _steps.Push(lid);
  606. _loopSteps.Clear();
  607. _isLoopMode = false;
  608. }
  609. }
  610. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  611. {
  612. Alarm($"Step:{id} 超时");
  613. _runnerState = _subState = RState.Timeout;
  614. _subStepTimer.Reset();
  615. }
  616. }
  617. return this;
  618. }
  619. public RoutineRunner LoopEnd(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  620. {
  621. if (bAllowLoopSubStepStart(id))
  622. {
  623. startNewLoopStep(id, action);
  624. }
  625. else if (IsSubStepRunning(id))
  626. {
  627. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  628. {
  629. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  630. _subState = RState.End;
  631. _loopCounter++;
  632. if (_loopCounter >= _loopCounterSP)
  633. {
  634. foreach (var lid in _loopSteps)
  635. _steps.Push(lid);
  636. _loopSteps.Clear();
  637. _isLoopMode = false;
  638. }
  639. }
  640. }
  641. return this;
  642. }
  643. public RoutineRunner LoopDelay(Enum id, int delayMS)
  644. {
  645. return LoopRun(id, () => { return true; }, delayMS);
  646. }
  647. public RoutineRunner LoopRunDelay(Enum id, Func<bool> action, int delayMS)
  648. {
  649. return LoopRun(id, action, delayMS);
  650. }
  651. public RoutineRunner LoopWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  652. {
  653. return LoopRun(id, () => { return true; }, condition, timeout);
  654. }
  655. public RoutineRunner LoopWaitIf(Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
  656. {
  657. if (bRun)
  658. {
  659. return LoopRun(id, () => { return true; }, condition, timeout);
  660. }
  661. return this;
  662. }
  663. public RoutineRunner LoopRetryStart(Enum id,string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  664. {
  665. if (bAllowSubStepStart(id))
  666. {
  667. _loopName = name;
  668. _loopCounterSP = cycleCount;
  669. _loopCounter = 0;
  670. _isLoopMode = true;
  671. _loopSteps.Clear();
  672. startNewLoopStep(id, action);
  673. }
  674. else if (bAllowLoopSubStepStart(id)&&!_loopSteps.Contains(id))
  675. {
  676. startNewLoopStep(id, action);
  677. }
  678. else if (IsSubStepRunning(id))
  679. {
  680. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  681. {
  682. _subState = RState.End;
  683. }
  684. }
  685. return this;
  686. }
  687. public RoutineRunner LoopRetrySecondRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  688. {
  689. if (_loopCounter > _loopCounterSP)
  690. {
  691. return this;
  692. }
  693. if (bAllowLoopSubStepStart(id)&&_loopCounter>=1&&_loopSteps.Count!=0&&!_loopSteps.Contains(id))
  694. {
  695. startNewLoopStep(id, action);
  696. }
  697. else if (IsSubStepRunning(id))
  698. {
  699. if (condition())
  700. {
  701. _subState = RState.End;
  702. }
  703. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  704. {
  705. Alarm($"Step:{id} 超时");
  706. _runnerState = _subState = RState.Timeout;
  707. _subStepTimer.Reset();
  708. }
  709. }
  710. return this;
  711. }
  712. public RoutineRunner LoopRetrySecondRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  713. {
  714. if (_loopCounter > _loopCounterSP)
  715. {
  716. return this;
  717. }
  718. if (bAllowLoopSubStepStart(id)&&_loopCounter>=1 && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  719. {
  720. startNewLoopStep(id, () => { return true; });
  721. }
  722. else if (IsSubStepRunning(id))
  723. {
  724. if (condition())
  725. {
  726. _subState = RState.End;
  727. }
  728. else if (failedCondition())
  729. {
  730. _runnerState = _subState = RState.Failed;
  731. }
  732. else
  733. {
  734. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  735. {
  736. Alarm($"Step:{id} 超时", true);
  737. _runnerState = _subState = RState.Timeout;
  738. _subStepTimer.Reset();
  739. }
  740. }
  741. }
  742. return this;
  743. }
  744. public RoutineRunner LoopRetryRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  745. {
  746. if (_loopCounter > _loopCounterSP)
  747. {
  748. return this;
  749. }
  750. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  751. {
  752. startNewLoopStep(id, action);
  753. }
  754. else if (IsSubStepRunning(id))
  755. {
  756. if (condition())
  757. {
  758. _subState = RState.End;
  759. }
  760. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  761. {
  762. Alarm($"Step:{id} 超时");
  763. _runnerState = _subState = RState.Timeout;
  764. _subStepTimer.Reset();
  765. }
  766. }
  767. return this;
  768. }
  769. public RoutineRunner LoopRetryRunBack(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  770. {
  771. if (_loopCounter > _loopCounterSP)
  772. {
  773. return this;
  774. }
  775. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  776. {
  777. startNewLoopStep(id, action);
  778. }
  779. else if (IsSubStepRunning(id))
  780. {
  781. if (condition())
  782. {
  783. _subState = RState.End;
  784. }
  785. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  786. {
  787. _subState = RState.End;
  788. _loopCounter++;
  789. if (_loopCounter <= _loopCounterSP)
  790. {
  791. _loopSteps.Clear();
  792. }
  793. _subStepTimer.Reset();
  794. }
  795. }
  796. return this;
  797. }
  798. public RoutineRunner LoopRetryWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  799. {
  800. if (_loopCounter > _loopCounterSP)
  801. {
  802. return this;
  803. }
  804. return LoopRetryRun(id, () => { return true; }, condition, timeout);
  805. }
  806. public RoutineRunner LoopRetryWaitBack(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  807. {
  808. if (_loopCounter > _loopCounterSP)
  809. {
  810. return this;
  811. }
  812. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  813. {
  814. startNewLoopStep(id, () => { return true; });
  815. }
  816. else if (IsSubStepRunning(id))
  817. {
  818. if (condition())
  819. {
  820. _subState = RState.End;
  821. }
  822. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  823. {
  824. _subState = RState.End;
  825. _loopCounter++;
  826. if (_loopCounter <= _loopCounterSP)
  827. {
  828. _loopSteps.Clear();
  829. }
  830. _subStepTimer.Reset();
  831. }
  832. }
  833. return this;
  834. }
  835. public RoutineRunner LoopRetryDelay(Enum id, int delayMS)
  836. {
  837. return LoopRetryRun(id, () => { return true; },()=> { return true; }, delayMS);
  838. }
  839. public RoutineRunner LoopRetryRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  840. {
  841. if (_loopCounter > _loopCounterSP)
  842. {
  843. return this;
  844. }
  845. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  846. {
  847. startNewLoopStep(id, () => { return true; });
  848. }
  849. else if (IsSubStepRunning(id))
  850. {
  851. if (condition())
  852. {
  853. _subState = RState.End;
  854. }
  855. else if (failedCondition())
  856. {
  857. _runnerState = _subState = RState.Failed;
  858. }
  859. else
  860. {
  861. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  862. {
  863. Alarm($"Step:{id} 超时", true);
  864. _runnerState = _subState = RState.Timeout;
  865. _subStepTimer.Reset();
  866. }
  867. }
  868. }
  869. return this;
  870. }
  871. public RoutineRunner LoopRetryRunWithStopStatusBack(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
  872. {
  873. if (_loopCounter > _loopCounterSP)
  874. {
  875. return this;
  876. }
  877. if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  878. {
  879. startNewLoopStep(id, () => { return true; });
  880. }
  881. else if (IsSubStepRunning(id))
  882. {
  883. if (condition())
  884. {
  885. _subState = RState.End;
  886. }
  887. else if (failedCondition())
  888. {
  889. _subState = RState.End;
  890. _loopCounter++;
  891. if (_loopCounter <= _loopCounterSP)
  892. {
  893. _loopSteps.Clear();
  894. }
  895. _subStepTimer.Reset();
  896. }
  897. else
  898. {
  899. if (_subStepTimer.ElapsedMilliseconds >= timeout)
  900. {
  901. _subState = RState.End;
  902. _loopCounter++;
  903. if (_loopCounter <= _loopCounterSP)
  904. {
  905. _loopSteps.Clear();
  906. }
  907. _subStepTimer.Reset();
  908. }
  909. }
  910. }
  911. return this;
  912. }
  913. public RoutineRunner LoopRetryEnd(Enum id, int delayMS = _defaultDelay)
  914. {
  915. if (bAllowLoopSubStepStart(id)&& _loopSteps.Count != 0 && !_loopSteps.Contains(id))
  916. {
  917. startNewLoopStep(id, () => { return true; });
  918. }
  919. else if (IsSubStepRunning(id))
  920. {
  921. foreach (var lid in _loopSteps)
  922. _steps.Push(lid);
  923. if (_loopCounter > _loopCounterSP)
  924. {
  925. Alarm($"Step:{id} Retry times over {_loopCounterSP}");
  926. _runnerState = _subState = RState.Timeout;
  927. _subStepTimer.Reset();
  928. }
  929. else
  930. {
  931. _subState = RState.End;
  932. }
  933. _loopSteps.Clear();
  934. _isLoopMode = false;
  935. }
  936. return this;
  937. }
  938. private void startNewStep(Enum id, Func<bool> action,bool error=true)
  939. {
  940. if (action())
  941. {
  942. Notify($"Step: {id} start ---");
  943. _steps.Push(id);
  944. _curStep = id;
  945. _subState = RState.Running;
  946. _subStepTimer.Restart();
  947. }
  948. else
  949. {
  950. Alarm($"Step: {id} failed ",error);
  951. _runnerState = RState.Failed;
  952. }
  953. }
  954. private void startNewLoopStep(Enum id, Func<bool> action)
  955. {
  956. if (action())
  957. {
  958. Notify($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} start ---");
  959. if (!_loopSteps.Contains(id))
  960. _loopSteps.Push(id);
  961. _curStep = id;
  962. _subState = RState.Running;
  963. _subStepTimer.Restart();
  964. }
  965. else
  966. {
  967. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  968. _runnerState = RState.Failed;
  969. }
  970. }
  971. protected void Notify(string message)
  972. {
  973. LOG.WriteLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name, message);
  974. }
  975. protected void Alarm(string message,bool error=true)
  976. {
  977. if (error)
  978. {
  979. _errorMsg= message;
  980. LOG.WriteLog(eEvent.ERR_ROUTINE_FAILED, _module,_name,message);
  981. }
  982. else
  983. {
  984. LOG.WriteLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name,message);
  985. }
  986. }
  987. }
  988. static public class HOFs
  989. {
  990. public static Func<R> Apply<T1, R>(this Func<T1, R> func, T1 t1)
  991. => () => func(t1);
  992. public static Func<R> Apply<T1, T2, R>(this Func<T1, T2, R> func, T1 t1, T2 t2)
  993. => () => func(t1, t2);
  994. public static Func<R> Apply<T1, T2, T3, R>(this Func<T1, T2, T3, R> func, T1 t1, T2 t2, T3 t3)
  995. => () => func(t1, t2, t3);
  996. public static Func<bool> WrapAction(this Action action)
  997. => () => { action(); return true; };
  998. public static Func<bool> WrapAction<T1>(this Action<T1> action, T1 t1)
  999. => () => { action(t1); return true; };
  1000. public static Func<bool> WrapAction<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
  1001. => () => { action(t1, t2); return true; };
  1002. public static Func<bool> WrapAction<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
  1003. => () => { action(t1, t2, t3); return true; };
  1004. }
  1005. }