VpwCellEntity.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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.Util;
  7. using Aitex.Core.Utilities;
  8. using MECF.Framework.Common.Alarm;
  9. using MECF.Framework.Common.CommonData;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.Persistent.Temperature;
  12. using MECF.Framework.Common.Persistent.VpwCell;
  13. using MECF.Framework.Common.Persistent.VpwMain;
  14. using MECF.Framework.Common.RecipeCenter;
  15. using MECF.Framework.Common.Routine;
  16. using MECF.Framework.Common.SubstrateTrackings;
  17. using MECF.Framework.Common.ToolLayout;
  18. using PunkHPX8_Core;
  19. using PunkHPX8_RT.Devices.VpwCell;
  20. using PunkHPX8_RT.Modules.VpwCell;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. namespace PunkHPX8_RT.Modules.VpwMain
  28. {
  29. public class VpwCellEntity : Entity, IEntity, IModuleEntity
  30. {
  31. public enum MSG
  32. {
  33. Home
  34. }
  35. #region 常量
  36. private const string STRATUS = "Stratus";
  37. private const string AUTO = "Auto";
  38. private const string MANUAL = "Manual";
  39. private const string DISABLED = "Disabled";
  40. private const string ENGINEERING = "Engineering";
  41. private const string PRODUCTION = "Production";
  42. #endregion
  43. #region 内部变量
  44. /// <summary>
  45. /// 持久化数值
  46. /// </summary>
  47. private VpwCellPersistentValue _persistentValue;
  48. /// <summary>
  49. /// VPW cell集合
  50. /// </summary>
  51. private List<VpwCellDevice> _vpwCellDevices = new List<VpwCellDevice>();
  52. /// <summary>
  53. /// Home Routine
  54. /// </summary>
  55. private VPWHomeRoutine _homeRoutine;
  56. /// <summary>
  57. /// Prepare
  58. /// </summary>
  59. private VpwPrepareRoutine _prepareRoutine;
  60. /// <summary>
  61. /// recipe routine
  62. /// </summary>
  63. private VpwRecipeRoutine _recipeRoutine;
  64. /// <summary>
  65. /// 手动recipe routine
  66. /// </summary>
  67. private VpwManualRecipeRoutine _manualRecipeRoutine;
  68. #endregion
  69. #region 属性
  70. public ModuleName Module { get; private set; }
  71. /// <summary>
  72. /// 是否Init
  73. /// </summary>
  74. public bool IsInit
  75. {
  76. get { return fsm.State == (int)VPWCellState.Init; }
  77. }
  78. /// <summary>
  79. /// 是否Idle
  80. /// </summary>
  81. public bool IsIdle
  82. {
  83. get
  84. {
  85. return fsm.State == (int)VPWCellState.Idle;
  86. }
  87. }
  88. /// <summary>
  89. /// 是否错误
  90. /// </summary>
  91. public bool IsError
  92. {
  93. get { return fsm.State == (int)VPWCellState.Error; }
  94. }
  95. /// <summary>
  96. /// 正在忙碌
  97. /// </summary>
  98. public bool IsBusy
  99. {
  100. get { return fsm.State == (int)VPWCellState.Initializing; }
  101. }
  102. /// <summary>
  103. /// 是否禁用
  104. /// </summary>
  105. public bool IsDisable { get { return _persistentValue == null || _persistentValue.OperatingMode == DISABLED; } }
  106. /// <summary>
  107. /// 自动模式
  108. /// </summary>
  109. public bool IsAuto { get { return _persistentValue != null && _persistentValue.OperatingMode == AUTO; } }
  110. /// <summary>
  111. /// 自动模式
  112. /// </summary>
  113. public bool IsManual { get { return _persistentValue != null && _persistentValue.OperatingMode == MANUAL; } }
  114. /// <summary>
  115. /// 是否为工程模式
  116. /// </summary>
  117. public bool IsEngineering { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == ENGINEERING; } }
  118. /// <summary>
  119. /// 是否为产品模式
  120. /// </summary>
  121. public bool IsProduction { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == PRODUCTION; } }
  122. #endregion
  123. /// <summary>
  124. /// 构造函数
  125. /// </summary>
  126. /// <param name="module"></param>
  127. public VpwCellEntity(ModuleName module)
  128. {
  129. this.Module = module;
  130. }
  131. /// <summary>
  132. /// 初始化
  133. /// </summary>
  134. /// <returns></returns>
  135. protected override bool Init()
  136. {
  137. InitialFsm();
  138. InitializeParameter();
  139. InitializeRoutine();
  140. InitializeDATA();
  141. InitializeOperation();
  142. return true;
  143. }
  144. /// <summary>
  145. /// 初始化参数
  146. /// </summary>
  147. private void InitializeParameter()
  148. {
  149. _persistentValue = VpwCellPersistentManager.Instance.GetPersistentValue(Module.ToString());
  150. if (_persistentValue == null)
  151. {
  152. LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), "Persistent Value Object is not exist");
  153. }
  154. _vpwCellDevices.Clear();
  155. VpwMainItem vpwMainItem = VpwMainItemManager.Instance.GetItem(ModuleName.VPWMain1.ToString());
  156. if (vpwMainItem == null || vpwMainItem.VpwCells == null)
  157. {
  158. return;
  159. }
  160. foreach (var item in vpwMainItem.VpwCells)
  161. {
  162. VpwCellDevice cellDevice = DEVICE.GetDevice<VpwCellDevice>(item.ModuleName);
  163. _vpwCellDevices.Add(cellDevice);
  164. }
  165. }
  166. /// <summary>
  167. /// 初始化状态机
  168. /// </summary>
  169. private void InitialFsm()
  170. {
  171. fsm = new StateMachine<VpwCellEntity>(Module.ToString(), (int)VPWCellState.Init, 100);
  172. fsm.EnableRepeatedMsg(true);
  173. AnyStateTransition(VpwCellMsg.Error, NullFunc, VPWCellState.Error);
  174. //Initialized
  175. Transition(VPWCellState.Error, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
  176. Transition(VPWCellState.Init, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
  177. Transition(VPWCellState.Idle, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
  178. Transition(VPWCellState.Initializing, FSM_MSG.TIMER, InitializeAllMonitor, VPWCellState.Idle);
  179. Transition(VPWCellState.Error, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
  180. Transition(VPWCellState.Init, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
  181. Transition(VPWCellState.Idle, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
  182. //Enter Init
  183. Transition(VPWCellState.Idle, VpwCellMsg.Init, NullFunc, VPWCellState.Init);
  184. //Manual Recipe
  185. Transition(VPWCellState.Idle, VpwCellMsg.ManualRecipe, ManualRunRecipe, VPWCellState.ManualReciping);
  186. Transition(VPWCellState.ManualReciping, FSM_MSG.TIMER, ManualRunRecipeMonitor, VPWCellState.Idle);
  187. //Prepare
  188. Transition(VPWCellState.Idle, VpwCellMsg.Prepare, Prepare, VPWCellState.Preparing);
  189. Transition(VPWCellState.Preparing, FSM_MSG.TIMER, PrepareMonitor, VPWCellState.WaitForRunRecipe);
  190. Transition(VPWCellState.WaitForRunRecipe, VpwCellMsg.RunRecipe, RunRecipe, VPWCellState.RunReciping);
  191. Transition(VPWCellState.RunReciping, FSM_MSG.TIMER, RunRecipeMonitor, VPWCellState.Idle);
  192. //Retry
  193. Transition(VPWCellState.Error, VpwCellMsg.Retry, NullFunc, VPWCellState.Retrying);
  194. Transition(VPWCellState.Retrying, FSM_MSG.TIMER, VpwCellRetry, VPWCellState.Retrying);
  195. Transition(VPWCellState.Retrying, VpwCellMsg.Prepare, RetryPrepare, VPWCellState.Preparing);
  196. Transition(VPWCellState.Retrying, VpwCellMsg.RunRecipe, RetryRunRecipe, VPWCellState.RunReciping);
  197. }
  198. /// <summary>
  199. /// 初始化数据
  200. /// </summary>
  201. private void InitializeDATA()
  202. {
  203. InitializeSVID();
  204. DATA.Subscribe($"{Module}.FsmState", () => ((VPWCellState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  205. DATA.Subscribe($"{Module}.IsIdle", () => IsIdle, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  206. DATA.Subscribe($"{Module}.IsInit", () => IsInit, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  207. DATA.Subscribe($"{Module}.IsDisable", () => IsDisable, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  208. DATA.Subscribe($"{Module}.IsBusy", () => IsBusy, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  209. DATA.Subscribe($"{Module}.IsError", () => IsError, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  210. }
  211. /// <summary>
  212. /// 初始化SVID
  213. /// </summary>
  214. private void InitializeSVID()
  215. {
  216. DATA.Subscribe($"{Module}.OperatingMode", () => _persistentValue != null ? _persistentValue.OperatingMode : "None", SubscriptionAttribute.FLAG.IgnoreSaveDB);
  217. }
  218. /// <summary>
  219. /// 初始化Routine
  220. /// </summary>
  221. private void InitializeRoutine()
  222. {
  223. _homeRoutine = new VPWHomeRoutine(Module.ToString());
  224. _prepareRoutine=new VpwPrepareRoutine(Module.ToString());
  225. _recipeRoutine=new VpwRecipeRoutine(Module.ToString());
  226. _manualRecipeRoutine=new VpwManualRecipeRoutine(Module.ToString());
  227. }
  228. /// <summary>
  229. /// 初始化操作
  230. /// </summary>
  231. private void InitializeOperation()
  232. {
  233. OP.Subscribe($"{Module}.InitializeAll", (cmd, args) => { return CheckToPostMessage<VPWCellState, VpwCellMsg>(eEvent.ERR_VPW, Module.ToString(), (int)VpwCellMsg.Initialize); });
  234. OP.Subscribe($"{Module}.Prepare", (cmd, args) => { return CheckToPostMessage<VPWCellState, VpwCellMsg>(eEvent.ERR_VPW, Module.ToString(), (int)VpwCellMsg.Prepare); });
  235. }
  236. #region InitializeAll
  237. /// <summary>
  238. /// Initialize
  239. /// </summary>
  240. /// <param name="param"></param>
  241. /// <returns></returns>
  242. private bool InitializeAll(object[] param)
  243. {
  244. if (_vpwCellDevices == null || _vpwCellDevices.Count == 0)
  245. {
  246. LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), "cell device is empty");
  247. return false;
  248. }
  249. foreach (var device in _vpwCellDevices)
  250. {
  251. VpwCellEntity vpwCellEntity = Singleton<RouteManager>.Instance.GetModule<VpwCellEntity>(device.Module);
  252. if (vpwCellEntity.IsBusy)
  253. {
  254. LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), $"cell device {device.Module} is busy,cannot initialize");
  255. return false;
  256. }
  257. }
  258. return _homeRoutine.Start(_vpwCellDevices) == RState.Running;
  259. }
  260. /// <summary>
  261. /// Initialize 监控
  262. /// </summary>
  263. /// <param name="param"></param>
  264. /// <returns></returns>
  265. private bool InitializeAllMonitor(object[] param)
  266. {
  267. RState ret = _homeRoutine.Monitor();
  268. if (ret == RState.Failed || ret == RState.Timeout)
  269. {
  270. PostMsg(VpwCellMsg.Error);
  271. return false;
  272. }
  273. return ret == RState.End;
  274. }
  275. #endregion
  276. #region Prepare
  277. /// <summary>
  278. /// Prepare
  279. /// </summary>
  280. /// <param name="param"></param>
  281. /// <returns></returns>
  282. private bool Prepare(object[] param)
  283. {
  284. VpwRecipe recipe = param[0] as VpwRecipe;
  285. return _prepareRoutine.Start(recipe) == RState.Running;
  286. }
  287. /// <summary>
  288. /// Prepare 监控
  289. /// </summary>
  290. /// <param name="param"></param>
  291. /// <returns></returns>
  292. private bool PrepareMonitor(object[] param)
  293. {
  294. RState ret = _prepareRoutine.Monitor();
  295. if (ret == RState.Failed || ret == RState.Timeout)
  296. {
  297. AlarmList alarmList = new AlarmList(Module.ToString(), ((VPWCellState)fsm.State).ToString(), (int)VpwCellMsg.Prepare,
  298. _prepareRoutine.ErrorMsg, _prepareRoutine.ErrorStep, (int)AlarmType.Error);
  299. AlarmListManager.Instance.AddAlarm(alarmList);
  300. PostMsg(VpwCellMsg.Error);
  301. return false;
  302. }
  303. return ret == RState.End;
  304. }
  305. /// <summary>
  306. /// Retry Prepare
  307. /// </summary>
  308. /// <param name="param"></param>
  309. /// <returns></returns>
  310. private bool RetryPrepare(object[] param)
  311. {
  312. int stepIndex = (int)param[0];
  313. bool result = _prepareRoutine.Retry(stepIndex) == RState.Running;
  314. return result;
  315. }
  316. #endregion
  317. #region Run Recipe
  318. /// <summary>
  319. /// run recipe
  320. /// </summary>
  321. /// <param name="param"></param>
  322. /// <returns></returns>
  323. private bool RunRecipe(object[] param)
  324. {
  325. VpwRecipe recipe = param[0] as VpwRecipe;
  326. return _recipeRoutine.Start(recipe) == RState.Running;
  327. }
  328. /// <summary>
  329. /// Prepare 监控
  330. /// </summary>
  331. /// <param name="param"></param>
  332. /// <returns></returns>
  333. private bool RunRecipeMonitor(object[] param)
  334. {
  335. RState ret = _recipeRoutine.Monitor();
  336. if (ret == RState.Failed || ret == RState.Timeout)
  337. {
  338. AlarmList alarmList = new AlarmList(Module.ToString(), ((VPWCellState)fsm.State).ToString(), (int)VpwCellMsg.RunRecipe,
  339. _recipeRoutine.ErrorMsg, _recipeRoutine.ErrorStep, (int)AlarmType.Error);
  340. AlarmListManager.Instance.AddAlarm(alarmList);
  341. PostMsg(VpwCellMsg.Error);
  342. return false;
  343. }
  344. return ret == RState.End;
  345. }
  346. /// <summary>
  347. /// Retry RunRecipe
  348. /// </summary>
  349. /// <param name="param"></param>
  350. /// <returns></returns>
  351. private bool RetryRunRecipe(object[] param)
  352. {
  353. int stepIndex = (int)param[0];
  354. bool result = _recipeRoutine.Retry(stepIndex) == RState.Running;
  355. return result;
  356. }
  357. #endregion
  358. #region Manual Run Recipe
  359. /// <summary>
  360. /// run recipe
  361. /// </summary>
  362. /// <param name="param"></param>
  363. /// <returns></returns>
  364. private bool ManualRunRecipe(object[] param)
  365. {
  366. VpwRecipe recipe = param[0] as VpwRecipe;
  367. return _manualRecipeRoutine.Start(recipe) == RState.Running;
  368. }
  369. /// <summary>
  370. /// Prepare 监控
  371. /// </summary>
  372. /// <param name="param"></param>
  373. /// <returns></returns>
  374. private bool ManualRunRecipeMonitor(object[] param)
  375. {
  376. RState ret = _manualRecipeRoutine.Monitor();
  377. if (ret == RState.Failed || ret == RState.Timeout)
  378. {
  379. PostMsg(VpwCellMsg.Error);
  380. return false;
  381. }
  382. return ret == RState.End;
  383. }
  384. #endregion
  385. #region VpwCell Retry
  386. /// <summary>
  387. /// VpwCell
  388. /// </summary>
  389. /// <param name="param"></param>
  390. /// <returns></returns>
  391. private bool VpwCellRetry(object[] param)
  392. {
  393. AlarmList alarmList = AlarmListManager.Instance.GetAlarmListByModule(Module.ToString());
  394. if (alarmList != null)
  395. {
  396. CheckToPostMessage<VPWCellState, VpwCellMsg>(eEvent.WARN_VPW, Module.ToString(), alarmList.ModuleCmd,
  397. alarmList.ModuleStep);
  398. }
  399. return false;
  400. }
  401. #endregion
  402. public bool Check(int msg, out string reason, params object[] args)
  403. {
  404. reason = "";
  405. return false;
  406. }
  407. public bool CheckAcked(int msg)
  408. {
  409. return false;
  410. }
  411. /// <summary>
  412. /// EnterInit
  413. /// </summary>
  414. public void EnterInit()
  415. {
  416. }
  417. public int Invoke(string function, params object[] args)
  418. {
  419. switch (function)
  420. {
  421. case "HomeAll":
  422. return (int)MSG.Home;
  423. }
  424. return (int)FSM_MSG.NONE;
  425. }
  426. }
  427. }