ProcessStation.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using SKMC.Api.Common;
  2. using SKMC.Api.Common.Tasks;
  3. using SKMC.Api.Motion.Control;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. namespace SKMC.Api.Process.Model
  8. {
  9. /// <summary>
  10. /// <p>流程站点</p>
  11. /// <br>每个流程站点由多个ProcessStep组成一个循环流程</br>
  12. /// </summary>
  13. public abstract class ProcessStation
  14. {
  15. protected static readonly ILogger log = LogFactory.Get();
  16. /// <summary>
  17. /// 线性流程引擎
  18. /// </summary>
  19. private readonly ProcessFlow processFlow = ObjectFactory.Create<ProcessFlow>();
  20. /// <summary>
  21. /// 持久化接口
  22. /// </summary>
  23. protected readonly ProcessStorage processStorage = ObjectFactory.Create<ProcessStorage>();
  24. /// <summary>
  25. /// 流程站点管理器
  26. /// </summary>
  27. public ProcessStationManager ProcessStationManager { get; set; }
  28. /// <summary>
  29. /// 物料状态, 使用物料类型、状态值组合
  30. /// </summary>
  31. public ConcurrentDictionary<int, int> MatStatuses { get; set; } = new ConcurrentDictionary<int, int>();
  32. /// <summary>
  33. /// 流程站点的内置数据集
  34. /// </summary>
  35. public ConcurrentDictionary<string, object> DataMapping { get; set; } = new ConcurrentDictionary<string, object>();
  36. /// <summary>
  37. /// 流程动作控制接口
  38. /// </summary>
  39. protected readonly IMotionControl motionControl = ObjectFactory.Resolve<IMotionControl>();
  40. private int id;
  41. public int Id
  42. {
  43. get { return id; }
  44. set { id = value; processFlow.Id = value; }
  45. }
  46. public string Name { get; set; }
  47. /// <summary>
  48. /// station复位时执行
  49. /// </summary>
  50. public Action OnReset { get; set; }
  51. /// <summary>
  52. /// station复位结果, 0:复位成功
  53. /// </summary>
  54. public int ResetResult { get; set; } = -1;
  55. /// <summary>
  56. /// station启动时执行
  57. /// </summary>
  58. public Action OnStart { get; set; }
  59. /// <summary>
  60. /// station停止后执行
  61. /// </summary>
  62. public Action OnStop { get; set; }
  63. /// <summary>
  64. /// station暂停后执行
  65. /// </summary>
  66. public Action OnPause { get; set; }
  67. /// <summary>
  68. /// station暂停恢复后执行
  69. /// </summary>
  70. public Action OnResume { get; set; }
  71. /// <summary>
  72. /// station恢复状态后执行
  73. /// </summary>
  74. public Action OnRestore { get; set; }
  75. /// <summary>
  76. /// 每轮循环开始前执行
  77. /// </summary>
  78. public Action OnPrepare { set => processFlow.OnPrepare = value; }
  79. /// <summary>
  80. /// 触发入料事件时执行
  81. /// </summary>
  82. public Action Onload { get; set; }
  83. /// <summary>
  84. /// 完全收到物料准备开工时
  85. /// </summary>
  86. public Action OnReady { get; set; }
  87. /// <summary>
  88. /// 触发出料事件后执行
  89. /// </summary>
  90. public Action OnUnload { get; set; }
  91. /// <summary>
  92. /// 物料完全离开后执行
  93. /// </summary>
  94. public Action OnExit { get; set; }
  95. /// <summary>
  96. /// 是否空闲(站内没有物料)
  97. /// </summary>
  98. public bool IsIdle { get; set; }
  99. /// <summary>
  100. /// 安全运行的判断条件
  101. /// </summary>
  102. public Func<bool> SafeChecker { get; set; }
  103. /// <summary>
  104. /// 站点运行状态, 同设备状态, 见DeviceStatusEnum
  105. /// </summary>
  106. public int RunStatus { get; set; }
  107. /// <summary>
  108. /// 多工位序号, 同一类ProcessStation的多个实例, 使用该属性区分
  109. /// </summary>
  110. public short StationIndex { get; set; }
  111. /// <summary>
  112. /// 获取流程步骤
  113. /// </summary>
  114. /// <param name="processStepId"> 流程步骤Id</param>
  115. /// <returns></returns>
  116. public virtual ProcessStep Get(int processStepId) => processFlow.Get(processStepId);
  117. /// <summary>
  118. /// 注册流程步骤
  119. /// </summary>
  120. /// <param name="processStep">流程步骤</param>
  121. public virtual void Register(ProcessStep processStep) => processFlow.Register(processStep);
  122. /// <summary>
  123. /// 设置流程起始的步骤, 后续流程不变
  124. /// </summary>
  125. /// <param name="processStep">流程步骤</param>
  126. public virtual void SetFirst(ProcessStep processStep) => processFlow.SetFirst(processStep);
  127. /// <summary>
  128. /// 设置流程起始的步骤, 后续流程不变
  129. /// </summary>
  130. /// <param name="processStepId">流程步骤Id</param>
  131. public virtual void SetFirst(int processStepId) => processFlow.SetFirst(processStepId);
  132. /// <summary>
  133. /// 提交下个流程步骤
  134. /// </summary>
  135. public virtual void PostAutoNext() => processFlow.PostAutoNext();
  136. /// <summary>
  137. /// 提交指定的流程步骤
  138. /// </summary>
  139. /// <param name="processStep">流程步骤</param>
  140. public virtual void PostManual(ProcessStep processStep) => processFlow.PostManual(processStep);
  141. /// <summary>
  142. /// 提交指定的流程步骤
  143. /// </summary>
  144. /// <param name="processStepId">流程步骤Id</param>
  145. public virtual void PostManual(int processStepId) => processFlow.PostManual(Get(processStepId));
  146. /// <summary>
  147. /// 直接调用一个流程步骤的Actions(自动模式的下一个)
  148. /// </summary>
  149. public virtual void CallAutoNext() => processFlow.CallAutoNext();
  150. /// <summary>
  151. /// 直接调用一个流程步骤的Actions(手动模式指定)
  152. /// </summary>
  153. /// <param name="processStep">流程步骤</param>
  154. public virtual void CallManual(ProcessStep processStep) => processFlow.CallManual(processStep);
  155. /// <summary>
  156. /// 直接调用一个流程步骤的Actions(手动模式指定)
  157. /// </summary>
  158. /// <param name="processStepId">流程步骤Id</param>
  159. public virtual void CallManual(int processStepId) => processFlow.CallManual(Get(processStepId));
  160. /// <summary>
  161. /// 获取数据对象
  162. /// </summary>
  163. /// <typeparam name="T"></typeparam>
  164. /// <param name="key"></param>
  165. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  166. /// <param name="fromStorage">是否从持久化获取, 如果为true并且从持久化获取成功, 将覆盖掉内存中已有的数据</param>
  167. /// <returns></returns>
  168. public virtual T GetData<T>(string key, int stationId = -1, bool fromStorage = false)
  169. {
  170. //if (fromStorage) LoadStationData(new[] { key }, true);
  171. //GetProcessStation(stationId).DataMapping.TryGetValue(key, out object dataVal);
  172. //return (T) dataVal;
  173. ProcessStation processStation = GetProcessStation(stationId);
  174. if (fromStorage) processStorage.LoadStationData(this, new[] { key }, true);
  175. if (processStation.DataMapping.ContainsKey(key))
  176. {
  177. if (processStation.DataMapping[key] != null)
  178. {
  179. T t = (T)processStation.DataMapping[key];
  180. return t;
  181. }
  182. }
  183. return default;
  184. }
  185. /// <summary>
  186. /// 获取数据对象并从数据集中删除
  187. /// </summary>
  188. /// <typeparam name="T"></typeparam>
  189. /// <param name="key"></param>
  190. /// <param name="stationId"></param>
  191. /// <returns></returns>
  192. public virtual T TakeData<T>(string key, int stationId = -1)
  193. {
  194. ProcessStation processStation = GetProcessStation(stationId);
  195. if (processStation.DataMapping.ContainsKey(key))
  196. {
  197. if (processStation.DataMapping[key] != null)
  198. {
  199. T t = (T)processStation.DataMapping[key];
  200. processStation.DataMapping.TryRemove(key, out _);
  201. return t;
  202. }
  203. }
  204. //return default;
  205. throw new KeyNotFoundException($"TakeData Failed, can't find key[{key}] from station[{processStation.Id}]");
  206. }
  207. /// <summary>
  208. /// 添加数据对象
  209. /// </summary>
  210. /// <param name="key"></param>
  211. /// <param name="value"></param>
  212. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  213. /// <param name="toStorage">是否持久化保存, 如果为true将覆盖掉持久化中已有的数据</param>
  214. public virtual void SetData(string key, object value, int stationId = -1, bool toStorage = false)
  215. {
  216. GetProcessStation(stationId).DataMapping.AddOrUpdate(key, value, (k, oldValue) => value);
  217. if (stationId == -1) stationId = Id;
  218. log.Debug($"Set Station[{stationId}] Data , key: {key}, value: {value}");
  219. if (toStorage)
  220. {
  221. processStorage.SaveStationData(this, new[] { key }, true);
  222. }
  223. }
  224. /// <summary>
  225. /// 删除数据对象
  226. /// </summary>
  227. /// <param name="key"></param>
  228. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  229. /// <param name="toStorage">是否持久化同步删除</param>
  230. public virtual void RemoveData(string key, int stationId = -1, bool toStorage = false)
  231. {
  232. bool result = GetProcessStation(stationId).DataMapping.TryRemove(key, out _);
  233. if (result && toStorage) processStorage.DeleteStationData(this, new[] { key });
  234. }
  235. /// <summary>
  236. /// 清除本站内的数据对象
  237. /// </summary>
  238. public void ClearData(int stationId = -1, bool toStorage = false)
  239. {
  240. GetProcessStation(stationId).DataMapping.Clear();
  241. if (toStorage) processStorage.DeleteStationDataAll(this);
  242. }
  243. /// <summary>
  244. /// 检查任务中断点是否触发
  245. /// </summary>
  246. /// <param name="onCancel">触发中断后的动作</param>
  247. /// <returns></returns>
  248. public virtual bool CheckTaskCancelled(Action onCancel = null)
  249. {
  250. TaskTokener.SetWaitPoint(Id);
  251. if (TaskTokener.IsCancelled(Id))
  252. {
  253. onCancel?.Invoke();
  254. return true;
  255. }
  256. return false;
  257. }
  258. /// <summary>
  259. /// 启动流程
  260. /// </summary>
  261. public virtual void Start()
  262. {
  263. processFlow.Start();
  264. TaskTokener.Add(Id);
  265. OnStart?.Invoke();
  266. processFlow.PostFirst();
  267. }
  268. /// <summary>
  269. /// 停止流程
  270. /// <param name="hasAction">是否有后续停止动作</param>
  271. /// </summary>
  272. public virtual void Stop(bool hasAction = false)
  273. {
  274. processFlow.Stop();
  275. TaskTokener.Stop(Id);
  276. if (hasAction) OnStop?.Invoke();
  277. }
  278. /// <summary>
  279. /// 复位流程
  280. /// </summary>
  281. public virtual void Reset()
  282. {
  283. processFlow.Reset();
  284. TaskTokener.Add(Id);
  285. OnReset?.Invoke();
  286. }
  287. /// <summary>
  288. /// 暂停流程
  289. /// </summary>
  290. public virtual void Pause()
  291. {
  292. processFlow.Pause();
  293. TaskTokener.Pause(Id);
  294. OnPause?.Invoke();
  295. }
  296. /// <summary>
  297. /// 恢复流程
  298. /// </summary>
  299. public virtual void Resume()
  300. {
  301. processFlow.Resume();
  302. OnResume?.Invoke();
  303. TaskTokener.Resume(Id);
  304. }
  305. public virtual void Restore()
  306. {
  307. OnRestore?.Invoke();
  308. TaskTokener.Add(Id);
  309. }
  310. /// <summary>
  311. /// 获取流程站点的物料状态
  312. /// </summary>
  313. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  314. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  315. /// <param name="fromStorage">是否从持久化获取, 如果为true并且从持久化获取成功, 将覆盖掉内存中已有的数据</param>
  316. /// <returns></returns>
  317. public int GetStationStatus(int matType, int stationId = -1, bool fromStorage = false)
  318. {
  319. if (fromStorage) processStorage.LoadStationStatus(this, new[] { matType }, true);
  320. GetProcessStation(stationId).MatStatuses.TryGetValue(matType, out int statusVal);
  321. return statusVal;
  322. }
  323. /// <summary>
  324. /// 判断流程站点的物料状态是否在给定的物料状态值内
  325. /// 只要满足其中一个状态值返回true, 全部不满足返回false
  326. /// </summary>
  327. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  328. /// <param name="stationId">流程站点的Id, 如果是本站则为-1</param>
  329. /// <param name="statusVals">需要比较的物料状态值集合</param>
  330. /// <returns></returns>
  331. public bool CheckStationStatus(int matType, int stationId, int[] statusVals)
  332. {
  333. int statusVal = GetStationStatus(matType, stationId);
  334. bool checkResult = false;
  335. foreach (int val in statusVals)
  336. {
  337. if (val == statusVal) return true;
  338. }
  339. return checkResult;
  340. }
  341. /// <summary>
  342. /// 设置流程站点的物料状态
  343. /// </summary>
  344. /// <param name="matType">状态类型, 可以是物料也可以是站点</param>
  345. /// <param name="status">状态值</param>
  346. /// <param name="stationId">站点Id, -1表示本站</param>
  347. /// <param name="toStorage">是否持久化保存, 如果为true将覆盖掉持久化中已有的数据</param>
  348. public void SetStationStatus(int matType, int status, int stationId = -1, bool toStorage = false)
  349. {
  350. int newStatus = GetProcessStation(stationId).MatStatuses.AddOrUpdate(matType, status, (key, oldValue) => status);
  351. if (stationId == -1) stationId = Id;
  352. log.Debug($"Set Station[{stationId}] Status, matType: {matType}, status check: {status == newStatus}, val: (expected:{status}, actual:{newStatus})");
  353. if (toStorage)
  354. {
  355. processStorage.SaveStationStatus(this, new[] { matType }, true);
  356. }
  357. }
  358. /// <summary>
  359. /// 删除流程站点的物料状态
  360. /// </summary>
  361. /// <param name="matType"></param>
  362. /// <param name="stationId"></param>
  363. /// <param name="toStorage">是否同步删除持久化中的物料状态</param>
  364. public void RemoveStationStatus(int matType, int stationId = -1, bool toStorage = false)
  365. {
  366. bool result = GetProcessStation(stationId).MatStatuses.TryRemove(matType, out _);
  367. if (result && toStorage) processStorage.DeleteStationStatus(this, new[] { matType });
  368. }
  369. /// <summary>
  370. /// 清除流程站点的所有状态
  371. /// </summary>
  372. /// <param name="stationId"></param>
  373. public void ClearStationStatus(int stationId = -1, bool toStorage = false)
  374. {
  375. GetProcessStation(stationId).MatStatuses.Clear();
  376. if (toStorage) processStorage.DeleteStationStatusAll(this);
  377. }
  378. private ProcessStation GetProcessStation(int stationId = -1)
  379. {
  380. ProcessStation processStation = (stationId == -1) ? this : ProcessStationManager.Get(stationId);
  381. if (processStation == null) throw new ArgumentNullException($"Cannot Find stationId[{stationId}] Object");
  382. return processStation;
  383. }
  384. }
  385. }