RoutineRunner.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using Aitex.Core.RT.Routine;
  2. using athosRT.tool;
  3. using MECF.Framework.Common.Device.Bases;
  4. using MECF.Framework.Common.Equipment;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace athosRT.FSM
  12. {
  13. public class RoutineRunner
  14. {
  15. public RState Status => _runnerState;
  16. private bool IsSubStepRunning(Enum id) => _runnerState == RState.Running && _subState == RState.Running && (_curStep != null && _curStep.GetHashCode() == id.GetHashCode());
  17. private bool bAllowSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode == false;
  18. private bool bAllowLoopSubStepStart(Enum id) => _runnerState == RState.Running && _subState == RState.End && !_steps.Contains(id) && _isLoopMode;
  19. public int LoopCounter => _loopCounter;
  20. public long StepElapsedMS => _subStepTimer.ElapsedMilliseconds;
  21. enum InvalidStep { Invalid = 0x7FFFFFFF, };
  22. private RState _subState = RState.Init;
  23. private Stack<Enum> _steps = new Stack<Enum>();
  24. private Stack<Enum> _loopSteps = new Stack<Enum>();
  25. private Enum _curStep = InvalidStep.Invalid;
  26. private RState _runnerState = RState.Init;
  27. private string _name;
  28. private string _loopName;
  29. private ModuleName _module;
  30. // 缺省最大超时 10 分钟, delay 5 秒
  31. private const int _defaultTimeout = 600000;
  32. private const int _defaultDelay = 5000;
  33. Stopwatch _subStepTimer = new Stopwatch();
  34. private bool _isLoopMode = false;
  35. private int _loopCounterSP = 0;
  36. private int _loopCounter = 0;
  37. static private int _RunnerToken = 0;
  38. public RoutineRunner()
  39. {
  40. _runnerState = RState.Init;
  41. }
  42. public void Reset()
  43. {
  44. _runnerState = RState.Init;
  45. _subState = RState.Init;
  46. _isLoopMode = false;
  47. _steps.Clear();
  48. _loopSteps.Clear();
  49. _subStepTimer.Reset();
  50. }
  51. public RState Start(ModuleName module, string name)
  52. {
  53. Reset();
  54. _module = module;
  55. _name = $"{name} (Token:{_RunnerToken++})";
  56. _runnerState = RState.Running;
  57. _subState = RState.End;
  58. Notify("开始");
  59. return _runnerState;
  60. }
  61. public void Stop(string reason)
  62. {
  63. Alarm(reason);
  64. _runnerState = _subState = RState.Failed;
  65. }
  66. public RoutineRunner Run(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  67. {
  68. if (bAllowSubStepStart(id))
  69. {
  70. startNewStep(id, action);
  71. }
  72. else if (IsSubStepRunning(id))
  73. {
  74. if (condition())
  75. {
  76. _subState = RState.End;
  77. }
  78. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  79. {
  80. Alarm($"Step:{id} 超时");
  81. _runnerState = _subState = RState.Timeout;
  82. _subStepTimer.Reset();
  83. }
  84. }
  85. return this;
  86. }
  87. public RoutineRunner Run(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  88. {
  89. if (bAllowSubStepStart(id))
  90. {
  91. startNewStep(id, action);
  92. }
  93. else if (IsSubStepRunning(id))
  94. {
  95. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  96. {
  97. _subState = RState.End;
  98. }
  99. }
  100. return this;
  101. }
  102. public RoutineRunner End(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  103. {
  104. if (bAllowSubStepStart(id))
  105. {
  106. startNewStep(id, action);
  107. }
  108. else if (IsSubStepRunning(id))
  109. {
  110. if (condition())
  111. {
  112. Notify($"结束");
  113. _runnerState = RState.End;
  114. _subState = RState.End;
  115. }
  116. else
  117. {
  118. if (_subStepTimer.ElapsedMilliseconds > timeout)
  119. {
  120. Alarm($"Step:{id} 超时");
  121. _runnerState = RState.Failed;
  122. _subState = RState.Timeout;
  123. }
  124. }
  125. }
  126. return this;
  127. }
  128. public RoutineRunner End(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  129. {
  130. if (bAllowSubStepStart(id))
  131. {
  132. startNewStep(id, action);
  133. }
  134. else if (IsSubStepRunning(id))
  135. {
  136. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  137. {
  138. Notify($"结束");
  139. _runnerState = RState.End;
  140. _subState = RState.End;
  141. }
  142. }
  143. return this;
  144. }
  145. public RoutineRunner Delay(Enum id, int delayMS = _defaultDelay)
  146. {
  147. return Run(id, () => { return true; }, delayMS);
  148. }
  149. public RoutineRunner Wait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  150. {
  151. return Run(id, () => { return true; }, condition, timeout);
  152. }
  153. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  154. {
  155. if (bAllowSubStepStart(id))
  156. {
  157. _loopName = name;
  158. _loopCounterSP = cycleCount;
  159. _loopCounter = 0;
  160. _isLoopMode = true;
  161. startNewLoopStep(id, action);
  162. }
  163. else if (bAllowLoopSubStepStart(id))
  164. {
  165. startNewLoopStep(id, action);
  166. }
  167. else if (IsSubStepRunning(id))
  168. {
  169. if (condition())
  170. {
  171. _subState = RState.End;
  172. }
  173. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  174. {
  175. Alarm($"Step:{id} 超时");
  176. _runnerState = _subState = RState.Timeout;
  177. _subStepTimer.Reset();
  178. }
  179. }
  180. return this;
  181. }
  182. public RoutineRunner LoopStart(Enum id, string name, int cycleCount, Func<bool> action, int delayMS = _defaultDelay)
  183. {
  184. if (bAllowSubStepStart(id))
  185. {
  186. _loopName = name;
  187. _loopCounterSP = cycleCount;
  188. _loopCounter = 0;
  189. _isLoopMode = true;
  190. startNewLoopStep(id, action);
  191. }
  192. else if (bAllowLoopSubStepStart(id))
  193. {
  194. startNewLoopStep(id, action);
  195. }
  196. else if (IsSubStepRunning(id))
  197. {
  198. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  199. {
  200. _subState = RState.End;
  201. }
  202. }
  203. return this;
  204. }
  205. public RoutineRunner LoopRun(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  206. {
  207. if (bAllowLoopSubStepStart(id))
  208. {
  209. startNewLoopStep(id, action);
  210. }
  211. else if (IsSubStepRunning(id))
  212. {
  213. if (condition())
  214. {
  215. _subState = RState.End;
  216. }
  217. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  218. {
  219. Alarm($"Step:{id} 超时");
  220. _runnerState = _subState = RState.Timeout;
  221. _subStepTimer.Reset();
  222. }
  223. }
  224. return this;
  225. }
  226. public RoutineRunner LoopRun(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  227. {
  228. if (bAllowLoopSubStepStart(id))
  229. {
  230. startNewLoopStep(id, action);
  231. }
  232. else if (IsSubStepRunning(id))
  233. {
  234. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  235. {
  236. _subState = RState.End;
  237. }
  238. }
  239. return this;
  240. }
  241. public RoutineRunner LoopEnd(Enum id, Func<bool> action, Func<bool> condition, int timeout = _defaultTimeout)
  242. {
  243. if (bAllowLoopSubStepStart(id))
  244. {
  245. startNewLoopStep(id, action);
  246. }
  247. else if (IsSubStepRunning(id))
  248. {
  249. if (condition())
  250. {
  251. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  252. _subState = RState.End;
  253. _loopCounter++;
  254. if (_loopCounter >= _loopCounterSP)
  255. {
  256. foreach (var lid in _loopSteps)
  257. _steps.Push(lid);
  258. _loopSteps.Clear();
  259. _isLoopMode = false;
  260. }
  261. }
  262. else if (_subStepTimer.ElapsedMilliseconds >= timeout)
  263. {
  264. Alarm($"Step:{id} 超时");
  265. _runnerState = _subState = RState.Timeout;
  266. _subStepTimer.Reset();
  267. }
  268. }
  269. return this;
  270. }
  271. public RoutineRunner LoopEnd(Enum id, Func<bool> action, int delayMS = _defaultDelay)
  272. {
  273. if (bAllowLoopSubStepStart(id))
  274. {
  275. startNewLoopStep(id, action);
  276. }
  277. else if (IsSubStepRunning(id))
  278. {
  279. if (_subStepTimer.ElapsedMilliseconds > delayMS)
  280. {
  281. Notify($"{_loopCounter + 1}th [{_loopName}] Loop End");
  282. _subState = RState.End;
  283. _loopCounter++;
  284. if (_loopCounter >= _loopCounterSP)
  285. {
  286. foreach (var lid in _loopSteps)
  287. _steps.Push(lid);
  288. _loopSteps.Clear();
  289. _isLoopMode = false;
  290. }
  291. }
  292. }
  293. return this;
  294. }
  295. public RoutineRunner LoopDelay(Enum id, int delayMS)
  296. {
  297. return LoopRun(id, () => { return true; }, delayMS);
  298. }
  299. public RoutineRunner LoopWait(Enum id, Func<bool> condition, int timeout = _defaultTimeout)
  300. {
  301. return LoopRun(id, () => { return true; }, condition, timeout);
  302. }
  303. private void startNewStep(Enum id, Func<bool> action)
  304. {
  305. if (action())
  306. {
  307. Notify($"Step: {id} start ---");
  308. _steps.Push(id);
  309. _curStep = id;
  310. _subState = RState.Running;
  311. _subStepTimer.Restart();
  312. }
  313. else
  314. {
  315. Alarm($"Step: {id} failed ---");
  316. _runnerState = RState.Failed;
  317. }
  318. }
  319. private void startNewLoopStep(Enum id, Func<bool> action)
  320. {
  321. if (action())
  322. {
  323. Notify($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} start ---");
  324. if (!_loopSteps.Contains(id))
  325. _loopSteps.Push(id);
  326. _curStep = id;
  327. _subState = RState.Running;
  328. _subStepTimer.Restart();
  329. }
  330. else
  331. {
  332. Alarm($"{_loopCounter + 1}th [{_loopName}] Loop Step: {id} failed");
  333. _runnerState = RState.Failed;
  334. }
  335. }
  336. protected void Notify(string message)
  337. {
  338. LogObject.Info($"{_module}", $"[{_name}] Notify: {message} now");
  339. Trace.WriteLine($"Notify:{_module} is {message} now");
  340. //log
  341. }
  342. protected void Alarm(string message)
  343. {
  344. LogObject.Error($"{_module}", $"[{_name}] Notify: {message} now");
  345. Trace.WriteLine($"Alarm:{_module} is {message} now");
  346. //log
  347. }
  348. }
  349. static public class HOFs
  350. {
  351. public static Func<R> Apply<T1, R>(this Func<T1, R> func, T1 t1)
  352. => () => func(t1);
  353. public static Func<R> Apply<T1, T2, R>(this Func<T1, T2, R> func, T1 t1, T2 t2)
  354. => () => func(t1, t2);
  355. public static Func<R> Apply<T1, T2, T3, R>(this Func<T1, T2, T3, R> func, T1 t1, T2 t2, T3 t3)
  356. => () => func(t1, t2, t3);
  357. public static Func<bool> WrapAction(this Action action)
  358. => () => { action(); return true; };
  359. public static Func<bool> WrapAction<T1>(this Action<T1> action, T1 t1)
  360. => () => { action(t1); return true; };
  361. public static Func<bool> WrapAction<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
  362. => () => { action(t1, t2); return true; };
  363. public static Func<bool> WrapAction<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
  364. => () => { action(t1, t2, t3); return true; };
  365. }
  366. }