SeqenecRoutine2.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 SeqenecRoutine2
  12. {
  13. //timer, 计算routine时间
  14. protected DeviceTimer counter = new DeviceTimer();
  15. protected DeviceTimer delayTimer = new DeviceTimer();
  16. private enum STATE
  17. {
  18. IDLE,
  19. WAIT,
  20. }
  21. public int TokenId
  22. {
  23. get { return _id; }
  24. }
  25. private int _id; //step index
  26. private int _currentTokenId = -1;
  27. /// <summary>
  28. /// already done steps
  29. /// </summary>
  30. private Stack<int> _steps = new Stack<int>();
  31. private STATE state; //step state //idel,wait,
  32. //loop control
  33. private int loop = 0;
  34. private int loopCount = 0;
  35. private int loopID = 0;
  36. private DeviceTimer timer = new DeviceTimer();
  37. public int LoopCounter { get { return loop; } }
  38. public int LoopTotalTime { get { return loopCount; } }
  39. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  40. //状态持续时间,单位为秒
  41. public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
  42. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  43. protected Tuple<bool, Result> ExecuteResult;
  44. public void Reset()
  45. {
  46. _id = 0;
  47. _steps.Clear();
  48. loop = 0;
  49. loopCount = 0;
  50. state = STATE.IDLE;
  51. counter.Start(60*60*100); //默认1小时
  52. RoutineToken.Result = RoutineState.Running;
  53. _currentTokenId = -1;
  54. ExecuteResult = Tuple.Create(false, Result.DONE);
  55. }
  56. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  57. {
  58. if (!Acitve(id))
  59. return;
  60. result.Result = execution();
  61. }
  62. #region interface
  63. public void StopLoop()
  64. {
  65. loop = loopCount;
  66. }
  67. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  68. {
  69. int idx = Convert.ToInt32(id);
  70. bool bActive = Acitve(idx);
  71. if (bActive)
  72. {
  73. if (!func())
  74. {
  75. return Tuple.Create(bActive, Result.FAIL); //执行错误
  76. }
  77. loopID = idx;
  78. loopCount = count;
  79. next();
  80. return Tuple.Create(true, Result.RUN);
  81. }
  82. return Tuple.Create(false, Result.RUN);
  83. }
  84. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  85. {
  86. int idx = Convert.ToInt32(id);
  87. bool bActive = Acitve(idx);
  88. if (bActive)
  89. {
  90. if (++loop >= loopCount) //Loop 结束
  91. {
  92. if (!func())
  93. {
  94. return Tuple.Create(bActive, Result.FAIL); //执行错误
  95. }
  96. loop = 0;
  97. loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
  98. next();
  99. return Tuple.Create(true, Result.RUN);
  100. }
  101. //继续下一LOOP
  102. next(loopID);
  103. return Tuple.Create(true, Result.RUN);
  104. }
  105. return Tuple.Create(false, Result.RUN);
  106. }
  107. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  108. {
  109. return Check(Check(Convert.ToInt32(id), func));
  110. }
  111. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  112. {
  113. return Check(execute(Convert.ToInt32(id), func));
  114. }
  115. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  116. {
  117. return Check(wait(Convert.ToInt32(id), func, timeout));
  118. }
  119. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  120. {
  121. return Check(wait(Convert.ToInt32(id), func, timeout));
  122. }
  123. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<Result?> check, double timeout = int.MaxValue)
  124. {
  125. int idx = Convert.ToInt32(id);
  126. bool bActive = Acitve(idx);
  127. Result? bExecute = Result.RUN;
  128. if (bActive)
  129. {
  130. //if (idx != _currentTokenId && ExecuteResult.Item1) return ExecuteResult;
  131. if (state == STATE.IDLE)
  132. {
  133. if (!execute())
  134. {
  135. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  136. return Tuple.Create(bActive, Result.FAIL); //执行错误
  137. }
  138. timer.Start(timeout);
  139. state = STATE.WAIT;
  140. _currentTokenId = idx;
  141. }
  142. bExecute = check();
  143. if (bExecute == null)
  144. {
  145. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  146. return Tuple.Create(bActive, Result.FAIL); //Termianate
  147. }
  148. else
  149. {
  150. if (bExecute == Result.DONE) //检查Success, next
  151. {
  152. next();
  153. ExecuteResult = Tuple.Create(true, Result.RUN);
  154. return Tuple.Create(true, Result.RUN);
  155. }
  156. if (bExecute == Result.Succeed) //检查Success, next
  157. {
  158. next();
  159. ExecuteResult = Tuple.Create(true, Result.RUN);
  160. return Tuple.Create(true, Result.RUN);
  161. }
  162. if (bExecute == Result.FAIL) //检查 Fail 直接返回Fail
  163. {
  164. ExecuteResult = Tuple.Create(true, Result.FAIL);
  165. return Tuple.Create(true, Result.FAIL);
  166. }
  167. }
  168. if (timer.IsTimeout())
  169. {
  170. ExecuteResult = Tuple.Create(true, Result.TIMEOUT);
  171. return Tuple.Create(true, Result.TIMEOUT);
  172. }
  173. ExecuteResult = Tuple.Create(true, Result.RUN);
  174. return Tuple.Create(true, Result.RUN);
  175. }
  176. ExecuteResult = Tuple.Create(false, Result.RUN);
  177. return Tuple.Create(false, Result.RUN);
  178. }
  179. //Monitor
  180. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  181. {
  182. int idx = Convert.ToInt32(id);
  183. bool bActive = Acitve(idx);
  184. bool bCheck = false;
  185. if (bActive)
  186. {
  187. if (state == STATE.IDLE)
  188. {
  189. if ((func != null) && !func())
  190. {
  191. return Tuple.Create(true, Result.FAIL);
  192. }
  193. timer.Start(time);
  194. state = STATE.WAIT;
  195. }
  196. bCheck = check();
  197. if (!bCheck)
  198. {
  199. return Tuple.Create(true, Result.FAIL); //Termianate
  200. }
  201. if (timer.IsTimeout())
  202. {
  203. next();
  204. }
  205. return Tuple.Create(true, Result.RUN);
  206. }
  207. return Tuple.Create(false, Result.RUN);
  208. }
  209. //Delay
  210. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  211. {
  212. int idx = Convert.ToInt32(id);
  213. bool bActive = Acitve(idx);
  214. if (bActive)
  215. {
  216. //if (_currentTokenId != idx && !ExecuteResult.Item1) return ExecuteResult;
  217. if (state == STATE.IDLE)
  218. {
  219. if ((func != null) && !func())
  220. {
  221. ExecuteResult = Tuple.Create(true, Result.FAIL);
  222. return Tuple.Create(true, Result.FAIL);
  223. }
  224. _currentTokenId = idx;
  225. timer.Start(time);
  226. state = STATE.WAIT;
  227. }
  228. if (timer.IsTimeout())
  229. {
  230. next();
  231. }
  232. ExecuteResult = Tuple.Create(true, Result.RUN);
  233. return Tuple.Create(true, Result.RUN);
  234. }
  235. ExecuteResult = Tuple.Create(false, Result.RUN);
  236. return Tuple.Create(false, Result.RUN);
  237. }
  238. //先delay 再运行
  239. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  240. {
  241. int idx = Convert.ToInt32(id);
  242. bool bActive = Acitve(idx);
  243. if (bActive)
  244. {
  245. if (state == STATE.IDLE)
  246. {
  247. timer.Start(time);
  248. state = STATE.WAIT;
  249. }
  250. if (timer.IsTimeout())
  251. {
  252. if (func != null && !func())
  253. {
  254. return Tuple.Create(true, Result.FAIL);
  255. }
  256. next();
  257. }
  258. return Tuple.Create(true, Result.RUN);
  259. }
  260. return Tuple.Create(false, Result.RUN);
  261. }
  262. #endregion
  263. private Tuple<bool,bool> execute(int id, Func<bool> func) //顺序执行
  264. {
  265. bool bActive = Acitve(id);
  266. bool bExecute = false;
  267. if (bActive)
  268. {
  269. //if (ExecuteResult.Item1) return Tuple.Create(true, true);
  270. bExecute = func();
  271. if (bExecute)
  272. {
  273. next();
  274. }
  275. }
  276. return Tuple.Create(bActive, bExecute);
  277. }
  278. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  279. {
  280. bool bActive = Acitve(id);
  281. bool bExecute = false;
  282. if (bActive)
  283. {
  284. if (ExecuteResult.Item1) return Tuple.Create(true, true);
  285. bExecute = func();
  286. next();
  287. }
  288. return Tuple.Create(bActive, bExecute);
  289. }
  290. /// <summary>
  291. /// </summary>
  292. /// <param name="id"></param>
  293. /// <param name="func"></param>
  294. /// <param name="timeout"></param>
  295. /// <returns>
  296. /// item1 Active
  297. /// item2 execute
  298. /// item3 Timeout
  299. ///</returns>
  300. private Tuple<bool,bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  301. {
  302. bool bActive = Acitve(id);
  303. bool bExecute = false;
  304. bool bTimeout = false;
  305. if (bActive)
  306. {
  307. if (state == STATE.IDLE)
  308. {
  309. timer.Start(timeout);
  310. state = STATE.WAIT;
  311. }
  312. bExecute = func();
  313. if (bExecute)
  314. {
  315. next();
  316. }
  317. bTimeout = timer.IsTimeout();
  318. }
  319. return Tuple.Create(bActive, bExecute, bTimeout);
  320. }
  321. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  322. {
  323. bool bActive = Acitve(id);
  324. bool? bExecute = false;
  325. bool bTimeout = false;
  326. if (bActive)
  327. {
  328. if (state == STATE.IDLE)
  329. {
  330. timer.Start(timeout);
  331. state = STATE.WAIT;
  332. }
  333. bExecute = func();
  334. if (bExecute.HasValue && bExecute.Value)
  335. {
  336. next();
  337. }
  338. bTimeout = timer.IsTimeout();
  339. }
  340. return Tuple.Create(bActive, bExecute, bTimeout);
  341. }
  342. /// <summary>
  343. /// </summary>
  344. /// <param name="value"></param>
  345. /// <returns>
  346. /// item1 true, return item2
  347. /// </returns>
  348. private Tuple<bool,Result> Check(Tuple<bool, bool> value)
  349. {
  350. if (value.Item1)
  351. {
  352. if (!value.Item2)
  353. {
  354. ExecuteResult = Tuple.Create(true, Result.FAIL);
  355. return Tuple.Create(true, Result.FAIL);
  356. }
  357. ExecuteResult = Tuple.Create(true, Result.RUN);
  358. return Tuple.Create(true, Result.RUN);
  359. }
  360. ExecuteResult = Tuple.Create(false, Result.RUN);
  361. return Tuple.Create(false, Result.RUN);
  362. }
  363. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  364. {
  365. if (value.Item1) // 当前执行
  366. {
  367. if (CheckTimeout(value)) //timeout
  368. {
  369. return Tuple.Create(true, Result.TIMEOUT);
  370. }
  371. return Tuple.Create(true, Result.RUN);
  372. }
  373. return Tuple.Create(false, Result.RUN);
  374. }
  375. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  376. {
  377. if (value.Item1) // 当前执行
  378. {
  379. if (value.Item2 == null)
  380. {
  381. return Tuple.Create(true, Result.FAIL);
  382. }
  383. else
  384. {
  385. if (value.Item2 == false && value.Item3 == true) //timeout
  386. {
  387. return Tuple.Create(true, Result.TIMEOUT);
  388. }
  389. return Tuple.Create(true, Result.RUN);
  390. }
  391. }
  392. return Tuple.Create(false, Result.RUN);
  393. }
  394. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  395. {
  396. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  397. }
  398. private bool Acitve(int id) //
  399. {
  400. if (_steps.Contains(id))
  401. return false;
  402. this._id = id;
  403. return true;
  404. }
  405. private void next()
  406. {
  407. _steps.Push(this._id);
  408. state = STATE.IDLE;
  409. }
  410. private void next(int step) //loop
  411. {
  412. while(_steps.Pop() != step);
  413. state = STATE.IDLE;
  414. }
  415. public void Delay(int id, double delaySeconds)
  416. {
  417. Tuple<bool, Result> ret = Delay(id, () =>
  418. {
  419. return true;
  420. }, delaySeconds * 1000);
  421. if (ret.Item1)
  422. {
  423. if (ret.Item2 == Result.RUN)
  424. {
  425. }
  426. }
  427. }
  428. public bool IsActived(int id)
  429. {
  430. return _steps.Contains(id);
  431. }
  432. }
  433. }