TDKBLoadPortGonaHt.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Device.Unit;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.Routine;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Util;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.Common.Equipment;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.CarrierIDReaderBase;
  14. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  15. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
  16. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO.Ports;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDKB
  25. {
  26. public class TDKBLoadPortGonaHt : TDKBLoadPort
  27. {
  28. public TDKBLoadPortGonaHt(string module, string name, string scRoot, IoTrigger[] dos = null, IoSensor[] dis = null, RobotBaseDevice robot = null, bool IsTCPconnection = false, IE84CallBack e84 = null) :
  29. base(module, name, scRoot, dos, dis,robot, false, e84)
  30. {
  31. }
  32. protected override bool fStartUnload(object[] param)
  33. {
  34. if (IsMapWaferByLoadPort)
  35. return base.fStartUnload(param);
  36. if (!MapRobot.IsReady())
  37. {
  38. EV.PostAlarmLog(Name, $"Robot is not ready,can't load.");
  39. return false;
  40. }
  41. if (!_isPlaced)
  42. {
  43. EV.PostAlarmLog(Name, $"No carrier on {Name},can't load.");
  44. return false;
  45. }
  46. if (!_isDocked)
  47. {
  48. EV.PostAlarmLog(Name, $"Carrier is undocked on {Name},can't load.");
  49. return false;
  50. }
  51. ResetRoutine();
  52. return true;
  53. }
  54. protected override bool fMonitorUnload(object[] param)
  55. {
  56. if (IsMapWaferByLoadPort)
  57. return base.fMonitorUnload(param);
  58. IsBusy = false;
  59. if(IsNeedMapOnUnload)
  60. {
  61. WaitRobotReady((int)LpStepEnum.Step2, TimelimitAction, OnError);
  62. if (ExecuteResult.Item1)
  63. {
  64. return false;
  65. }
  66. RobotMap((int)LpStepEnum.Step3, TimelimitAction, OnError);
  67. if (ExecuteResult.Item1)
  68. {
  69. return false;
  70. }
  71. WaitRobotReady((int)LpStepEnum.Step4, TimelimitAction, OnError);
  72. if (ExecuteResult.Item1)
  73. {
  74. return false;
  75. }
  76. }
  77. XssUnload((int)LpStepEnum.Step5, TimelimitAction, OnError);
  78. if (ExecuteResult.Item1)
  79. {
  80. return false;
  81. }
  82. _isMapped = false;
  83. OnActionDone(null);
  84. return false;
  85. }
  86. public void WaitLoadPortReady(int id, int time,LoadPortBaseDevice lp, Action<string> error)
  87. {
  88. var ret = ExecuteAndWait(id, () =>
  89. {
  90. EV.PostInfoLog("System", $"Wait {lp.LPModuleName} to be ready");
  91. return true;
  92. }, () =>
  93. {
  94. if (lp.IsReady())
  95. return Result.DONE;
  96. if (lp.CurrentState == LoadPortStateEnum.Error)
  97. return Result.DONE;
  98. return Result.RUN;
  99. }, time * 1000);
  100. if (ret.Item1)
  101. {
  102. if (ret.Item2 == Result.FAIL)
  103. error("Wait lp ready failed");
  104. if (ret.Item2 == Result.TIMEOUT) //timeout
  105. {
  106. error("Wait lp ready timeout");
  107. }
  108. }
  109. }
  110. public void WaitRobotReady(int id, int time, Action<string> error)
  111. {
  112. var ret = ExecuteAndWait(id, () =>
  113. {
  114. EV.PostInfoLog("System", $"Wait {MapRobot.RobotModuleName} to be ready");
  115. return true;
  116. }, () =>
  117. {
  118. if (MapRobot.IsReady())
  119. return Result.DONE;
  120. if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
  121. return Result.FAIL;
  122. return Result.RUN;
  123. }, time * 1000);
  124. if (ret.Item1)
  125. {
  126. if (ret.Item2 == Result.FAIL)
  127. error("Wait robot ready failed");
  128. if (ret.Item2 == Result.TIMEOUT) //timeout
  129. {
  130. error("Wait robot ready timeout");
  131. }
  132. }
  133. }
  134. public void RobotHome(int id, int time, Action<string> error)
  135. {
  136. var ret = ExecuteAndWait(id, () =>
  137. {
  138. EV.PostInfoLog("System",$"{MapRobot.RobotModuleName} start homing");
  139. var reason = string.Empty;
  140. return MapRobot.HomeModule(null);
  141. }, () =>
  142. {
  143. if (MapRobot.IsReady())
  144. return Result.DONE;
  145. if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
  146. return Result.FAIL;
  147. return Result.RUN;
  148. }, time * 1000);
  149. if (ret.Item1)
  150. {
  151. if (ret.Item2 == Result.FAIL)
  152. error("Map robot home failed");
  153. if (ret.Item2 == Result.TIMEOUT) //timeout
  154. {
  155. error("Map robot home timeout");
  156. }
  157. }
  158. }
  159. public void RobotMap(int id, int time,Action<string> error)
  160. {
  161. var ret = ExecuteAndWait(id, () =>
  162. {
  163. EV.PostInfoLog($"System",$"{MapRobot.RobotModuleName} start to map LP:{LPModuleName}");
  164. var reason = string.Empty;
  165. return MapRobot.WaferMapping(LPModuleName,out _);
  166. }, () =>
  167. {
  168. if (MapRobot.IsReady())
  169. return Result.DONE;
  170. if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
  171. return Result.FAIL;
  172. return Result.RUN;
  173. }, time * 1000);
  174. if (ret.Item1)
  175. {
  176. if (ret.Item2 == Result.FAIL)
  177. error($"Robot map {LPModuleName} failed");
  178. if (ret.Item2 == Result.TIMEOUT) //timeout
  179. {
  180. error($"Robot map {LPModuleName} timeout");
  181. }
  182. }
  183. }
  184. public void XssLoad(int id, int time, Action<string> error)
  185. {
  186. var ret = ExecuteAndWait(id, () =>
  187. {
  188. EV.PostInfoLog($"System", $"Start to load LP:{LPModuleName}");
  189. var reason = string.Empty;
  190. return base.fStartLoad(null);
  191. }, () =>
  192. {
  193. if(!IsHandlerBusy)
  194. return Result.DONE;
  195. return Result.RUN;
  196. }, time * 1000);
  197. if (ret.Item1)
  198. {
  199. if (ret.Item2 == Result.FAIL)
  200. error($"{LPModuleName} load failed");
  201. if (ret.Item2 == Result.TIMEOUT) //timeout
  202. {
  203. error($"{LPModuleName} load timeout");
  204. }
  205. }
  206. }
  207. public void XssUnload(int id, int time, Action<string> error)
  208. {
  209. var ret = ExecuteAndWait(id, () =>
  210. {
  211. EV.PostInfoLog($"System", $"Start to unload LP:{LPModuleName}");
  212. var reason = string.Empty;
  213. return base.fStartUnload(null);
  214. }, () =>
  215. {
  216. if (!IsHandlerBusy)
  217. return Result.DONE;
  218. return Result.RUN;
  219. }, time * 1000);
  220. if (ret.Item1)
  221. {
  222. if (ret.Item2 == Result.FAIL)
  223. error($"{LPModuleName} unload failed");
  224. if (ret.Item2 == Result.TIMEOUT) //timeout
  225. {
  226. error($"{LPModuleName} unload timeout");
  227. }
  228. }
  229. }
  230. public void XssHome(int id, int time, Action<string> error)
  231. {
  232. var ret = ExecuteAndWait(id, () =>
  233. {
  234. EV.PostInfoLog($"System", $"Start to home LP:{LPModuleName}");
  235. var reason = string.Empty;
  236. return base.fStartInit(CurrentParamter);
  237. }, () =>
  238. {
  239. if (!IsHandlerBusy)
  240. return Result.DONE;
  241. return Result.RUN;
  242. }, time * 1000);
  243. if (ret.Item1)
  244. {
  245. if (ret.Item2 == Result.FAIL)
  246. error($"{LPModuleName} load failed");
  247. if (ret.Item2 == Result.TIMEOUT) //timeout
  248. {
  249. error($"{LPModuleName} load timeout");
  250. }
  251. }
  252. }
  253. public enum LpStepEnum
  254. {
  255. Step1,
  256. Step2,
  257. Step3,
  258. Step4,
  259. Step5,
  260. Step6,
  261. Step7,
  262. }
  263. protected DeviceTimer counter = new DeviceTimer();
  264. protected DeviceTimer delayTimer = new DeviceTimer();
  265. private enum STATE
  266. {
  267. IDLE,
  268. WAIT,
  269. }
  270. public int TokenId
  271. {
  272. get { return _id; }
  273. }
  274. private int _id; //step index
  275. private int _currentTokenId = -1;
  276. /// <summary>
  277. /// already done steps
  278. /// </summary>
  279. private Stack<int> _steps = new Stack<int>();
  280. private STATE state; //step state //idel,wait,
  281. //loop control
  282. private int loop = 0;
  283. private int loopCount = 0;
  284. private int loopID = 0;
  285. private DeviceTimer timer = new DeviceTimer();
  286. public int LoopCounter { get { return loop; } }
  287. public int LoopTotalTime { get { return loopCount; } }
  288. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  289. //状态持续时间,单位为秒
  290. public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
  291. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  292. protected Tuple<bool, Result> ExecuteResult;
  293. public void ResetRoutine()
  294. {
  295. _id = 0;
  296. _steps.Clear();
  297. loop = 0;
  298. loopCount = 0;
  299. state = STATE.IDLE;
  300. counter.Start(60 * 60 * 100); //默认1小时
  301. RoutineToken.Result = RoutineState.Running;
  302. _currentTokenId = -1;
  303. ExecuteResult = Tuple.Create(false, Result.DONE);
  304. }
  305. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  306. {
  307. if (!Acitve(id))
  308. return;
  309. result.Result = execution();
  310. }
  311. #region interface
  312. public void StopLoop()
  313. {
  314. loop = loopCount;
  315. }
  316. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  317. {
  318. int idx = Convert.ToInt32(id);
  319. bool bActive = Acitve(idx);
  320. if (bActive)
  321. {
  322. if (!func())
  323. {
  324. return Tuple.Create(bActive, Result.FAIL); //执行错误
  325. }
  326. loopID = idx;
  327. loopCount = count;
  328. next();
  329. return Tuple.Create(true, Result.RUN);
  330. }
  331. return Tuple.Create(false, Result.RUN);
  332. }
  333. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  334. {
  335. int idx = Convert.ToInt32(id);
  336. bool bActive = Acitve(idx);
  337. if (bActive)
  338. {
  339. if (++loop >= loopCount) //Loop 结束
  340. {
  341. if (!func())
  342. {
  343. return Tuple.Create(bActive, Result.FAIL); //执行错误
  344. }
  345. loop = 0;
  346. loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
  347. next();
  348. return Tuple.Create(true, Result.RUN);
  349. }
  350. //继续下一LOOP
  351. next(loopID);
  352. return Tuple.Create(true, Result.RUN);
  353. }
  354. return Tuple.Create(false, Result.RUN);
  355. }
  356. public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
  357. {
  358. int idx = Convert.ToInt32(id);
  359. bool bActive = Acitve(idx);
  360. if (bActive)
  361. {
  362. if (state == STATE.IDLE)
  363. {
  364. Result startRet = routine.Start();
  365. if (startRet == Result.FAIL)
  366. {
  367. return Tuple.Create(true, Result.FAIL); //执行错误
  368. }
  369. else if (startRet == Result.DONE)
  370. {
  371. next();
  372. return Tuple.Create(true, Result.DONE);
  373. }
  374. state = STATE.WAIT;
  375. }
  376. Result ret = routine.Monitor();
  377. if (ret == Result.DONE)
  378. {
  379. next();
  380. return Tuple.Create(true, Result.DONE);
  381. }
  382. else if (ret == Result.FAIL || ret == Result.TIMEOUT)
  383. {
  384. return Tuple.Create(true, Result.FAIL);
  385. }
  386. else
  387. {
  388. return Tuple.Create(true, Result.RUN);
  389. }
  390. }
  391. return Tuple.Create(false, Result.RUN);
  392. }
  393. public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
  394. {
  395. int idx = Convert.ToInt32(id);
  396. bool bActive = Acitve(idx);
  397. if (bActive)
  398. {
  399. if (state == STATE.IDLE)
  400. {
  401. foreach (var item in routines)
  402. {
  403. if (item.Start() == Result.FAIL)
  404. return Tuple.Create(true, Result.FAIL);
  405. }
  406. state = STATE.WAIT;
  407. }
  408. //wait all sub failed or completedboo
  409. bool bFail = false;
  410. bool bDone = true;
  411. foreach (var item in routines)
  412. {
  413. Result ret = item.Monitor();
  414. bDone &= (ret == Result.FAIL || ret == Result.DONE);
  415. bFail |= ret == Result.FAIL;
  416. }
  417. if (bDone)
  418. {
  419. next();
  420. if (bFail)
  421. return Tuple.Create(true, Result.FAIL);
  422. return Tuple.Create(true, Result.DONE);
  423. }
  424. return Tuple.Create(true, Result.RUN);
  425. }
  426. return Tuple.Create(false, Result.RUN);
  427. }
  428. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  429. {
  430. return Check(Check(Convert.ToInt32(id), func));
  431. }
  432. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  433. {
  434. return Check(execute(Convert.ToInt32(id), func));
  435. }
  436. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  437. {
  438. return Check(wait(Convert.ToInt32(id), func, timeout));
  439. }
  440. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  441. {
  442. return Check(wait(Convert.ToInt32(id), func, timeout));
  443. }
  444. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<Result?> check, double timeout = int.MaxValue)
  445. {
  446. int idx = Convert.ToInt32(id);
  447. bool bActive = Acitve(idx);
  448. Result? bExecute = Result.RUN;
  449. if (bActive)
  450. {
  451. //if (idx != _currentTokenId && ExecuteResult.Item1) return ExecuteResult;
  452. if (state == STATE.IDLE)
  453. {
  454. if (!execute())
  455. {
  456. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  457. return Tuple.Create(bActive, Result.FAIL); //执行错误
  458. }
  459. timer.Start(timeout);
  460. state = STATE.WAIT;
  461. _currentTokenId = idx;
  462. }
  463. bExecute = check();
  464. if (bExecute == null)
  465. {
  466. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  467. return Tuple.Create(bActive, Result.FAIL); //Termianate
  468. }
  469. else
  470. {
  471. if (bExecute == Result.DONE) //检查Success, next
  472. {
  473. next();
  474. ExecuteResult = Tuple.Create(true, Result.RUN);
  475. return Tuple.Create(true, Result.RUN);
  476. }
  477. if (bExecute == Result.Succeed) //检查Success, next
  478. {
  479. next();
  480. ExecuteResult = Tuple.Create(true, Result.RUN);
  481. return Tuple.Create(true, Result.RUN);
  482. }
  483. if (bExecute == Result.FAIL) //检查 Fail 直接返回Fail
  484. {
  485. ExecuteResult = Tuple.Create(true, Result.FAIL);
  486. return Tuple.Create(true, Result.FAIL);
  487. }
  488. }
  489. if (timer.IsTimeout())
  490. {
  491. ExecuteResult = Tuple.Create(true, Result.TIMEOUT);
  492. return Tuple.Create(true, Result.TIMEOUT);
  493. }
  494. ExecuteResult = Tuple.Create(true, Result.RUN);
  495. return Tuple.Create(true, Result.RUN);
  496. }
  497. ExecuteResult = Tuple.Create(false, Result.RUN);
  498. return Tuple.Create(false, Result.RUN);
  499. }
  500. public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
  501. {
  502. int idx = Convert.ToInt32(id);
  503. bool bActive = Acitve(idx);
  504. if (bActive)
  505. {
  506. if (state == STATE.IDLE)
  507. {
  508. rt.Start();
  509. state = STATE.WAIT;
  510. }
  511. Result ret = rt.Monitor();
  512. return Tuple.Create(true, ret);
  513. }
  514. return Tuple.Create(false, Result.RUN);
  515. }
  516. //Monitor
  517. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  518. {
  519. int idx = Convert.ToInt32(id);
  520. bool bActive = Acitve(idx);
  521. bool bCheck = false;
  522. if (bActive)
  523. {
  524. if (state == STATE.IDLE)
  525. {
  526. if ((func != null) && !func())
  527. {
  528. return Tuple.Create(true, Result.FAIL);
  529. }
  530. timer.Start(time);
  531. state = STATE.WAIT;
  532. }
  533. bCheck = check();
  534. if (!bCheck)
  535. {
  536. return Tuple.Create(true, Result.FAIL); //Termianate
  537. }
  538. if (timer.IsTimeout())
  539. {
  540. next();
  541. }
  542. return Tuple.Create(true, Result.RUN);
  543. }
  544. return Tuple.Create(false, Result.RUN);
  545. }
  546. //Delay
  547. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  548. {
  549. int idx = Convert.ToInt32(id);
  550. bool bActive = Acitve(idx);
  551. if (bActive)
  552. {
  553. //if (_currentTokenId != idx && !ExecuteResult.Item1) return ExecuteResult;
  554. if (state == STATE.IDLE)
  555. {
  556. if ((func != null) && !func())
  557. {
  558. ExecuteResult = Tuple.Create(true, Result.FAIL);
  559. return Tuple.Create(true, Result.FAIL);
  560. }
  561. _currentTokenId = idx;
  562. timer.Start(time);
  563. state = STATE.WAIT;
  564. }
  565. if (timer.IsTimeout())
  566. {
  567. next();
  568. }
  569. ExecuteResult = Tuple.Create(true, Result.RUN);
  570. return Tuple.Create(true, Result.RUN);
  571. }
  572. ExecuteResult = Tuple.Create(false, Result.RUN);
  573. return Tuple.Create(false, Result.RUN);
  574. }
  575. //先delay 再运行
  576. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  577. {
  578. int idx = Convert.ToInt32(id);
  579. bool bActive = Acitve(idx);
  580. if (bActive)
  581. {
  582. if (state == STATE.IDLE)
  583. {
  584. timer.Start(time);
  585. state = STATE.WAIT;
  586. }
  587. if (timer.IsTimeout())
  588. {
  589. if (func != null && !func())
  590. {
  591. return Tuple.Create(true, Result.FAIL);
  592. }
  593. next();
  594. }
  595. return Tuple.Create(true, Result.RUN);
  596. }
  597. return Tuple.Create(false, Result.RUN);
  598. }
  599. #endregion
  600. private Tuple<bool, bool> execute(int id, Func<bool> func) //顺序执行
  601. {
  602. bool bActive = Acitve(id);
  603. bool bExecute = false;
  604. if (bActive)
  605. {
  606. //if (ExecuteResult.Item1) return Tuple.Create(true, true);
  607. bExecute = func();
  608. if (bExecute)
  609. {
  610. next();
  611. }
  612. }
  613. return Tuple.Create(bActive, bExecute);
  614. }
  615. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  616. {
  617. bool bActive = Acitve(id);
  618. bool bExecute = false;
  619. if (bActive)
  620. {
  621. if (ExecuteResult.Item1) return Tuple.Create(true, true);
  622. bExecute = func();
  623. next();
  624. }
  625. return Tuple.Create(bActive, bExecute);
  626. }
  627. /// <summary>
  628. /// </summary>
  629. /// <param name="id"></param>
  630. /// <param name="func"></param>
  631. /// <param name="timeout"></param>
  632. /// <returns>
  633. /// item1 Active
  634. /// item2 execute
  635. /// item3 Timeout
  636. ///</returns>
  637. private Tuple<bool, bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  638. {
  639. bool bActive = Acitve(id);
  640. bool bExecute = false;
  641. bool bTimeout = false;
  642. if (bActive)
  643. {
  644. if (state == STATE.IDLE)
  645. {
  646. timer.Start(timeout);
  647. state = STATE.WAIT;
  648. }
  649. bExecute = func();
  650. if (bExecute)
  651. {
  652. next();
  653. }
  654. bTimeout = timer.IsTimeout();
  655. }
  656. return Tuple.Create(bActive, bExecute, bTimeout);
  657. }
  658. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  659. {
  660. bool bActive = Acitve(id);
  661. bool? bExecute = false;
  662. bool bTimeout = false;
  663. if (bActive)
  664. {
  665. if (state == STATE.IDLE)
  666. {
  667. timer.Start(timeout);
  668. state = STATE.WAIT;
  669. }
  670. bExecute = func();
  671. if (bExecute.HasValue && bExecute.Value)
  672. {
  673. next();
  674. }
  675. bTimeout = timer.IsTimeout();
  676. }
  677. return Tuple.Create(bActive, bExecute, bTimeout);
  678. }
  679. /// <summary>
  680. /// </summary>
  681. /// <param name="value"></param>
  682. /// <returns>
  683. /// item1 true, return item2
  684. /// </returns>
  685. private Tuple<bool, Result> Check(Tuple<bool, bool> value)
  686. {
  687. if (value.Item1)
  688. {
  689. if (!value.Item2)
  690. {
  691. ExecuteResult = Tuple.Create(true, Result.FAIL);
  692. return Tuple.Create(true, Result.FAIL);
  693. }
  694. ExecuteResult = Tuple.Create(true, Result.RUN);
  695. return Tuple.Create(true, Result.RUN);
  696. }
  697. ExecuteResult = Tuple.Create(false, Result.RUN);
  698. return Tuple.Create(false, Result.RUN);
  699. }
  700. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  701. {
  702. if (value.Item1) // 当前执行
  703. {
  704. if (CheckTimeout(value)) //timeout
  705. {
  706. return Tuple.Create(true, Result.TIMEOUT);
  707. }
  708. return Tuple.Create(true, Result.RUN);
  709. }
  710. return Tuple.Create(false, Result.RUN);
  711. }
  712. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  713. {
  714. if (value.Item1) // 当前执行
  715. {
  716. if (value.Item2 == null)
  717. {
  718. return Tuple.Create(true, Result.FAIL);
  719. }
  720. else
  721. {
  722. if (value.Item2 == false && value.Item3 == true) //timeout
  723. {
  724. return Tuple.Create(true, Result.TIMEOUT);
  725. }
  726. return Tuple.Create(true, Result.RUN);
  727. }
  728. }
  729. return Tuple.Create(false, Result.RUN);
  730. }
  731. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  732. {
  733. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  734. }
  735. private bool Acitve(int id) //
  736. {
  737. if (_steps.Contains(id))
  738. return false;
  739. this._id = id;
  740. return true;
  741. }
  742. private void next()
  743. {
  744. _steps.Push(this._id);
  745. state = STATE.IDLE;
  746. }
  747. private void next(int step) //loop
  748. {
  749. while (_steps.Pop() != step) ;
  750. state = STATE.IDLE;
  751. }
  752. public void Delay(int id, double delaySeconds)
  753. {
  754. Tuple<bool, Result> ret = Delay(id, () =>
  755. {
  756. return true;
  757. }, delaySeconds * 1000);
  758. if (ret.Item1)
  759. {
  760. if (ret.Item2 == Result.RUN)
  761. {
  762. }
  763. }
  764. }
  765. public bool IsActived(int id)
  766. {
  767. return _steps.Contains(id);
  768. }
  769. }
  770. }