using System; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using Aitex.Core.RT.Log; using MECF.Framework.Common.Equipment; using Venus_Core; namespace MECF.Framework.Common.Routine { public class RoutineRunner { public RState Status => _runnerState; private bool IsSubStepRunning(int id) => _runnerState == RState.Running && _subState == RState.Running && _curStep == id; private bool bAllowSubStepStart(int id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode == false; private bool bAllowLoopSubStepStart(int 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 _steps = new Stack(); private Stack _loopSteps = new Stack(); private int _curStep = int.MaxValue; private RState _runnerState = RState.Init; private string _name; private string _loopName; private ModuleName _module; // 缺省最大超时 10 分钟, delay 5 秒 private const int _defaultTimeout = 600000; private const int _defaultDelay = 5000; Stopwatch _subStepTimer = new Stopwatch(); private bool _isLoopMode = false; private int _loopCounterSP = 0; private int _loopCounter = 0; static private int _RunnerToken = 0; public RoutineRunner() { _runnerState = RState.Init; } public void Reset() { _runnerState = RState.Init; _subState = RState.Init; _isLoopMode = false; _steps.Clear(); _loopSteps.Clear(); _subStepTimer.Reset(); } public RState Start(ModuleName module, string name) { Reset(); _module = module; _name = $"{name} (Token:{_RunnerToken++})"; _runnerState = RState.Running; _subState = RState.End; Notify("开始"); return _runnerState; } public void Stop(string reason) { Alarm(reason); _runnerState = _subState = RState.Failed; } public RoutineRunner Run(int id, Func action, Func condition, int timeout = _defaultTimeout) { if (bAllowSubStepStart(id)) { startNewStep(id, action); } else if(IsSubStepRunning(id)) { if (condition()) { _subState = RState.End; } else if(_subStepTimer.ElapsedMilliseconds >= timeout) { Alarm($"Step:{id} 超时"); _runnerState = _subState = RState.Timeout; _subStepTimer.Reset(); } } return this; } public RoutineRunner Run(int id, Func 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 End(int id, Func action, Func condition, int timeout = _defaultTimeout) { if (bAllowSubStepStart(id)) { startNewStep(id, action); } else if (IsSubStepRunning(id)) { if (condition()) { Notify($"结束"); _runnerState = RState.End; _subState = RState.End; } else { if (_subStepTimer.ElapsedMilliseconds > timeout) { Alarm($"Step:{id} 超时"); _runnerState = RState.Failed; _subState = RState.Timeout; } } } return this; } public RoutineRunner End(int id, Func action, int delayMS = _defaultDelay) { if (bAllowSubStepStart(id)) { startNewStep(id, action); } else if (IsSubStepRunning(id)) { if (_subStepTimer.ElapsedMilliseconds > delayMS) { Notify($"结束"); _runnerState = RState.End; _subState = RState.End; } } return this; } public RoutineRunner Delay(int id, int delayMS = _defaultDelay) { return Run(id, () => { return true; }, delayMS); } public RoutineRunner Wait(int id, Func condition, int timeout = _defaultTimeout) { return Run(id, () => { return true; }, condition, timeout); } public RoutineRunner LoopStart(int id, string name, int cycleCount, Func action, Func 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} 超时"); _runnerState = _subState = RState.Timeout; _subStepTimer.Reset(); } } return this; } public RoutineRunner LoopStart(int id, string name, int cycleCount, Func 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(int id, Func action, Func 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} 超时"); _runnerState = _subState = RState.Timeout; _subStepTimer.Reset(); } } return this; } public RoutineRunner LoopRun(int id, Func 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 LoopEnd(int id, Func action, Func 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} 超时"); _runnerState = _subState = RState.Timeout; _subStepTimer.Reset(); } } return this; } public RoutineRunner LoopEnd(int id, Func 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(int id, int delayMS) { return LoopRun(id, () => { return true; }, delayMS); } public RoutineRunner LoopWait(int id, Func condition, int timeout = _defaultTimeout) { return LoopRun(id, () => { return true; }, condition, timeout); } private void startNewStep(int id, Func action) { if (action()) { Notify($"Step: {id} start ---"); _steps.Push(id); _curStep = id; _subState = RState.Running; _subStepTimer.Restart(); } else { Alarm($"Step: {id} failed "); _runnerState = RState.Failed; } } private void startNewLoopStep(int id, Func 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.Write(eEvent.EV_ROUTINE_NOTIFY, _module, _name, message); } protected void Alarm(string message) { LOG.Write(eEvent.ERR_ROUTINE_FAILED, _module, _name, message); } } static public class HOFs { public static Func Apply(this Func func, T1 t1) => () => func(t1); public static Func Apply(this Func func, T1 t1, T2 t2) => () => func(t1, t2); public static Func Apply(this Func func, T1 t1, T2 t2, T3 t3) => () => func(t1, t2, t3); public static Func WrapAction(this Action action) => () => { action(); return true; }; public static Func WrapAction(this Action action, T1 t1) => () => { action(t1); return true; }; public static Func WrapAction(this Action action, T1 t1, T2 t2) => () => { action(t1, t2); return true; }; public static Func WrapAction(this Action action, T1 t1, T2 t2, T3 t3) => () => { action(t1, t2, t3); return true; }; } }