12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- using Aitex.Core.Common;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Device.Unit;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.Routine;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using Aitex.Sorter.Common;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.CarrierIDReaderBase;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDKB
- {
- public class TDKBLoadPortXss : TDKBLoadPort
- {
- public TDKBLoadPortXss(string module, string name, string scRoot, IoTrigger[] dos = null, IoSensor[] dis = null, RobotBaseDevice robot = null, bool IsTCPconnection = false, IE84CallBack e84 = null) :
- base(module, name, scRoot, dos, dis,robot, false, e84)
- {
-
- }
- protected override bool fStartInit(object[] param)
- {
- if (MapRobot == null || MapRobot.RobotState != Robots.RobotStateEnum.Idle)
- {
- EV.PostAlarmLog(Name, $"Robot is not ready,can't home.");
- return false;
- }
- IsBusy = false;
- ResetRoutine();
- return true;
- }
- protected override bool fMonitorInit(object[] param)
- {
- RobotHome((int)LpStepEnum.Step1, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- WaitRobotReady((int)LpStepEnum.Step2, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- XssHome((int)LpStepEnum.Step3, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- OnActionDone(null);
- return false;
- }
- protected override bool fStartLoad(object[] param)
- {
- if (MapRobot == null||MapRobot.RobotState == Robots.RobotStateEnum.Error
- || MapRobot.RobotState == Robots.RobotStateEnum.Init)
- {
- EV.PostAlarmLog(Name, $"Robot is not ready,can't load.");
- return false;
- }
- if (!_isPlaced)
- {
- EV.PostAlarmLog(Name, $"No carrier on {Name},can't load.");
- return false;
- }
- if (_isDocked)
- {
- EV.PostAlarmLog(Name, $"Carrier is docked on {Name},can't load.");
- return false;
- }
- ResetRoutine();
- _isNeedToWaitAnotherLPComplete = _anotherLP.CurrentState != LoadPortStateEnum.Error;
- return true;
- }
- private bool _isNeedToWaitAnotherLPComplete;
- private LoadPortBaseDevice _anotherLP
- {
- get
- {
- if(LPModuleName == ModuleName.LP1)
- {
- return DEVICE.GetDevice<LoadPortBaseDevice>("LP2");
- }
- if (LPModuleName == ModuleName.LP2)
- {
- return DEVICE.GetDevice<LoadPortBaseDevice>("LP1");
- }
- return null;
- }
- }
- protected override bool fMonitorLoad(object[] param)
- {
- IsBusy = false;
- WaitLoadPortReady((int)LpStepEnum.Step6, TimelimitAction,_anotherLP, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- WaitRobotReady((int)LpStepEnum.Step1, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- RobotHome((int)LpStepEnum.Step2, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- WaitRobotReady((int)LpStepEnum.Step3, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- XssLoad((int)LpStepEnum.Step4, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- RobotMap((int)LpStepEnum.Step5, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- OnActionDone(null);
- return false;
- }
- protected override bool fStartUnload(object[] param)
- {
- if (MapRobot == null || MapRobot.RobotState == Robots.RobotStateEnum.Error
- || MapRobot.RobotState == Robots.RobotStateEnum.Init)
- {
- EV.PostAlarmLog(Name, $"Robot is not ready,can't load.");
- return false;
- }
- if (!_isPlaced)
- {
- EV.PostAlarmLog(Name, $"No carrier on {Name},can't load.");
- return false;
- }
- if (!_isDocked)
- {
- EV.PostAlarmLog(Name, $"Carrier is undocked on {Name},can't load.");
- return false;
- }
- ResetRoutine();
- return true;
- }
- protected override bool fMonitorUnload(object[] param)
- {
- IsBusy = false;
- WaitLoadPortReady((int)LpStepEnum.Step1, TimelimitAction, _anotherLP, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- WaitRobotReady((int)LpStepEnum.Step2, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- RobotHome((int)LpStepEnum.Step3, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- WaitRobotReady((int)LpStepEnum.Step4, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- XssUnload((int)LpStepEnum.Step5, TimelimitAction, OnError);
- if (ExecuteResult.Item1)
- {
- return false;
- }
- _isMapped = false;
- OnActionDone(null);
- return false;
- }
- public void WaitLoadPortReady(int id, int time,LoadPortBaseDevice lp, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog("System", $"Wait {lp.LPModuleName} to be ready");
- return true;
- }, () =>
- {
- if (lp.IsReady())
- return Result.DONE;
- if (lp.CurrentState == LoadPortStateEnum.Error)
- return Result.DONE;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error("Wait lp ready failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error("Wait lp ready timeout");
- }
- }
- }
- public void WaitRobotReady(int id, int time, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog("System", $"Wait {MapRobot.RobotModuleName} to be ready");
- return true;
- }, () =>
- {
- if (MapRobot.IsReady())
- return Result.DONE;
- if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
- return Result.FAIL;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error("Wait robot ready failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error("Wait robot ready timeout");
- }
- }
- }
- public void RobotHome(int id, int time, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog("System",$"{MapRobot.RobotModuleName} start homing");
- var reason = string.Empty;
- return MapRobot.HomeModule(null);
- }, () =>
- {
- if (MapRobot.IsReady())
- return Result.DONE;
- if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
- return Result.FAIL;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error("Map robot home failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error("Map robot home timeout");
- }
- }
- }
- public void RobotMap(int id, int time,Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog($"System",$"{MapRobot.RobotModuleName} start to map LP:{LPModuleName}");
- var reason = string.Empty;
- return MapRobot.WaferMapping(LPModuleName,out _);
- }, () =>
- {
- if (MapRobot.IsReady())
- return Result.DONE;
- if (MapRobot.RobotState == Robots.RobotStateEnum.Error)
- return Result.FAIL;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error($"Robot map {LPModuleName} failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error($"Robot map {LPModuleName} timeout");
- }
- }
- }
- public void XssLoad(int id, int time, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog($"System", $"Start to load LP:{LPModuleName}");
- var reason = string.Empty;
- return base.fStartLoad(null);
- }, () =>
- {
- if(!IsHandlerBusy)
- return Result.DONE;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error($"{LPModuleName} load failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error($"{LPModuleName} load timeout");
- }
- }
- }
- public void XssUnload(int id, int time, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog($"System", $"Start to unload LP:{LPModuleName}");
- var reason = string.Empty;
- return base.fStartUnload(null);
- }, () =>
- {
- if (!IsHandlerBusy)
- return Result.DONE;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error($"{LPModuleName} unload failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error($"{LPModuleName} unload timeout");
- }
- }
- }
- public void XssHome(int id, int time, Action<string> error)
- {
- var ret = ExecuteAndWait(id, () =>
- {
- EV.PostInfoLog($"System", $"Start to home LP:{LPModuleName}");
- var reason = string.Empty;
- return base.fStartInit(CurrentParamter);
- }, () =>
- {
- if (!IsHandlerBusy)
- return Result.DONE;
- return Result.RUN;
- }, time * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.FAIL)
- error($"{LPModuleName} load failed");
- if (ret.Item2 == Result.TIMEOUT) //timeout
- {
- error($"{LPModuleName} load timeout");
- }
- }
- }
- public enum LpStepEnum
- {
- Step1,
- Step2,
- Step3,
- Step4,
- Step5,
- Step6,
- Step7,
- }
- protected DeviceTimer counter = new DeviceTimer();
- protected DeviceTimer delayTimer = new DeviceTimer();
- private enum STATE
- {
- IDLE,
- WAIT,
- }
- public int TokenId
- {
- get { return _id; }
- }
- private int _id; //step index
- private int _currentTokenId = -1;
- /// <summary>
- /// already done steps
- /// </summary>
- private Stack<int> _steps = new Stack<int>();
- private STATE state; //step state //idel,wait,
- //loop control
- private int loop = 0;
- private int loopCount = 0;
- private int loopID = 0;
- private DeviceTimer timer = new DeviceTimer();
- public int LoopCounter { get { return loop; } }
- public int LoopTotalTime { get { return loopCount; } }
- // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
- //状态持续时间,单位为秒
- public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
- protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
- protected Tuple<bool, Result> ExecuteResult;
- public void ResetRoutine()
- {
- _id = 0;
- _steps.Clear();
- loop = 0;
- loopCount = 0;
- state = STATE.IDLE;
- counter.Start(60 * 60 * 100); //默认1小时
- RoutineToken.Result = RoutineState.Running;
- _currentTokenId = -1;
- ExecuteResult = Tuple.Create(false, Result.DONE);
- }
- protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
- {
- if (!Acitve(id))
- return;
- result.Result = execution();
- }
- #region interface
- public void StopLoop()
- {
- loop = loopCount;
- }
- public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (!func())
- {
- return Tuple.Create(bActive, Result.FAIL); //执行错误
- }
- loopID = idx;
- loopCount = count;
- next();
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (++loop >= loopCount) //Loop 结束
- {
- if (!func())
- {
- return Tuple.Create(bActive, Result.FAIL); //执行错误
- }
- loop = 0;
- loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
- next();
- return Tuple.Create(true, Result.RUN);
- }
- //继续下一LOOP
- next(loopID);
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- Result startRet = routine.Start();
- if (startRet == Result.FAIL)
- {
- return Tuple.Create(true, Result.FAIL); //执行错误
- }
- else if (startRet == Result.DONE)
- {
- next();
- return Tuple.Create(true, Result.DONE);
- }
- state = STATE.WAIT;
- }
- Result ret = routine.Monitor();
- if (ret == Result.DONE)
- {
- next();
- return Tuple.Create(true, Result.DONE);
- }
- else if (ret == Result.FAIL || ret == Result.TIMEOUT)
- {
- return Tuple.Create(true, Result.FAIL);
- }
- else
- {
- return Tuple.Create(true, Result.RUN);
- }
- }
- return Tuple.Create(false, Result.RUN);
- }
- public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- foreach (var item in routines)
- {
- if (item.Start() == Result.FAIL)
- return Tuple.Create(true, Result.FAIL);
- }
- state = STATE.WAIT;
- }
- //wait all sub failed or completedboo
- bool bFail = false;
- bool bDone = true;
- foreach (var item in routines)
- {
- Result ret = item.Monitor();
- bDone &= (ret == Result.FAIL || ret == Result.DONE);
- bFail |= ret == Result.FAIL;
- }
- if (bDone)
- {
- next();
- if (bFail)
- return Tuple.Create(true, Result.FAIL);
- return Tuple.Create(true, Result.DONE);
- }
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
- {
- return Check(Check(Convert.ToInt32(id), func));
- }
- public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
- {
- return Check(execute(Convert.ToInt32(id), func));
- }
- public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
- {
- return Check(wait(Convert.ToInt32(id), func, timeout));
- }
- public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
- {
- return Check(wait(Convert.ToInt32(id), func, timeout));
- }
- public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<Result?> check, double timeout = int.MaxValue)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- Result? bExecute = Result.RUN;
- if (bActive)
- {
- //if (idx != _currentTokenId && ExecuteResult.Item1) return ExecuteResult;
- if (state == STATE.IDLE)
- {
- if (!execute())
- {
- ExecuteResult = Tuple.Create(bActive, Result.FAIL);
- return Tuple.Create(bActive, Result.FAIL); //执行错误
- }
- timer.Start(timeout);
- state = STATE.WAIT;
- _currentTokenId = idx;
- }
- bExecute = check();
- if (bExecute == null)
- {
- ExecuteResult = Tuple.Create(bActive, Result.FAIL);
- return Tuple.Create(bActive, Result.FAIL); //Termianate
- }
- else
- {
- if (bExecute == Result.DONE) //检查Success, next
- {
- next();
- ExecuteResult = Tuple.Create(true, Result.RUN);
- return Tuple.Create(true, Result.RUN);
- }
- if (bExecute == Result.Succeed) //检查Success, next
- {
- next();
- ExecuteResult = Tuple.Create(true, Result.RUN);
- return Tuple.Create(true, Result.RUN);
- }
- if (bExecute == Result.FAIL) //检查 Fail 直接返回Fail
- {
- ExecuteResult = Tuple.Create(true, Result.FAIL);
- return Tuple.Create(true, Result.FAIL);
- }
- }
- if (timer.IsTimeout())
- {
- ExecuteResult = Tuple.Create(true, Result.TIMEOUT);
- return Tuple.Create(true, Result.TIMEOUT);
- }
- ExecuteResult = Tuple.Create(true, Result.RUN);
- return Tuple.Create(true, Result.RUN);
- }
- ExecuteResult = Tuple.Create(false, Result.RUN);
- return Tuple.Create(false, Result.RUN);
- }
- public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- rt.Start();
- state = STATE.WAIT;
- }
- Result ret = rt.Monitor();
- return Tuple.Create(true, ret);
- }
- return Tuple.Create(false, Result.RUN);
- }
- //Monitor
- public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- bool bCheck = false;
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- if ((func != null) && !func())
- {
- return Tuple.Create(true, Result.FAIL);
- }
- timer.Start(time);
- state = STATE.WAIT;
- }
- bCheck = check();
- if (!bCheck)
- {
- return Tuple.Create(true, Result.FAIL); //Termianate
- }
- if (timer.IsTimeout())
- {
- next();
- }
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- //Delay
- public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- //if (_currentTokenId != idx && !ExecuteResult.Item1) return ExecuteResult;
- if (state == STATE.IDLE)
- {
- if ((func != null) && !func())
- {
- ExecuteResult = Tuple.Create(true, Result.FAIL);
- return Tuple.Create(true, Result.FAIL);
- }
- _currentTokenId = idx;
- timer.Start(time);
- state = STATE.WAIT;
- }
- if (timer.IsTimeout())
- {
- next();
- }
- ExecuteResult = Tuple.Create(true, Result.RUN);
- return Tuple.Create(true, Result.RUN);
- }
- ExecuteResult = Tuple.Create(false, Result.RUN);
- return Tuple.Create(false, Result.RUN);
- }
- //先delay 再运行
- public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
- {
- int idx = Convert.ToInt32(id);
- bool bActive = Acitve(idx);
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- timer.Start(time);
- state = STATE.WAIT;
- }
- if (timer.IsTimeout())
- {
- if (func != null && !func())
- {
- return Tuple.Create(true, Result.FAIL);
- }
- next();
- }
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- #endregion
- private Tuple<bool, bool> execute(int id, Func<bool> func) //顺序执行
- {
- bool bActive = Acitve(id);
- bool bExecute = false;
- if (bActive)
- {
- //if (ExecuteResult.Item1) return Tuple.Create(true, true);
- bExecute = func();
- if (bExecute)
- {
- next();
- }
- }
- return Tuple.Create(bActive, bExecute);
- }
- private Tuple<bool, bool> Check(int id, Func<bool> func) //check
- {
- bool bActive = Acitve(id);
- bool bExecute = false;
- if (bActive)
- {
- if (ExecuteResult.Item1) return Tuple.Create(true, true);
- bExecute = func();
- next();
- }
- return Tuple.Create(bActive, bExecute);
- }
- /// <summary>
- /// </summary>
- /// <param name="id"></param>
- /// <param name="func"></param>
- /// <param name="timeout"></param>
- /// <returns>
- /// item1 Active
- /// item2 execute
- /// item3 Timeout
- ///</returns>
- private Tuple<bool, bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
- {
- bool bActive = Acitve(id);
- bool bExecute = false;
- bool bTimeout = false;
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- timer.Start(timeout);
- state = STATE.WAIT;
- }
- bExecute = func();
- if (bExecute)
- {
- next();
- }
- bTimeout = timer.IsTimeout();
- }
- return Tuple.Create(bActive, bExecute, bTimeout);
- }
- private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
- {
- bool bActive = Acitve(id);
- bool? bExecute = false;
- bool bTimeout = false;
- if (bActive)
- {
- if (state == STATE.IDLE)
- {
- timer.Start(timeout);
- state = STATE.WAIT;
- }
- bExecute = func();
- if (bExecute.HasValue && bExecute.Value)
- {
- next();
- }
- bTimeout = timer.IsTimeout();
- }
- return Tuple.Create(bActive, bExecute, bTimeout);
- }
- /// <summary>
- /// </summary>
- /// <param name="value"></param>
- /// <returns>
- /// item1 true, return item2
- /// </returns>
- private Tuple<bool, Result> Check(Tuple<bool, bool> value)
- {
- if (value.Item1)
- {
- if (!value.Item2)
- {
- ExecuteResult = Tuple.Create(true, Result.FAIL);
- return Tuple.Create(true, Result.FAIL);
- }
- ExecuteResult = Tuple.Create(true, Result.RUN);
- return Tuple.Create(true, Result.RUN);
- }
- ExecuteResult = Tuple.Create(false, Result.RUN);
- return Tuple.Create(false, Result.RUN);
- }
- private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
- {
- if (value.Item1) // 当前执行
- {
- if (CheckTimeout(value)) //timeout
- {
- return Tuple.Create(true, Result.TIMEOUT);
- }
- return Tuple.Create(true, Result.RUN);
- }
- return Tuple.Create(false, Result.RUN);
- }
- private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
- {
- if (value.Item1) // 当前执行
- {
- if (value.Item2 == null)
- {
- return Tuple.Create(true, Result.FAIL);
- }
- else
- {
- if (value.Item2 == false && value.Item3 == true) //timeout
- {
- return Tuple.Create(true, Result.TIMEOUT);
- }
- return Tuple.Create(true, Result.RUN);
- }
- }
- return Tuple.Create(false, Result.RUN);
- }
- private bool CheckTimeout(Tuple<bool, bool, bool> value)
- {
- return value.Item1 == true && value.Item2 == false && value.Item3 == true;
- }
- private bool Acitve(int id) //
- {
- if (_steps.Contains(id))
- return false;
- this._id = id;
- return true;
- }
- private void next()
- {
- _steps.Push(this._id);
- state = STATE.IDLE;
- }
- private void next(int step) //loop
- {
- while (_steps.Pop() != step) ;
- state = STATE.IDLE;
- }
- public void Delay(int id, double delaySeconds)
- {
- Tuple<bool, Result> ret = Delay(id, () =>
- {
- return true;
- }, delaySeconds * 1000);
- if (ret.Item1)
- {
- if (ret.Item2 == Result.RUN)
- {
- }
- }
- }
- public bool IsActived(int id)
- {
- return _steps.Contains(id);
- }
- }
- }
|