PlatingCellEntity.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Fsm;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.RecipeCenter;
  7. using Aitex.Core.Util;
  8. using Aitex.Core.Utilities;
  9. using MECF.Framework.Common.Equipment;
  10. using MECF.Framework.Common.Persistent.Reservoirs;
  11. using MECF.Framework.Common.ProcessCell;
  12. using MECF.Framework.Common.RecipeCenter;
  13. using MECF.Framework.Common.Routine;
  14. using MECF.Framework.Common.ToolLayout;
  15. using PunkHPX8_Core;
  16. using PunkHPX8_RT.Devices.PlatingCell;
  17. using PunkHPX8_RT.Devices.PowerSupplier;
  18. using PunkHPX8_RT.Devices.Temperature;
  19. using PunkHPX8_RT.Modules.Reservoir;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. namespace PunkHPX8_RT.Modules.PlatingCell
  26. {
  27. public class PlatingCellEntity : Entity, IEntity, IModuleEntity
  28. {
  29. public enum PlatingCellMsg
  30. {
  31. NONE,
  32. Error,
  33. ResumeError,
  34. Initialize,
  35. Manual,
  36. Auto,
  37. CurrentShortTest,
  38. CloseFlowValve,
  39. OpenFlowValve,
  40. RunRecipe,
  41. Abort,
  42. Init
  43. }
  44. #region 常量
  45. private const string STRATUS = "Stratus";
  46. private const string AUTO = "Auto";
  47. private const string MANUAL = "Manual";
  48. private const string DISABLED = "Disabled";
  49. private const string ENGINEERING = "Engineering";
  50. private const string PRODUCTION = "Production";
  51. #endregion
  52. #region 内部变量
  53. /// <summary>
  54. /// 持久化数值
  55. /// </summary>
  56. private PlatingCellPersistentValue _persistentValue;
  57. /// <summary>
  58. /// 当前recipe
  59. /// </summary>
  60. private DepRecipe _currentRecipe;
  61. /// <summary>
  62. /// recipe时间
  63. /// </summary>
  64. private int _recipeTime;
  65. /// <summary>
  66. /// 当前RunRecipe Routine
  67. /// </summary>
  68. private RoutineBase _currentRunRecipeRoutine;
  69. #endregion
  70. #region 属性
  71. /// <summary>
  72. /// 模块名称
  73. /// </summary>
  74. public ModuleName Module { get; private set; }
  75. /// <summary>
  76. /// 是否Init
  77. /// </summary>
  78. public bool IsInit
  79. {
  80. get { return fsm.State == (int)PlatingCellState.Init; }
  81. }
  82. /// <summary>
  83. /// 是否Idle
  84. /// </summary>
  85. public bool IsIdle
  86. {
  87. get
  88. {
  89. return fsm.State == (int)PlatingCellState.Idle;
  90. }
  91. }
  92. /// <summary>
  93. /// 是否错误
  94. /// </summary>
  95. public bool IsError
  96. {
  97. get { return fsm.State == (int)PlatingCellState.Error; }
  98. }
  99. /// <summary>
  100. /// 正在忙碌
  101. /// </summary>
  102. public bool IsBusy
  103. {
  104. get { return fsm.State == (int)PlatingCellState.Initializing; }
  105. }
  106. /// <summary>
  107. /// 化学液
  108. /// </summary>
  109. public string Chemistry
  110. {
  111. get { return _currentRecipe != null ? _currentRecipe.Chemistry : ""; }
  112. }
  113. /// <summary>
  114. /// 是否禁用
  115. /// </summary>
  116. public bool IsDisable { get { return _persistentValue == null || _persistentValue.OperatingMode == DISABLED; } }
  117. /// <summary>
  118. /// 自动模式
  119. /// </summary>
  120. public bool IsAuto { get { return _persistentValue != null && _persistentValue.OperatingMode == AUTO; } }
  121. /// <summary>
  122. /// 自动模式
  123. /// </summary>
  124. public bool IsManual { get { return _persistentValue != null && _persistentValue.OperatingMode == MANUAL; } }
  125. /// <summary>
  126. /// 是否为工程模式
  127. /// </summary>
  128. public bool IsEngineering { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == ENGINEERING; } }
  129. /// <summary>
  130. /// 是否为产品模式
  131. /// </summary>
  132. public bool IsProduction { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == PRODUCTION; } }
  133. /// <summary>
  134. /// 状态机状态
  135. /// </summary>
  136. public PlatingCellState State { get { return (PlatingCellState)fsm.State; } }
  137. /// <summary>
  138. /// 是否初始化完成
  139. /// </summary>
  140. public bool IsInitialized { get { return fsm.State >= (int)PlatingCellState.Initialized; } }
  141. /// <summary>
  142. /// Reservoir项
  143. /// </summary>
  144. private ReservoirItem _reservoirItem;
  145. #endregion
  146. /// <summary>
  147. /// 构造函数
  148. /// </summary>
  149. /// <param name="module"></param>
  150. public PlatingCellEntity(ModuleName module)
  151. {
  152. this.Module = module;
  153. InitializeParameter();
  154. InitialFsm();
  155. }
  156. /// <summary>
  157. /// 初始化
  158. /// </summary>
  159. /// <returns></returns>
  160. protected override bool Init()
  161. {
  162. InitializeRoutine();
  163. InitializeDATA();
  164. InitializeOperation();
  165. return true;
  166. }
  167. /// <summary>
  168. /// 初始化参数
  169. /// </summary>
  170. private void InitializeParameter()
  171. {
  172. _persistentValue = PlatingCellPersistentManager.Instance.GetPlatingCellPersistentValue(Module.ToString());
  173. if (_persistentValue == null)
  174. {
  175. LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module.ToString(), "Persistent Value Object is not exist");
  176. }
  177. }
  178. /// <summary>
  179. /// 初始化Routine
  180. /// </summary>
  181. private void InitializeRoutine()
  182. {
  183. }
  184. /// <summary>
  185. /// 初始化DATA
  186. /// </summary>
  187. private void InitializeDATA()
  188. {
  189. DATA.Subscribe($"{Module}.FsmState", () => ((PlatingCellState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  190. DATA.Subscribe($"{Module}.CurrentRecipe", () => _currentRecipe != null ? _currentRecipe.Ppid : "", SubscriptionAttribute.FLAG.IgnoreSaveDB);
  191. DATA.Subscribe($"{Module}.TotalTime", () => _recipeTime, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  192. DATA.Subscribe($"{Module}.TimeRemain", () => _recipeTime != 0 && _currentRunRecipeRoutine != null ? (_recipeTime - Math.Round((double)_currentRunRecipeRoutine.ElapsedMilliseconds / 1000, 0)) : 0, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  193. DATA.Subscribe($"{Module}.Chemistry", () => _currentRecipe != null ? _currentRecipe.Chemistry : "", SubscriptionAttribute.FLAG.IgnoreSaveDB);
  194. }
  195. /// <summary>
  196. /// 初始化Operation
  197. /// </summary>
  198. private void InitializeOperation()
  199. {
  200. OP.Subscribe($"{Module}.InitializeAll", (cmd, args) => { return CheckToPostMessage<PlatingCellState, ReservoirMsg>(eEvent.ERR_RESERVOIR, Module.ToString(), (int)ReservoirMsg.Initialize); });
  201. }
  202. /// 初始化状态机
  203. /// </summary>
  204. private void InitialFsm()
  205. {
  206. fsm = new StateMachine<PlatingCellEntity>(Module.ToString(), (int)PlatingCellState.Init, 100);
  207. fsm.EnableRepeatedMsg(true);
  208. AnyStateTransition(ReservoirMsg.Error, NullFunc, PlatingCellState.Error);
  209. //Initialized
  210. Transition(PlatingCellState.Error, PlatingCellMsg.Initialize, InitializeAll, PlatingCellState.Initializing);
  211. Transition(PlatingCellState.Init, PlatingCellMsg.Initialize, InitializeAll, PlatingCellState.Initializing);
  212. Transition(PlatingCellState.Idle, PlatingCellMsg.Initialize, InitializeAll, PlatingCellState.Initializing);
  213. Transition(PlatingCellState.Initializing, FSM_MSG.TIMER, InitializeAllMonitor, PlatingCellState.Idle);
  214. //直接进入Idle
  215. Transition(PlatingCellState.Initialized, FSM_MSG.TIMER, NullFunc, PlatingCellState.Idle);
  216. //Enter Init
  217. Transition(PlatingCellState.Idle, ReservoirMsg.Init, NullFunc, PlatingCellState.Init);
  218. EnumLoop<PlatingCellState>.ForEach((item) => { fsm.MapState((int)item, item.ToString()); });
  219. EnumLoop<PlatingCellState>.ForEach((item) => { fsm.MapMessage((int)item, item.ToString()); });
  220. }
  221. #region Initialize All
  222. /// <summary>
  223. /// 初始化
  224. /// </summary>
  225. /// <returns></returns>
  226. private bool InitializeAll(object[] param)
  227. {
  228. if (_persistentValue == null)
  229. {
  230. LOG.WriteLog(eEvent.ERR_RESERVOIR, Module.ToString(), "persistent is null");
  231. return false;
  232. }
  233. return true;
  234. }
  235. /// <summary>
  236. /// Initialize 监控
  237. /// </summary>
  238. /// <param name="param"></param>
  239. /// <returns></returns>
  240. private bool InitializeAllMonitor(object[] param)
  241. {
  242. RState ret = RState.Init;
  243. if (ret == RState.Failed || ret == RState.Timeout)
  244. {
  245. PostMsg(ReservoirMsg.Error);
  246. return false;
  247. }
  248. return ret == RState.End;
  249. }
  250. #endregion
  251. /// <summary>
  252. /// EnterInit
  253. /// </summary>
  254. public void EnterInit()
  255. {
  256. if ((PlatingCellState)fsm.State != PlatingCellState.Idle) return;
  257. else
  258. {
  259. CheckToPostMessage<PlatingCellState, PlatingCellMsg>(eEvent.ERR_PLATINGCELL, Module.ToString(), (int)PlatingCellMsg.Init);
  260. }
  261. }
  262. public bool Check(int msg, out string reason, params object[] args)
  263. {
  264. reason = "";
  265. return true;
  266. }
  267. public bool CheckAcked(int msg)
  268. {
  269. throw new NotImplementedException();
  270. }
  271. public int Invoke(string function, params object[] args)
  272. {
  273. throw new NotImplementedException();
  274. }
  275. }
  276. }