SeqenecRoutine.cs 16 KB

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