ProcessStep.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using SKMC.Api.Common.Logger;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Threading;
  6. namespace SKMC.Api.Process.Model
  7. {
  8. /// <summary>
  9. /// 流程步骤
  10. /// </summary>
  11. public class ProcessStep
  12. {
  13. protected static readonly ILogger log = LogFactory.Get();
  14. /// <summary>
  15. /// 活动Id
  16. /// </summary>
  17. public int Id { get; set; }
  18. /// <summary>
  19. /// 活动名称
  20. /// </summary>
  21. public string Name { get; set; }
  22. /// <summary>
  23. /// 是否活动中
  24. /// </summary>
  25. public bool InAction { get; set; }
  26. /// <summary>
  27. /// Di触发条件集合
  28. /// </summary>
  29. public List<DioCnd> TriggerDiCnds { get; set; } = new List<DioCnd>();
  30. /// <summary>
  31. /// 电机触发条件集合
  32. /// </summary>
  33. public List<AxisCnd> TriggerAxisCnds { get; set; } = new List<AxisCnd>();
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public List<DioCnd> AlarmDiCnds { get; set; } = new List<DioCnd>();
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public List<AxisCnd> AlarmAxisCnds { get; set; } = new List<AxisCnd>();
  42. /// <summary>
  43. /// 流程站点
  44. /// </summary>
  45. public ProcessStation ProcessStation { get; set; }
  46. // 跳过条件判断(一次)
  47. public bool SkipCndOnce { get; set; }
  48. /// <summary>
  49. /// 是否初始步骤
  50. /// </summary>
  51. public bool First { get; set; }
  52. /// <summary>
  53. /// 是否自动模式
  54. /// 自动模式下完成Actions后会自动提交下一个流程步骤(按注册顺序)
  55. /// 默认为自动模式
  56. /// 可取消自动模式, 在Actions最后调用ProcessStation的PostManual提交指定的流程步骤
  57. /// </summary>
  58. public bool AutoMode { get; set; } = true;
  59. /// <summary>
  60. /// 满足该条件函数将执行Action动作
  61. /// </summary>
  62. public Func<bool> ActionCnd { get; set; }
  63. /// <summary>
  64. /// 项目实现的动作链
  65. /// 通过ActionCnd判断后回调执行
  66. /// </summary>
  67. public Action Actions { get; set; }
  68. /// <summary>
  69. /// 未通过ActionCnd判断的超时时间, 单位ms, 默认-1表示不启用
  70. /// </summary>
  71. [Obsolete("此属性已暂时废弃")]
  72. public int Timeout { get; set; } = -1;
  73. /// <summary>
  74. /// 超过Timeout后回调执行
  75. /// </summary>
  76. [Obsolete("此属性已暂时废弃")]
  77. public Action Defaults { get; set; }
  78. /// <summary>
  79. /// 与ActionCnd同时判断, 如果满足AlarmCnd将执行Alarms
  80. /// </summary>
  81. public Func<bool> AlarmCnd { get; set; }
  82. /// <summary>
  83. /// 满足AlarmCnd后执行的动作
  84. /// </summary>
  85. public Action Alarms { get; set; }
  86. public string StepCode { get; set; }
  87. /// <summary>
  88. /// 测试模式下绕过ActionCnd, 通过该延时触发Actions
  89. /// </summary>
  90. public int TestDelay { get; set; }
  91. /// <summary>
  92. /// 是否可用
  93. /// </summary>
  94. public bool IsEnabled { get; set; } = true;
  95. /// <summary>
  96. /// 是否挂起
  97. /// </summary>
  98. public bool IsSuspend { get; set; }
  99. /// <summary>
  100. /// 是否停止
  101. /// </summary>
  102. public bool IsStoped { get; set; }
  103. /// <summary>
  104. /// 顺序号
  105. /// </summary>
  106. public int Seq { get; set; }
  107. /// <summary>
  108. /// 开始时间戳
  109. /// </summary>
  110. public long StartTicks { get; set; }
  111. /// <summary>
  112. /// 判断次数 (ActionCnd未通过后+1)
  113. /// </summary>
  114. public int CheckCount { get; set; }
  115. public ProcessStep()
  116. {
  117. }
  118. public ProcessStep(ProcessStation processStation) : this()
  119. {
  120. ProcessStation = processStation;
  121. }
  122. /// <summary>
  123. /// 获取流程站点的状态
  124. /// </summary>
  125. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  126. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  127. /// <param name="fromStorage">是否从持久化获取, 如果为true并且从持久化获取成功, 将覆盖掉内存中已有的数据</param>
  128. /// <returns></returns>
  129. public int GetStationStatus(int matType, int stationId = -1, bool fromStorage = false) =>
  130. ProcessStation.GetStationStatus(matType, stationId, fromStorage);
  131. /// <summary>
  132. /// 判断流程站点的状态是否在给定的状态值内
  133. /// 只要满足其中一个状态值返回true, 全部不满足返回false
  134. /// </summary>
  135. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  136. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  137. /// <param name="statusVals">需要比较的状态值集合</param>
  138. /// <returns></returns>
  139. public bool CheckStationStatus(int matType, int stationId, int[] statusVals) =>
  140. ProcessStation.CheckStationStatus(matType, stationId, statusVals);
  141. /// <summary>
  142. /// 设置流程站点的状态
  143. /// </summary>
  144. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  145. /// <param name="status">状态值</param>
  146. /// <param name="stationId">站点Id, -1表示本站</param>
  147. /// <param name="toStorage">是否同步持久化保存, 如果为true并且持久化中已有相同matType将会覆盖</param>
  148. public void SetStationStatus(int matType, int status, int stationId = -1, bool toStorage = false) =>
  149. ProcessStation.SetStationStatus(matType, status, stationId, toStorage);
  150. /// <summary>
  151. /// 删除流程站点的状态
  152. /// </summary>
  153. /// <param name="matType"></param>
  154. /// <param name="stationId"></param>
  155. public void RemoveStationStatus(int matType, int stationId = -1) =>
  156. ProcessStation.RemoveStationStatus(matType, stationId);
  157. /// <summary>
  158. /// 清除流程站点的所有状态
  159. /// </summary>
  160. /// <param name="stationId"></param>
  161. public void ClearStationStatus(int stationId = -1) => ProcessStation.ClearStationStatus(stationId);
  162. /// <summary>
  163. /// 在ProcessStationManager中更新当前运行的Step
  164. /// </summary>
  165. public void UpdateRunningStep()
  166. {
  167. ProcessStation.ProcessStationManager.runningSteps[ProcessStation.Id] = this;
  168. }
  169. /// <summary>
  170. /// 等待条件满足
  171. /// </summary>
  172. /// <param name="cnd">满足的条件</param>
  173. /// <param name="onTimeout">超时后动作</param>
  174. /// <param name="timeout">超时时间, 默认60秒</param>
  175. /// <param name="period">条件判断间隔</param>
  176. public void WaitingFor(Func<bool> cnd, Action onTimeout = null, int timeout = 60000, int period = 100)
  177. {
  178. int loops = timeout / period + 1;
  179. for (int i = 0; i < loops; i++)
  180. {
  181. if (cnd.Invoke()) return;
  182. Thread.Sleep(period);
  183. }
  184. if (onTimeout != null) onTimeout.Invoke();
  185. }
  186. /// <summary>
  187. /// 回复Step属性状态
  188. /// </summary>
  189. public void Reset()
  190. {
  191. IsStoped = false;
  192. IsSuspend = false;
  193. InAction = false;
  194. }
  195. /// <summary>
  196. /// 每次执行动作前的准备
  197. /// </summary>
  198. public Action OnPrepare { get; set; }
  199. /// <summary>
  200. /// 回调方法
  201. /// </summary>
  202. public void DoActions()
  203. {
  204. double waitTime = 0;
  205. if (StartTicks > 0)
  206. {
  207. waitTime = (DateTime.Now.Ticks - StartTicks) / 10000d;
  208. }
  209. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]动作开始... 等待用时: {waitTime}");
  210. Stopwatch stopwatch = Stopwatch.StartNew();
  211. // 回复Di初始值
  212. foreach (var diCnd in TriggerDiCnds)
  213. {
  214. diCnd.Reset();
  215. }
  216. InAction = true;
  217. Actions?.Invoke();
  218. InAction = false;
  219. stopwatch.Stop();
  220. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]动作完成, 运行用时: {stopwatch.Elapsed.TotalMilliseconds}");
  221. }
  222. public void DoDefaults()
  223. {
  224. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]等待超时...");
  225. InAction = true;
  226. //Defaults?.Invoke();
  227. InAction = false;
  228. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]超时处理完成");
  229. }
  230. public void DoAlarms()
  231. {
  232. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]告警开始...");
  233. foreach (var diCnd in TriggerDiCnds)
  234. {
  235. diCnd.Reset();
  236. }
  237. InAction = true;
  238. Alarms?.Invoke();
  239. InAction = false;
  240. log.Debug($"任务[{ProcessStation.Id}-{Id}:{Name}]告警处理完成");
  241. }
  242. public void DoStop()
  243. {
  244. IsStoped = true;
  245. if (OnStop != null) OnStop?.Invoke();
  246. InAction = false;
  247. }
  248. /// <summary>
  249. /// 停止
  250. /// </summary>
  251. public Action OnStop { get; set; }
  252. public void DoEmgStop()
  253. {
  254. IsStoped = true;
  255. OnEmgStop?.Invoke();
  256. }
  257. /// <summary>
  258. /// 急停
  259. /// </summary>
  260. public Action OnEmgStop { get; set; }
  261. public void DoPause()
  262. {
  263. IsSuspend = true;
  264. OnPause?.Invoke();
  265. }
  266. public Action OnPause { get; set; }
  267. public void DoResume()
  268. {
  269. IsSuspend = false;
  270. StartTicks = DateTime.Now.Ticks;
  271. OnResume?.Invoke();
  272. }
  273. public Action OnResume { get; set; }
  274. public override bool Equals(object obj)
  275. {
  276. return obj is ProcessStep step && Id == step.Id;
  277. }
  278. public override int GetHashCode()
  279. {
  280. return 2108858624 + Id.GetHashCode();
  281. }
  282. }
  283. }