RoutineRunner.cs 13 KB

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