SeqenecRoutine.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. namespace Aitex.Core.RT.Routine
  10. {
  11. public class SeqenecRoutine
  12. {
  13. //timer, 计算routine时间
  14. protected DeviceTimer counter = new DeviceTimer();
  15. protected DeviceTimer delayTimer = new DeviceTimer();
  16. protected enum STATE
  17. {
  18. IDLE,
  19. WAIT,
  20. }
  21. public int TokenId
  22. {
  23. get { return _currentStepId; }
  24. }
  25. protected int _currentStepId; //step index
  26. /// <summary>
  27. /// already done steps
  28. /// </summary>
  29. protected List<int> _historySteps = new List<int>();
  30. /// <summary>
  31. /// wait run steps
  32. /// </summary>
  33. protected List<int> _waitSteps = new List<int>();
  34. protected STATE _stepState; //step state //idel,wait,
  35. //loop control
  36. protected int _loopCounter = 0;
  37. protected int _loopTotalCountSetting = 0;
  38. protected int _loopStepStartId = 0;
  39. protected DeviceTimer _timer = new DeviceTimer();
  40. public int LoopCounter { get { return _loopCounter; } }
  41. public int LoopTotalTime { get { return _loopTotalCountSetting; } }
  42. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  43. //状态持续时间,单位为秒
  44. public int Elapsed { get { return (int)(_timer.GetElapseTime() / 1000); } }
  45. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  46. public void Reset()
  47. {
  48. _currentStepId = -1;
  49. _historySteps.Clear();
  50. _waitSteps.Clear();
  51. _loopCounter = 0;
  52. _loopTotalCountSetting = 0;
  53. _stepState = STATE.IDLE;
  54. counter.Start(60*60*100); //默认1小时
  55. RoutineToken.Result = RoutineState.Running;
  56. }
  57. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  58. {
  59. if (!ActiveStep(id))
  60. return;
  61. result.Result = execution();
  62. }
  63. #region interface
  64. public void StopLoop()
  65. {
  66. _loopCounter = _loopTotalCountSetting;
  67. }
  68. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  69. {
  70. int idx = Convert.ToInt32(id);
  71. bool bActive = ActiveStep(idx);
  72. if (bActive)
  73. {
  74. if (!func())
  75. {
  76. return Tuple.Create(bActive, Result.FAIL); //执行错误
  77. }
  78. _loopStepStartId = idx;
  79. _loopTotalCountSetting = count;
  80. NextStep();
  81. return Tuple.Create(true, Result.RUN);
  82. }
  83. return Tuple.Create(false, Result.RUN);
  84. }
  85. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  86. {
  87. int idx = Convert.ToInt32(id);
  88. bool bActive = ActiveStep(idx);
  89. if (bActive)
  90. {
  91. if (++_loopCounter >= _loopTotalCountSetting) //Loop 结束
  92. {
  93. if (!func())
  94. {
  95. return Tuple.Create(bActive, Result.FAIL); //执行错误
  96. }
  97. _loopCounter = 0;
  98. _loopTotalCountSetting = 0; // Loop 结束时,当前loop和loop总数都清零
  99. NextStep();
  100. return Tuple.Create(true, Result.RUN);
  101. }
  102. //继续下一LOOP
  103. NextStep(_loopStepStartId);
  104. return Tuple.Create(true, Result.RUN);
  105. }
  106. return Tuple.Create(false, Result.RUN);
  107. }
  108. public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
  109. {
  110. int idx = Convert.ToInt32(id);
  111. bool bActive = ActiveStep(idx);
  112. if (bActive)
  113. {
  114. if (_stepState == STATE.IDLE)
  115. {
  116. Result startRet = routine.Start();
  117. if (startRet == Result.FAIL)
  118. {
  119. return Tuple.Create(true, Result.FAIL); //执行错误
  120. }else if (startRet == Result.DONE)
  121. {
  122. NextStep();
  123. return Tuple.Create(true, Result.DONE);
  124. }
  125. _stepState = STATE.WAIT;
  126. }
  127. Result ret = routine.Monitor();
  128. if (ret == Result.DONE)
  129. {
  130. NextStep();
  131. return Tuple.Create(true, Result.DONE);
  132. }
  133. else if (ret == Result.FAIL || ret == Result.TIMEOUT)
  134. {
  135. return Tuple.Create(true, Result.FAIL);
  136. }
  137. else
  138. {
  139. return Tuple.Create(true, Result.RUN);
  140. }
  141. }
  142. return Tuple.Create(false, Result.RUN);
  143. }
  144. public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
  145. {
  146. int idx = Convert.ToInt32(id);
  147. bool bActive = ActiveStep(idx);
  148. if (bActive)
  149. {
  150. if (_stepState == STATE.IDLE)
  151. {
  152. foreach (var item in routines)
  153. {
  154. if (item.Start() == Result.FAIL)
  155. return Tuple.Create(true, Result.FAIL);
  156. }
  157. _stepState = STATE.WAIT;
  158. }
  159. //wait all sub failed or completedboo
  160. bool bFail = false;
  161. bool bDone = true;
  162. foreach (var item in routines)
  163. {
  164. Result ret = item.Monitor();
  165. bDone &= (ret == Result.FAIL || ret == Result.DONE);
  166. bFail |= ret == Result.FAIL;
  167. }
  168. if (bDone)
  169. {
  170. NextStep();
  171. if(bFail)
  172. return Tuple.Create(true, Result.FAIL);
  173. return Tuple.Create(true, Result.DONE);
  174. }
  175. return Tuple.Create(true, Result.RUN);
  176. }
  177. return Tuple.Create(false, Result.RUN);
  178. }
  179. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  180. {
  181. return Check(Check(Convert.ToInt32(id), func));
  182. }
  183. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  184. {
  185. return Check(execute(Convert.ToInt32(id), func));
  186. }
  187. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  188. {
  189. return Check(wait(Convert.ToInt32(id), func, timeout));
  190. }
  191. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  192. {
  193. return Check(wait(Convert.ToInt32(id), func, timeout));
  194. }
  195. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, double timeout = int.MaxValue)
  196. {
  197. int idx = Convert.ToInt32(id);
  198. bool bActive = ActiveStep(idx);
  199. bool? bExecute = false;
  200. if (bActive)
  201. {
  202. if (_stepState == STATE.IDLE)
  203. {
  204. if (!execute())
  205. {
  206. return Tuple.Create(bActive, Result.FAIL); //执行错误
  207. }
  208. _timer.Start(timeout);
  209. _stepState = STATE.WAIT;
  210. }
  211. bExecute = check();
  212. if (bExecute == null)
  213. {
  214. return Tuple.Create(bActive, Result.FAIL); //Termianate
  215. }
  216. else
  217. {
  218. if (bExecute.Value) //检查Success, next
  219. {
  220. NextStep();
  221. return Tuple.Create(true, Result.RUN);
  222. }
  223. }
  224. if(_timer.IsTimeout())
  225. return Tuple.Create(true, Result.TIMEOUT);
  226. return Tuple.Create(true, Result.RUN);
  227. }
  228. return Tuple.Create(false, Result.RUN);
  229. }
  230. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, Func<double> time)
  231. {
  232. int idx = Convert.ToInt32(id);
  233. bool bActive = ActiveStep(idx);
  234. bool? bExecute = false;
  235. double timeout = 0;
  236. if (bActive)
  237. {
  238. if (_stepState == STATE.IDLE)
  239. {
  240. timeout = time();
  241. if (!execute())
  242. {
  243. return Tuple.Create(true, Result.FAIL); //执行错误
  244. }
  245. _timer.Start(timeout);
  246. _stepState = STATE.WAIT;
  247. }
  248. bExecute = check();
  249. if (bExecute == null)
  250. {
  251. return Tuple.Create(true, Result.FAIL); //Termianate
  252. }
  253. if (bExecute.Value) //检查Success, next
  254. {
  255. NextStep();
  256. return Tuple.Create(true, Result.RUN);
  257. }
  258. if (_timer.IsTimeout())
  259. return Tuple.Create(true, Result.TIMEOUT);
  260. return Tuple.Create(true, Result.RUN);
  261. }
  262. return Tuple.Create(false, Result.RUN);
  263. }
  264. public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
  265. {
  266. int idx = Convert.ToInt32(id);
  267. bool bActive = ActiveStep(idx);
  268. if (bActive)
  269. {
  270. if (_stepState == STATE.IDLE)
  271. {
  272. rt.Start();
  273. _stepState = STATE.WAIT;
  274. }
  275. Result ret = rt.Monitor();
  276. return Tuple.Create(true, ret);
  277. }
  278. return Tuple.Create(false, Result.RUN);
  279. }
  280. //Monitor
  281. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  282. {
  283. int idx = Convert.ToInt32(id);
  284. bool bActive = ActiveStep(idx);
  285. bool bCheck = false;
  286. if (bActive)
  287. {
  288. if (_stepState == STATE.IDLE)
  289. {
  290. if ((func != null) && !func())
  291. {
  292. return Tuple.Create(true, Result.FAIL);
  293. }
  294. _timer.Start(time);
  295. _stepState = STATE.WAIT;
  296. }
  297. bCheck = check();
  298. if (!bCheck)
  299. {
  300. return Tuple.Create(true, Result.FAIL); //Termianate
  301. }
  302. if (_timer.IsTimeout())
  303. {
  304. NextStep();
  305. }
  306. return Tuple.Create(true, Result.RUN);
  307. }
  308. return Tuple.Create(false, Result.RUN);
  309. }
  310. //Delay
  311. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  312. {
  313. int idx = Convert.ToInt32(id);
  314. bool bActive = ActiveStep(idx);
  315. if (bActive)
  316. {
  317. if (_stepState == STATE.IDLE)
  318. {
  319. if ((func != null) && !func())
  320. {
  321. return Tuple.Create(true, Result.FAIL);
  322. }
  323. _timer.Start(time);
  324. _stepState = STATE.WAIT;
  325. }
  326. if (_timer.IsTimeout())
  327. {
  328. NextStep();
  329. }
  330. return Tuple.Create(true, Result.RUN);
  331. }
  332. return Tuple.Create(false, Result.RUN);
  333. }
  334. //先delay 再运行
  335. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  336. {
  337. int idx = Convert.ToInt32(id);
  338. bool bActive = ActiveStep(idx);
  339. if (bActive)
  340. {
  341. if (_stepState == STATE.IDLE)
  342. {
  343. _timer.Start(time);
  344. _stepState = STATE.WAIT;
  345. }
  346. if (_timer.IsTimeout())
  347. {
  348. if (func != null && !func())
  349. {
  350. return Tuple.Create(true, Result.FAIL);
  351. }
  352. NextStep();
  353. }
  354. return Tuple.Create(true, Result.RUN);
  355. }
  356. return Tuple.Create(false, Result.RUN);
  357. }
  358. #endregion
  359. private Tuple<bool,bool> execute(int id, Func<bool> func) //顺序执行
  360. {
  361. bool bActive = ActiveStep(id);
  362. bool bExecute = false;
  363. if (bActive)
  364. {
  365. bExecute = func();
  366. if (bExecute)
  367. {
  368. NextStep();
  369. }
  370. }
  371. return Tuple.Create(bActive, bExecute);
  372. }
  373. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  374. {
  375. bool bActive = ActiveStep(id);
  376. bool bExecute = false;
  377. if (bActive)
  378. {
  379. bExecute = func();
  380. NextStep();
  381. }
  382. return Tuple.Create(bActive, bExecute);
  383. }
  384. /// <summary>
  385. /// </summary>
  386. /// <param name="id"></param>
  387. /// <param name="func"></param>
  388. /// <param name="timeout"></param>
  389. /// <returns>
  390. /// item1 Active
  391. /// item2 execute
  392. /// item3 Timeout
  393. ///</returns>
  394. private Tuple<bool,bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  395. {
  396. bool bActive = ActiveStep(id);
  397. bool bExecute = false;
  398. bool bTimeout = false;
  399. if (bActive)
  400. {
  401. if (_stepState == STATE.IDLE)
  402. {
  403. _timer.Start(timeout);
  404. _stepState = STATE.WAIT;
  405. }
  406. bExecute = func();
  407. if (bExecute)
  408. {
  409. NextStep();
  410. }
  411. bTimeout = _timer.IsTimeout();
  412. }
  413. return Tuple.Create(bActive, bExecute, bTimeout);
  414. }
  415. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  416. {
  417. bool bActive = ActiveStep(id);
  418. bool? bExecute = false;
  419. bool bTimeout = false;
  420. if (bActive)
  421. {
  422. if (_stepState == STATE.IDLE)
  423. {
  424. _timer.Start(timeout);
  425. _stepState = STATE.WAIT;
  426. }
  427. bExecute = func();
  428. if (bExecute.HasValue && bExecute.Value)
  429. {
  430. NextStep();
  431. }
  432. bTimeout = _timer.IsTimeout();
  433. }
  434. return Tuple.Create(bActive, bExecute, bTimeout);
  435. }
  436. /// <summary>
  437. /// </summary>
  438. /// <param name="value"></param>
  439. /// <returns>
  440. /// item1 true, return item2
  441. /// </returns>
  442. private Tuple<bool,Result> Check(Tuple<bool, bool> value)
  443. {
  444. if (value.Item1)
  445. {
  446. if (!value.Item2)
  447. {
  448. return Tuple.Create(true, Result.FAIL);
  449. }
  450. return Tuple.Create(true, Result.RUN);
  451. }
  452. return Tuple.Create(false, Result.RUN);
  453. }
  454. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  455. {
  456. if (value.Item1) // 当前执行
  457. {
  458. if (CheckTimeout(value)) //timeout
  459. {
  460. return Tuple.Create(true, Result.TIMEOUT);
  461. }
  462. return Tuple.Create(true, Result.RUN);
  463. }
  464. return Tuple.Create(false, Result.RUN);
  465. }
  466. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  467. {
  468. if (value.Item1) // 当前执行
  469. {
  470. if (value.Item2 == null)
  471. {
  472. return Tuple.Create(true, Result.FAIL);
  473. }
  474. else
  475. {
  476. if (value.Item2 == false && value.Item3 == true) //timeout
  477. {
  478. return Tuple.Create(true, Result.TIMEOUT);
  479. }
  480. return Tuple.Create(true, Result.RUN);
  481. }
  482. }
  483. return Tuple.Create(false, Result.RUN);
  484. }
  485. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  486. {
  487. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  488. }
  489. private bool ActiveStep(int id) //
  490. {
  491. if (_historySteps.Contains(id))
  492. return false;
  493. this._currentStepId = id;
  494. return true;
  495. }
  496. protected void NextStep()
  497. {
  498. _historySteps.Add(_currentStepId);
  499. _stepState = STATE.IDLE;
  500. }
  501. protected void NextStep(int step) //loop
  502. {
  503. if (!_historySteps.Contains(step))
  504. {
  505. System.Diagnostics.Trace.Assert(false, $"Error, no step {step}");
  506. LOG.Write($"Error, no step {step}");
  507. return;
  508. }
  509. int[] steps = _historySteps.ToArray();
  510. for (int i = steps.Length - 1; i >= 0; i--)
  511. {
  512. _historySteps.RemoveAt(i);
  513. if (steps[i] == step)
  514. {
  515. _currentStepId = step;
  516. break;
  517. }
  518. _waitSteps.Insert(0,steps[i]);
  519. }
  520. _stepState = STATE.IDLE;
  521. }
  522. public void Delay(int id, double delaySeconds)
  523. {
  524. Tuple<bool, Result> ret = Delay(id, () =>
  525. {
  526. return true;
  527. }, delaySeconds * 1000);
  528. if (ret.Item1)
  529. {
  530. if (ret.Item2 == Result.RUN)
  531. {
  532. throw (new RoutineBreakException());
  533. }
  534. }
  535. }
  536. public bool IsActived(int id)
  537. {
  538. return _historySteps.Contains(id);
  539. }
  540. }
  541. }