ProcessStep.cs 10 KB

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