| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Diagnostics;
- using Aitex.Core.RT.Log;
- using MECF.Framework.Common.Equipment;
- using Aitex.Core.RT.Routine;
- using System.Threading;
- using PunkHPX8_Core;
- namespace MECF.Framework.Common.Routine
- {
- public class RoutineRunner
- {
- public RState Status => _runnerState;
- private bool IsSubStepRunning(Enum id) => _runnerState == RState.Running && _subState == RState.Running && (_curStep != null && _curStep.GetHashCode() == id.GetHashCode());
- private bool bAllowSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode == false;
- private bool bAllowLoopSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode;
- public int LoopCounter => _loopCounter;
- public long StepElapsedMS => _subStepTimer.ElapsedMilliseconds;
- private RState _subState = RState.Init;
- private Stack<Enum> _steps = new Stack<Enum>();
- private Stack<Enum> _loopSteps = new Stack<Enum>();
- enum InvalidStep { Invalid = 0x7FFFFFFF, };
- public Enum CurrentStep => _curStep;
- private Enum _curStep = InvalidStep.Invalid;
- private RState _runnerState = RState.Init;
- private string _name;
- private string _loopName;
- private string _module;
- // 缺省最大超时 10 分钟, delay 5 秒
- private const int _defaultTimeout = 600000;
- private const int _defaultDelay = 200;
- private const int _defaultDelay_5s = 5000;
- //用于计时
- Stopwatch _subStepTimer = new Stopwatch();
- private bool _isLoopMode = false;
- private int _loopCounterSP = 0;
- private int _loopCounter = 0;
- private int _subRoutineIndex = -1;
- private IRoutine _subRoutine = null;
- static private int _RunnerToken = 0;
- private Stopwatch _stopwatch = new Stopwatch();
- private string _errorMsg = "";
- public string ErrorMsg { get{ return _errorMsg; } set { _errorMsg = value; } }
- /// <summary>
- /// 完成时长
- /// </summary>
- public long ElapsedMS =>_stopwatch.ElapsedMilliseconds;
- public RoutineRunner()
- {
- _runnerState = RState.Init;
- }
- public void Reset()
- {
- _runnerState = RState.Init;
- _subState = RState.Init;
- _isLoopMode = false;
- _steps.Clear();
- _loopSteps.Clear();
- _stopwatch.Reset();
- _subStepTimer.Reset();
- _errorMsg = "";
- }
- public RState Start(ModuleName module, string name)
- {
- Reset();
- _module = module.ToString();
- _name = $"{name} (Token:{_RunnerToken++})";
- _runnerState = RState.Running;
- _subState = RState.End;
- _stopwatch.Restart();
- Notify("Start");
- return _runnerState;
- }
- public RState Start(string module, string name)
- {
- Reset();
- _module = module;
- _name = $"{name} (Token:{_RunnerToken++})";
- _runnerState = RState.Running;
- _subState = RState.End;
- _stopwatch.Restart();
- Notify("Start");
- return _runnerState;
- }
- /// <summary>
- /// 重试
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public RState Retry(Enum id,List<Enum> _preStepIds,string module, string name)
- {
- Reset();
- foreach (Enum item in _preStepIds)
- {
- _steps.Push(item);
- }
- _module = module;
- _name = $"{name} (Token:{_RunnerToken++})";
- _runnerState = RState.Running;
- _subState = RState.End;
- _stopwatch.Restart();
- Notify($"{id} Retry");
- _curStep = id;
- return _runnerState;
- }
- public void Stop(string reason,bool error=false)
- {
- if (Status == RState.Running)
- {
- Alarm(reason, error);
- }
- _runnerState = _subState = RState.Failed;
- _stopwatch.Stop();
- }
- public RoutineRunner Run(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout,bool error=true)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action,error);
- }
- else if(IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if(_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout",error);
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner RunConditionSubRoutine(Enum id,Func<int> routeIndexAction, IRoutine[] routines, object[] objects)
- {
- if (bAllowSubStepStart(id))
- {
- StartNewStepConditionRoutine(id, routeIndexAction, routines, objects);
- }
- else if(IsSubStepRunning(id))
- {
- if(_subRoutine==null)
- {
- _subState = RState.End;
- }
- else
- {
- if (objects.Length > _subRoutineIndex && _subRoutineIndex != -1)
- {
- _subRoutine.Start(objects[_subRoutineIndex]);
- }
- else
- {
- _subRoutine.Start();
- }
- _subState = RState.End;
- }
- }
- return this;
- }
- public RoutineRunner WaitConditionSubRoutine(Enum id)
- {
- if(bAllowSubStepStart(id))
- {
- Notify($"Step: {id} start ---");
- _steps.Push(id);
- _curStep = id;
- _subState = RState.Running;
- _subStepTimer.Restart();
- }
- else if(IsSubStepRunning(id))
- {
- if(_subRoutine==null)
- {
- _subState= RState.End;
- _subRoutineIndex = -1;
- }
- else
- {
- _subState = _subRoutine.Monitor();
- if(_subState==RState.End||_subState==RState.Failed||_subState==RState.Timeout)
- {
- _subRoutine = null;
- _subRoutineIndex = -1;
- }
- }
- }
- return this;
- }
- public void StartNewStepConditionRoutine(Enum id,Func<int> routeIndexAction, IRoutine[] routines, object[] objects)
- {
- _subRoutineIndex = routeIndexAction();
- if (routines.Length>_subRoutineIndex&&_subRoutineIndex!=-1)
- {
- _subRoutine = routines[_subRoutineIndex];
- }
- else
- {
- _subRoutine = null;
- }
- Notify($"Step: {id} start ---");
- _steps.Push(id);
- _curStep = id;
- _subState = RState.Running;
- _subStepTimer.Restart();
- }
- public RoutineRunner Run(Enum id, Func<bool> action, Func<bool> condition,Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout,bool error=true)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if(failedCondition())
- {
- if (!skipToNext)
- {
- _runnerState = _subState = RState.Failed;
- }
- else
- {
- _subState = RState.End;
- }
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout", error);
- if (!skipToNext)
- {
- _runnerState = _subState = RState.Timeout;
- }
- else
- {
- _subState = RState.End;
- }
- _subStepTimer.Reset();
- }
- }
- }
- return this;
- }
- public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if(bRun)
- {
- return Run(id, action, condition, timeout);
- }
- return this;
- }
- public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout)
- {
- if (bRun)
- {
- return Run(id, action, condition, failedCondition,skipToNext,timeout);
- }
- return this;
- }
- public RoutineRunner Run(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- _subState = RState.End;
- }
- }
- return this;
- }
- public RoutineRunner RunDelay(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- if (!action())
- {
- Alarm($"Step: {id} Failed ");
- _runnerState = _subState = RState.Failed;
- _subStepTimer.Reset();
- }
- else
- {
- _subState = RState.End;
- }
- }
- }
- return this;
- }
- public RoutineRunner RunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
- {
- if(bRun)
- {
- return Run(id, action, delayMS);
- }
- return this;
- }
- public RoutineRunner End(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- Notify($"End");
- _runnerState = RState.End;
- _subState = RState.End;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds > timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = RState.Failed;
- _subState = RState.Timeout;
- }
- }
- }
- return this;
- }
- public RoutineRunner End(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- Notify($"End");
- _runnerState = RState.End;
- _subState = RState.End;
- _stopwatch.Stop();
- }
- }
- return this;
- }
- public RoutineRunner Delay(Enum id, int delayMS = _defaultDelay_5s)
- {
- return Run(id, () => { return true; }, delayMS);
- }
- public RoutineRunner DelayIf(Enum id, bool bRun, int delayMS = _defaultDelay_5s)
- {
- if (bRun)
- {
- return Run(id, () => { return true; }, delayMS);
- }
- return this;
- }
- public RoutineRunner Wait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
- {
- return Run(id, () => { return true; }, condition, timeout);
- }
- public RoutineRunner WaitIf (Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (bRun)
- {
- return Run(id, () => { return true; }, condition, timeout);
- }
- return this;
- }
- public RoutineRunner WaitWithStopCondition(Enum id, Func<bool> condition, Func<bool> stopCondition, bool skipToNext = false,int timeout=_defaultTimeout,bool error=true)
- {
- return Run(id, () => { return true; }, condition, stopCondition,skipToNext,timeout,error);
- }
- public RoutineRunner WaitWithStopConditionIf(Enum id, bool bRun, Func<bool> condition, Func<bool> stopCondition,bool skipToNext=false, int timeout = _defaultTimeout, bool error = true)
- {
- if (bRun)
- {
- return Run(id, () => { return true; }, condition, stopCondition,skipToNext, timeout, error);
- }
- return this;
- }
- public RoutineRunner LoopWait(Enum id,Func<bool> action,Func<bool> condition,Func<bool> fail)
- {
- if (bAllowSubStepStart(id))
- {
- startNewStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if(condition())
- {
- _subState = RState.End;
- }
- else if(fail())
- {
- Notify($"End");
- _runnerState = RState.End;
- _subState = RState.End;
- }
- }
- return this;
- }
- public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if(bAllowSubStepStart(id))
- {
- _loopName = name;
- _loopCounterSP = cycleCount;
- _loopCounter = 0;
- _isLoopMode = true;
- startNewLoopStep(id, action);
- }
- else if(bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if(IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowSubStepStart(id))
- {
- _loopName = name;
- _loopCounterSP = cycleCount;
- _loopCounter = 0;
- _isLoopMode = true;
- startNewLoopStep(id, action);
- }
- else if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- _subState = RState.End;
- }
- }
-
- return this;
- }
- public RoutineRunner LoopRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if(bRun)
- {
- return LoopRun(id, action, condition, timeout);
- }
- return this;
- }
- public RoutineRunner LoopRunIfWithStopStatus(Enum id, bool bRun,Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
- {
- if (bRun)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (failedCondition())
- {
- _runnerState = _subState = RState.Failed;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout", true);
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopRun(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- _subState = RState.End;
- }
- }
- return this;
- }
- public RoutineRunner LoopRunOnlyTimeOutFault(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- if (!action())
- {
- Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
- _runnerState = _subState = RState.Failed;
- _subStepTimer.Reset();
- }
- else
- {
- _subState = RState.End;
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopRunIfOnlyTimeOutFault(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bRun)
- {
- return LoopRunOnlyTimeOutFault(id, action, delayMS);
- }
- return this;
- }
- public RoutineRunner LoopRunIf(Enum id, bool bRun, Func<bool> action, int delayMS = _defaultDelay)
- {
- if(bRun)
- {
- return LoopRun(id, action, delayMS);
- }
- return this;
- }
- public RoutineRunner LoopRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition,bool skipToNext=false, int timeout = _defaultTimeout)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (failedCondition())
- {
- if (!skipToNext)
- {
- _runnerState = _subState = RState.Failed;
- }
- else
- {
- _subState = RState.End;
- }
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout", true);
- if (!skipToNext)
- {
- _runnerState = _subState = RState.Timeout;
- }
- else
- {
- _subState = RState.End;
- }
- _subStepTimer.Reset();
- }
- }
- }
- return this;
- }
-
- public RoutineRunner LoopEnd(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter >= _loopCounterSP)
- {
- foreach (var lid in _loopSteps)
- _steps.Push(lid);
- _loopSteps.Clear();
- _isLoopMode = false;
- }
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopEnd(Enum id, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowLoopSubStepStart(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter >= _loopCounterSP)
- {
- foreach (var lid in _loopSteps)
- _steps.Push(lid);
- _loopSteps.Clear();
- _isLoopMode = false;
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopDelay(Enum id, int delayMS)
- {
- return LoopRun(id, () => { return true; }, delayMS);
- }
- public RoutineRunner LoopDelayIf(Enum id, bool bRun, int delayMS)
- {
- if (bRun)
- {
- return LoopRun(id, () => { return true; }, delayMS);
- }
- return this;
- }
- public RoutineRunner LoopRunDelay(Enum id, Func<bool> action, int delayMS)
- {
- return LoopRun(id, action, delayMS);
- }
- public RoutineRunner LoopWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
- {
- return LoopRun(id, () => { return true; }, condition, timeout);
- }
- public RoutineRunner LoopWaitIf(Enum id, bool bRun, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (bRun)
- {
- return LoopRun(id, () => { return true; }, condition, timeout);
- }
- return this;
-
- }
- public RoutineRunner LoopRetryStart(Enum id,string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
- {
- if (bAllowSubStepStart(id))
- {
- _loopName = name;
- _loopCounterSP = cycleCount;
- _loopCounter = 0;
- _isLoopMode = true;
- _loopSteps.Clear();
- startNewLoopStep(id, action);
- }
- else if (bAllowLoopSubStepStart(id)&&!_loopSteps.Contains(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (_subStepTimer.ElapsedMilliseconds > delayMS)
- {
- _subState = RState.End;
- }
- }
- return this;
- }
- public RoutineRunner LoopRetrySecondRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id)&&_loopCounter>=1&&_loopSteps.Count!=0&&!_loopSteps.Contains(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopRetrySecondRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id)&&_loopCounter>=1 && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (failedCondition())
- {
- _runnerState = _subState = RState.Failed;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout", true);
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
-
- if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryRunBack(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, action);
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter <= _loopCounterSP)
- {
- _loopSteps.Clear();
- }
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- return LoopRetryRun(id, () => { return true; }, condition, timeout);
- }
- public RoutineRunner LoopRetryWaitBack(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter <= _loopCounterSP)
- {
- _loopSteps.Clear();
- }
- _subStepTimer.Reset();
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryDelay(Enum id, int delayMS)
- {
- return LoopRetryRun(id, () => { return true; },()=> { return true; }, delayMS);
- }
- public RoutineRunner LoopRetryRunWithStopStatus(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (failedCondition())
- {
- _runnerState = _subState = RState.Failed;
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- Alarm($"Step:{id} timeout", true);
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryRunWithStopStatusBack(Enum id, Func<bool> condition, Func<bool> failedCondition, int timeout = _defaultTimeout)
- {
- if (_loopCounter > _loopCounterSP)
- {
- return this;
- }
- if (bAllowLoopSubStepStart(id) && _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- if (condition())
- {
- _subState = RState.End;
- }
- else if (failedCondition())
- {
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter <= _loopCounterSP)
- {
- _loopSteps.Clear();
- }
- _subStepTimer.Reset();
- }
- else
- {
- if (_subStepTimer.ElapsedMilliseconds >= timeout)
- {
- _subState = RState.End;
- _loopCounter++;
- if (_loopCounter <= _loopCounterSP)
- {
- _loopSteps.Clear();
- }
- _subStepTimer.Reset();
- }
- }
- }
- return this;
- }
- public RoutineRunner LoopRetryEnd(Enum id, int delayMS = _defaultDelay)
- {
- if (bAllowLoopSubStepStart(id)&& _loopSteps.Count != 0 && !_loopSteps.Contains(id))
- {
- startNewLoopStep(id, () => { return true; });
- }
- else if (IsSubStepRunning(id))
- {
- foreach (var lid in _loopSteps)
- _steps.Push(lid);
- if (_loopCounter > _loopCounterSP)
- {
- Alarm($"Step:{id} Retry times over {_loopCounterSP}");
- _runnerState = _subState = RState.Timeout;
- _subStepTimer.Reset();
- }
- else
- {
- _subState = RState.End;
- }
- _loopSteps.Clear();
- _isLoopMode = false;
- }
- return this;
- }
- private void startNewStep(Enum id, Func<bool> action,bool error=true)
- {
- if (action())
- {
- Notify($"Step: {id} start ---");
- _steps.Push(id);
- _curStep = id;
- _subState = RState.Running;
- _subStepTimer.Restart();
- }
- else
- {
- Alarm($"Step: {id} failed ",error);
- _runnerState = RState.Failed;
- }
- }
- private void startNewLoopStep(Enum id, Func<bool> action)
- {
- if (action())
- {
- Notify($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} start ---");
- if (!_loopSteps.Contains(id))
- _loopSteps.Push(id);
- _curStep = id;
- _subState = RState.Running;
- _subStepTimer.Restart();
- }
- else
- {
- Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
- _runnerState = RState.Failed;
- }
- }
- protected void Notify(string message)
- {
- LOG.WriteBackgroundLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name, message);
- }
- protected void Alarm(string message,bool error=true)
- {
- if (error)
- {
- _errorMsg= message;
- LOG.WriteLog(eEvent.ERR_ROUTINE_FAILED, _module,_name,message);
- }
- else
- {
- LOG.WriteLog(eEvent.EV_ROUTINE_NOTIFY, _module, _name,message);
- }
- }
- }
- }
|