VpwCellEntity.cs 17 KB

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