RouteManager.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Fsm;
  8. using Aitex.Core.Common;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.Util;
  14. using MECF.Framework.Common.Jobs;
  15. using MECF.Framework.Common.Equipment;
  16. using MECF.Framework.Common.SubstrateTrackings;
  17. using PunkHPX8_Core;
  18. using Aitex.Core.RT.Log;
  19. using SecsGem.Core.Application;
  20. using PunkHPX8_RT.Modules.SRD;
  21. using Aitex.Core.RT.Device;
  22. using MECF.Framework.Common.ToolLayout;
  23. using MECF.Framework.Common.Routine;
  24. using PunkHPX8_RT.Dispatch;
  25. using PunkHPX8_RT.Modules.Reservoir;
  26. using System.IO;
  27. using PunkHPX8_RT.Modules.Transporter;
  28. using PunkHPX8_RT.Devices.SRD;
  29. using MECF.Framework.Common.Alarm;
  30. using System.Diagnostics;
  31. using PunkHPX8_RT.Schedulers;
  32. using MECF.Framework.Common.ProcessCell;
  33. using System.Reflection;
  34. using PunkHPX8_RT.Modules.VpwMain;
  35. using PunkHPX8_RT.Modules.PlatingCell;
  36. namespace PunkHPX8_RT.Modules
  37. {
  38. class RouteManager : Entity, IEntity
  39. {
  40. public enum MSG
  41. {
  42. MoveWafer,
  43. ReturnWafer,
  44. HomeUnit,
  45. PauseAuto,
  46. ResumeAuto,
  47. Stop,
  48. StartCycle,
  49. StopCycle,
  50. HOME,
  51. RESET,
  52. ABORT,
  53. ERROR,
  54. SetAutoMode,
  55. SetManualMode,
  56. ResetIdleCleanTime,
  57. ResetIdlePurgeTime,
  58. CreateJob,
  59. PauseJob,
  60. PauseAllJobs,
  61. ResumeJob,
  62. ResumeAllJobs,
  63. StartJob,
  64. StopJob,
  65. AbortJob,
  66. JobDone,
  67. CassetteLeave, //For unload light control off afer job done
  68. Map,
  69. ReturnAllWafer,
  70. TMCycle,
  71. Retry,
  72. ConfirmComplete,
  73. ResetWafers,
  74. FaStartJob,
  75. FaStopJob,
  76. }
  77. #region 属性
  78. public string Name { get; set; }
  79. public bool IsAutoMode
  80. {
  81. get
  82. {
  83. return fsm.State == (int)RtState.AutoRunning || fsm.State == (int)RtState.AutoIdle||fsm.State==(int)RtState.AutoAborting;
  84. }
  85. }
  86. public bool IsFaAutoMode
  87. {
  88. get
  89. {
  90. return fsm.State == (int)RtState.AutoRunning || fsm.State == (int)RtState.AutoIdle;
  91. }
  92. }
  93. public bool IsInit
  94. {
  95. get { return fsm.State == (int)RtState.Init; }
  96. }
  97. public bool IsIdle
  98. {
  99. get { return fsm.State == (int)RtState.Idle || fsm.State == (int)RtState.AutoIdle; }
  100. }
  101. public bool IsAlarm
  102. {
  103. get { return fsm.State == (int)RtState.Error; }
  104. }
  105. public bool IsPaused
  106. {
  107. get { return _jobCycle.CycleState == RState.Paused; }
  108. }
  109. public bool IsRunning
  110. {
  111. get
  112. {
  113. return !IsInit && !IsAlarm && !IsIdle;
  114. }
  115. }
  116. /// <summary>
  117. /// 是否AutoRunning
  118. /// </summary>
  119. public bool IsAutoRunning
  120. {
  121. get { return fsm.State == (int)RtState.AutoRunning||fsm.State==(int)RtState.AutoAborting; }
  122. }
  123. /// <summary>
  124. /// 是否
  125. /// </summary>
  126. public bool IsAutoIdle
  127. {
  128. get { return fsm.State == (int)RtState.AutoIdle; }
  129. }
  130. /// <summary>
  131. /// EFEM实体
  132. /// </summary>
  133. public EfemEntity EFEM { get; }
  134. public WaferSize WaferSize { get; private set; }
  135. #endregion
  136. #region 内部变量
  137. private string _systemControlIp;
  138. private ICycle _jobCycle;
  139. /// <summary>
  140. /// 模块实体字典
  141. /// </summary>
  142. private Dictionary<string, IModuleEntity> _moduleEntitiesDic = new Dictionary<string, IModuleEntity>();
  143. /// <summary>
  144. /// 模块类型实体字典
  145. /// </summary>
  146. private Dictionary<ModuleType, List<IModuleEntity>> _modultTypeEntitiesDic = new Dictionary<ModuleType, List<IModuleEntity>>();
  147. /// <summary>
  148. /// Home stopwatch
  149. /// </summary>
  150. private Stopwatch _homeStopWatch = new Stopwatch();
  151. /// <summary>
  152. ///
  153. /// </summary>
  154. private List<ReservoirCellHomeRoutine> _reservoirCellRoutines = new List<ReservoirCellHomeRoutine>();
  155. #endregion
  156. /// <summary>
  157. /// 构造函数
  158. /// </summary>
  159. public RouteManager()
  160. {
  161. Name = "System";
  162. WaferSize = (WaferSize)SC.GetValue<int>("System.WaferSize");
  163. if (ModuleHelper.IsInstalled(ModuleName.EFEM))
  164. {
  165. EFEM = new EfemEntity();
  166. EFEM.Initialize();
  167. _moduleEntitiesDic[ModuleName.EFEM.ToString()] = EFEM;
  168. }
  169. InitialModuleList(VpwMainItemManager.Instance.InstalledModules, typeof(VpwMainEntity), ModuleType.VpwMain);
  170. InitialModuleList(VpwCellItemManager.Instance.InstalledModules, typeof(VpwCellEntity), ModuleType.VPW);
  171. InitialModuleList(SrdItemManager.Instance.InstalledModules, typeof(SRDEntity), ModuleType.SRD);
  172. InitialModuleList(ReservoirItemManager.Instance.InstalledModules, typeof(ReservoirEntity), ModuleType.Reservoir);
  173. InitialModuleList(PlatingCellItemManager.Instance.InstalledModules, typeof(PlatingCellEntity), ModuleType.PlatingCell);
  174. fsm = new StateMachine<RouteManager>(Name, (int)RtState.Init, 200);
  175. SubscribeOperation();
  176. SubscribeDataVariable();
  177. }
  178. /// <summary>
  179. /// 初始化模块集合
  180. /// </summary>
  181. /// <param name="lst"></param>
  182. /// <param name="entityType"></param>
  183. private void InitialModuleList(List<string> lst,Type entityType,ModuleType moduleType)
  184. {
  185. foreach(string item in lst)
  186. {
  187. InitialModule(item, entityType,moduleType);
  188. }
  189. }
  190. /// <summary>
  191. /// 初始化模块对象
  192. /// </summary>
  193. /// <param name="item"></param>
  194. /// <param name="moduleType"></param>
  195. private void InitialModule(string item,Type entityType,ModuleType moduleType)
  196. {
  197. ModuleName moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), item);
  198. IModuleEntity moduleEntity= (IModuleEntity)System.Activator.CreateInstance(entityType,moduleName);
  199. moduleEntity.Initialize();
  200. _moduleEntitiesDic[item] = moduleEntity;
  201. List<IModuleEntity> lst = new List<IModuleEntity>();
  202. if(_modultTypeEntitiesDic.ContainsKey(moduleType))
  203. {
  204. lst= _modultTypeEntitiesDic[moduleType];
  205. }
  206. else
  207. {
  208. _modultTypeEntitiesDic[moduleType] = lst;
  209. }
  210. lst.Add(moduleEntity);
  211. }
  212. public bool Check(int msg, out string reason, params object[] args)
  213. {
  214. if (!fsm.FindTransition(fsm.State, msg))
  215. {
  216. reason = String.Format("{0} is in {1} state,can not do {2}", Name, 0, (MSG)msg);
  217. return false;
  218. }
  219. if (msg == (int)MSG.StartCycle)
  220. {
  221. if (!IsAutoMode)
  222. {
  223. reason = String.Format("can not do {0}, isn't auto mode.", msg.ToString());
  224. return false;
  225. }
  226. }
  227. reason = "";
  228. return true;
  229. }
  230. /// <summary>
  231. /// 订阅数据
  232. /// </summary>
  233. void SubscribeDataVariable()
  234. {
  235. DATA.Subscribe("System.SystemControlIp", ()=>_systemControlIp, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  236. DATA.Subscribe("Rt.Status", () => ((RtState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  237. DATA.Subscribe("System.State", () => ((RtState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  238. DATA.Subscribe("System.IsAutoMode", () => IsAutoMode, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  239. DATA.Subscribe("System.IsAutoRunning", () => IsAutoRunning, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  240. DATA.Subscribe("System.IsIdle", () => IsIdle || IsInit, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  241. DATA.Subscribe("System.IsAlarm", () => IsAlarm, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  242. DATA.Subscribe("System.IsBusy", () => IsRunning, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  243. DATA.Subscribe("System.IsConnectedWithHost", () => CheckSecsGemOnline(Singleton<SecGemApplication>.Instance.ControlState),SubscriptionAttribute.FLAG.IgnoreSaveDB);
  244. DATA.Subscribe("System.IsDisconnectWithHost", () => CheckSecsGemOnline(Singleton<SecGemApplication>.Instance.ControlState),SubscriptionAttribute.FLAG.IgnoreSaveDB);
  245. DATA.Subscribe("System.EquipmentMode", () => IsAutoMode ? "Auto":"Manual", SubscriptionAttribute.FLAG.IgnoreSaveDB);
  246. DATA.Subscribe("EquipmentStatus", () =>
  247. {
  248. if (IsInit) return 0;
  249. if (IsIdle) return 1;
  250. if (IsAlarm) return 3;
  251. if (IsPaused) return 4;
  252. return 2;
  253. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  254. }
  255. /// <summary>
  256. /// 订阅操作
  257. /// </summary>
  258. void SubscribeOperation()
  259. {
  260. OP.Subscribe("ApplySystemControl", ApplySystemControl);
  261. OP.Subscribe("ReleaseSystemControl", ReleaseSystemControl);
  262. OP.Subscribe("CreateWafer", InvokeCreateWafer);
  263. OP.Subscribe("DeleteWafer", InvokeDeleteWafer);
  264. OP.Subscribe("System.Home", (cmd, args) => CheckToPostMessage((int)MSG.HOME, args));
  265. DATA.Subscribe("SYSTEM.FsmState", () => (((RtState)fsm.State).ToString()), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  266. OP.Subscribe("System.ReturnAllWafer", (string cmd, object[] args) =>
  267. {
  268. return CheckToPostMessage((int)MSG.ReturnAllWafer, args[0], args[1], args[2], args[3]);
  269. });
  270. OP.Subscribe("System.MoveWafer", (string cmd, object[] args) =>
  271. {
  272. if (!Enum.TryParse((string)args[0], out ModuleName source))
  273. {
  274. EV.PostWarningLog(Name, $"Parameter source {(string)args[0]} not valid");
  275. return false;
  276. }
  277. if (!Enum.TryParse((string)args[2], out ModuleName destination))
  278. {
  279. EV.PostWarningLog(Name, $"Parameter destination {(string)args[1]} not valid");
  280. return false;
  281. }
  282. return CheckToPostMessage((int)MSG.MoveWafer,
  283. source, (int)args[1],
  284. destination, (int)args[3],
  285. args[4], args[5],
  286. args[6], args[7], (string)args[8]);
  287. });
  288. OP.Subscribe("System.HomeAll", (string cmd, object[] args) =>
  289. {
  290. return CheckToPostMessage((int)MSG.HOME);
  291. });
  292. OP.Subscribe("System.Abort", (string cmd, object[] args) =>
  293. {
  294. return CheckToPostMessage((int)MSG.ABORT);
  295. });
  296. OP.Subscribe("System.Reset", (string cmd, object[] args) =>
  297. {
  298. return CheckToPostMessage((int)MSG.RESET);
  299. });
  300. OP.Subscribe("System.SetAutoMode", (string cmd, object[] args) =>
  301. {
  302. return CheckToPostMessage((int)MSG.SetAutoMode);
  303. });
  304. OP.Subscribe("System.SetManualMode", (string cmd, object[] args) =>
  305. {
  306. return CheckToPostMessage((int)MSG.SetManualMode);
  307. });
  308. OP.Subscribe("System.CreateJob", (string cmd, object[] args) =>
  309. {
  310. return CheckToPostMessage((int)MSG.CreateJob, args[0]);
  311. });
  312. OP.Subscribe("System.StartJob", (string cmd, object[] args) =>
  313. {
  314. return CheckToPostMessage((int)MSG.StartJob, args[0]);
  315. });
  316. OP.Subscribe("System.PauseJob", (string cmd, object[] args) =>
  317. {
  318. return CheckToPostMessage((int)MSG.PauseJob, args[0]);
  319. });
  320. OP.Subscribe("System.ResumeJob", (string cmd, object[] args) =>
  321. {
  322. return CheckToPostMessage((int)MSG.ResumeJob, args[0]);
  323. });
  324. OP.Subscribe("System.PauseAllJob", (cmd, args) => { return CheckToPostMessage((int)MSG.PauseAllJobs); }) ;
  325. OP.Subscribe("System.ResumeAllJob", (cmd, args) => { return CheckToPostMessage((int)MSG.ResumeAllJobs); });
  326. OP.Subscribe("System.StopJob", (string cmd, object[] args) =>
  327. {
  328. return CheckToPostMessage((int)MSG.StopJob, args[0]);
  329. });
  330. OP.Subscribe("System.AbortJob", (string cmd, object[] args) =>
  331. {
  332. return CheckToPostMessage((int)MSG.AbortJob, args[0]);
  333. });
  334. OP.Subscribe(RtOperation.SetConfig.ToString(), (name, args) =>
  335. {
  336. string sc_key = args[0] as string;
  337. if (!string.IsNullOrWhiteSpace(sc_key) && args.Length > 1)
  338. {
  339. SC.SetItemValue(sc_key, args[1]);
  340. }
  341. return true;
  342. });
  343. OP.Subscribe("System.SetWaferSize", (string cmd, object[] args) =>
  344. {
  345. string module = (string)args[0];
  346. string size = (string)args[1];
  347. switch (size)
  348. {
  349. case "3":
  350. WaferManager.Instance.UpdateWaferSize(ModuleHelper.Converter(module), 0, WaferSize.WS3);
  351. break;
  352. case "4":
  353. WaferManager.Instance.UpdateWaferSize(ModuleHelper.Converter(module), 0, WaferSize.WS4);
  354. break;
  355. case "6":
  356. WaferManager.Instance.UpdateWaferSize(ModuleHelper.Converter(module), 0, WaferSize.WS6);
  357. break;
  358. default:
  359. EV.PostWarningLog("System", $"wafer size {size} not valid");
  360. break;
  361. }
  362. return true;
  363. });
  364. //OP.Subscribe("System.CassetteLeave", (string cmd, object[] args) =>
  365. //{
  366. // return CheckToPostMessage((int)MSG.CassetteLeave);
  367. //});
  368. OP.Subscribe("System.Retry", (string cmd, object[] args) =>
  369. {
  370. return CheckToPostMessage((int)MSG.Retry, args[0]);
  371. });
  372. OP.Subscribe("System.ConfirmComplete", (string cmd, object[] args) =>
  373. {
  374. return CheckToPostMessage((int)MSG.ConfirmComplete, args[0]);
  375. });
  376. OP.Subscribe("WaferHolder.ResetLayoutWafers", (string cmd, object[] args) =>
  377. {
  378. return CheckToPostMessage((int)MSG.ResetWafers, args);
  379. });
  380. }
  381. /// <summary>
  382. /// 申请系统控制权
  383. /// </summary>
  384. /// <param name="cmd"></param>
  385. /// <param name="args"></param>
  386. /// <returns></returns>
  387. private bool ApplySystemControl(string cmd, object[] args)
  388. {
  389. if (args[0].ToString() == "")
  390. {
  391. LOG.WriteLog(eEvent.WARN_ROUTER, "System", $"Current IP does not allow the application of UI control permissions");
  392. return false;
  393. }
  394. string applyIp = args[0].ToString();
  395. if (string.IsNullOrEmpty(_systemControlIp))
  396. {
  397. _systemControlIp = applyIp;
  398. LOG.WriteLog(eEvent.EV_ROUTER, "System", $"{_systemControlIp} apply system control success");
  399. return true;
  400. }
  401. else if(_systemControlIp!=applyIp)
  402. {
  403. LOG.WriteLog(eEvent.WARN_ROUTER, "System", $"{_systemControlIp} already has system control permission");
  404. return false;
  405. }
  406. return false;
  407. }
  408. /// <summary>
  409. /// 释放系统控制权
  410. /// </summary>
  411. /// <param name="cmd"></param>
  412. /// <param name="args"></param>
  413. /// <returns></returns>
  414. private bool ReleaseSystemControl(string cmd, object[] args)
  415. {
  416. string releaseIp = args[0].ToString();
  417. if (_systemControlIp == releaseIp)
  418. {
  419. LOG.WriteLog(eEvent.EV_ROUTER, "System", $"{releaseIp} release system control success");
  420. _systemControlIp = "";
  421. }
  422. else
  423. {
  424. LOG.WriteLog(eEvent.ERR_ROUTER, "System", $"{releaseIp} is not SystemControlIp");
  425. }
  426. return false;
  427. }
  428. /// <summary>
  429. /// 检验Secsgem在线情况
  430. /// </summary>
  431. /// <param name="controlState"></param>
  432. /// <returns></returns>
  433. private bool CheckSecsGemOnline(SecsGem.Core.EnumData.ControlState controlState)
  434. {
  435. if (controlState == SecsGem.Core.EnumData.ControlState.OnlineLocal || controlState == SecsGem.Core.EnumData.ControlState.OnlineRemote)
  436. {
  437. return true;
  438. }
  439. return false;
  440. }
  441. public bool CheckToPostMessage(int msg, params object[] args)
  442. {
  443. if (!fsm.FindTransition(fsm.State, msg))
  444. {
  445. LOG.Write(eEvent.WARN_ROUTER, ModuleName.System, $"System is in {(RtState)fsm.State} state,can not do {(MSG)msg}");
  446. return false;
  447. }
  448. Running = true;
  449. fsm.PostMsg(msg, args);
  450. return true;
  451. }
  452. private bool InvokeCreateWafer(string arg1, object[] args)
  453. {
  454. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  455. int slot = (int)args[1];
  456. WaferStatus state = WaferStatus.Normal;
  457. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  458. {
  459. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  460. {
  461. LOG.Write(eEvent.EV_ROUTER, "System", string.Format("{0} slot {1} already has wafer.create wafer is not valid", chamber, slot));
  462. }
  463. else if (WaferManager.Instance.CreateWafer(chamber, slot, state) != null)
  464. {
  465. LOG.Write(eEvent.EV_WAFER_CREATE, ModuleName.System, chamber.ToString(), (slot + 1).ToString(), state.ToString());
  466. }
  467. }
  468. else
  469. {
  470. LOG.Write(eEvent.WARN_ROUTER, "System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  471. return false;
  472. }
  473. return true;
  474. }
  475. private bool InvokeDeleteWafer(string arg1, object[] args)
  476. {
  477. ModuleName chamber = ModuleHelper.Converter(args[0].ToString());
  478. int slot = (int)args[1];
  479. if (WaferManager.Instance.IsWaferSlotLocationValid(chamber, slot))
  480. {
  481. if (WaferManager.Instance.CheckHasWafer(chamber, slot))
  482. {
  483. WaferManager.Instance.DeleteWafer(chamber, slot);
  484. EV.PostMessage(ModuleName.System.ToString(), EventEnum.WaferDelete, chamber.ToString(), slot + 1);
  485. }
  486. else
  487. {
  488. LOG.Write(eEvent.EV_ROUTER, "System", string.Format("No wafer at {0} {1}, delete not valid", chamber.ToString(), slot + 1));
  489. }
  490. }
  491. else
  492. {
  493. LOG.Write(eEvent.WARN_ROUTER, "System", string.Format("Invalid position,{0},{1}", chamber.ToString(), slot.ToString()));
  494. return false;
  495. }
  496. return true;
  497. }
  498. protected override bool Init()
  499. {
  500. _jobCycle = new JobDispatcher();
  501. Singleton<FaRemoteManager>.Instance.JobCycle = _jobCycle;
  502. BuildTransitionTable();
  503. return true;
  504. }
  505. private void BuildTransitionTable()
  506. {
  507. //Init sequence
  508. Transition(RtState.Init, MSG.HOME, FsmStartHome, RtState.Initializing);
  509. Transition(RtState.Idle, MSG.HOME, FsmStartHome, RtState.Initializing);
  510. Transition(RtState.Error, MSG.HOME, FsmStartHome, RtState.Initializing);
  511. EnterExitTransition<RtState, FSM_MSG>(RtState.AutoRunning, FsmEnterAutoRunning, FSM_MSG.NONE, FsmExitAutoTransfer);
  512. EnterExitTransition<RtState, FSM_MSG>(RtState.Transfer, null, FSM_MSG.NONE, FsmExitTransfer);
  513. EnterExitTransition<RtState, FSM_MSG>(RtState.ReturnWafer, null, FSM_MSG.NONE, FsmExitReturnWafer);
  514. AnyStateTransition(MSG.ERROR, FsmError, RtState.Error);
  515. Transition(RtState.Idle, FSM_MSG.TIMER, FsmMonitor, RtState.Idle);
  516. Transition(RtState.Init, FSM_MSG.TIMER, FsmMonitor, RtState.Init);
  517. Transition(RtState.Init, MSG.ABORT, FsmAbort, RtState.Init);
  518. Transition(RtState.Error, MSG.ABORT, FsmAbort, RtState.Init);
  519. Transition(RtState.Idle, MSG.ABORT, FsmAbort, RtState.Idle);
  520. Transition(RtState.Initializing, MSG.ABORT, FsmAbort, RtState.Init);
  521. Transition(RtState.Initializing, FSM_MSG.TIMER, FsmMonitorHome, RtState.Idle);
  522. Transition(RtState.Initializing, MSG.ERROR, FsmError, RtState.Error);
  523. //Auto/manual
  524. Transition(RtState.Idle, MSG.SetAutoMode, FsmStartAutoTransfer, RtState.AutoIdle);
  525. Transition(RtState.AutoAborting, FSM_MSG.TIMER, FsmAutoTransfer, RtState.AutoIdle);
  526. Transition(RtState.AutoRunning, FSM_MSG.TIMER, FsmAutoTransfer, RtState.AutoIdle);
  527. Transition(RtState.AutoRunning, MSG.ABORT, FsmAbort, RtState.AutoAborting);
  528. Transition(RtState.AutoAborting, MSG.SetManualMode, FsmStartAbortingSetManualMode, RtState.Idle);
  529. //Transition(RtState.AutoRunning, MSG.SetManualMode, FsmStartSetManualMode, RtState.Idle);
  530. Transition(RtState.AutoRunning, MSG.JobDone, FsmJobDone, RtState.AutoIdle);
  531. //Transition(RtState.AutoRunning, MSG.CassetteLeave, fCassetteLeave, RtState.AutoRunning); //For unload light control off afer job done
  532. Transition(RtState.AutoRunning, MSG.CreateJob, FsmCreateJob, RtState.AutoRunning);
  533. Transition(RtState.AutoRunning, MSG.StartJob, FsmStartJob, RtState.AutoRunning);
  534. Transition(RtState.AutoRunning, MSG.PauseJob, FsmPauseJob, RtState.AutoRunning);
  535. Transition(RtState.AutoRunning, MSG.PauseAllJobs, FsmPauseAllJobs, RtState.AutoRunning);
  536. Transition(RtState.AutoRunning, MSG.ResumeJob, FsmResumeJob, RtState.AutoRunning);
  537. Transition(RtState.AutoRunning, MSG.ResumeAllJobs, FsmResumeAllJobs, RtState.AutoRunning);
  538. Transition(RtState.AutoRunning, MSG.StopJob, FsmStopJob, RtState.AutoRunning);
  539. Transition(RtState.AutoRunning, MSG.Retry, FsmRetry, RtState.AutoRunning);
  540. Transition(RtState.AutoRunning, MSG.ConfirmComplete, FsmConfirmComplete, RtState.AutoRunning);
  541. Transition(RtState.AutoIdle, FSM_MSG.TIMER, FsmMonitorAutoIdle, RtState.AutoIdle);
  542. Transition(RtState.AutoIdle, MSG.SetManualMode, FsmStartSetManualMode, RtState.Idle);
  543. Transition(RtState.AutoIdle, MSG.CreateJob, FsmCreateJob, RtState.AutoIdle);
  544. Transition(RtState.AutoIdle, MSG.StartJob, FsmStartJob, RtState.AutoRunning);
  545. Transition(RtState.AutoIdle, MSG.PauseJob, FsmPauseJob, RtState.AutoIdle);
  546. Transition(RtState.AutoIdle, MSG.PauseAllJobs, FsmPauseAllJobs, RtState.AutoIdle);
  547. Transition(RtState.AutoIdle, MSG.ResumeJob, FsmResumeJob, RtState.AutoIdle);
  548. Transition(RtState.AutoIdle, MSG.ResumeAllJobs, FsmResumeAllJobs, RtState.AutoIdle);
  549. Transition(RtState.AutoIdle, MSG.StopJob, FsmStopJob, RtState.AutoIdle);
  550. Transition(RtState.AutoIdle, MSG.FaStartJob, FsmEnterAutoRunning, RtState.AutoRunning);
  551. Transition(RtState.AutoRunning, MSG.FaStartJob, FsmEnterAutoRunning, RtState.AutoRunning);
  552. }
  553. private bool FsmMonitor(object[] objs)
  554. {
  555. return true;
  556. }
  557. /// <summary>
  558. /// 检验前置条件
  559. /// </summary>
  560. /// <returns></returns>
  561. private bool CheckPreCondition()
  562. {
  563. if (ModuleHelper.IsInstalled(ModuleName.SRD1))
  564. {
  565. SrdCommonDevice srdCommonDevice = DEVICE.GetDevice<SrdCommonDevice>($"{ModuleName.SRD1}.Common");
  566. if (!srdCommonDevice.CommonData.ChuckVacuum)
  567. {
  568. LOG.WriteLog(eEvent.ERR_AXIS, ModuleName.SRD1.ToString(), "Vacuum is on");
  569. return false;
  570. }
  571. }
  572. if (ModuleHelper.IsInstalled(ModuleName.SRD2))
  573. {
  574. SrdCommonDevice srdCommonDevice = DEVICE.GetDevice<SrdCommonDevice>($"{ModuleName.SRD2}.Common");
  575. if (!srdCommonDevice.CommonData.ChuckVacuum)
  576. {
  577. LOG.WriteLog(eEvent.ERR_AXIS, ModuleName.SRD2.ToString(), "Vacuum is on");
  578. return false;
  579. }
  580. }
  581. return true;
  582. }
  583. private bool FsmStartHome(object[] objs)
  584. {
  585. if (!CheckPreCondition())
  586. {
  587. return false;
  588. }
  589. List<string> keys = _moduleEntitiesDic.Keys.ToList();
  590. foreach (string item in keys)
  591. {
  592. if(!Enum.TryParse(item, out ModuleName moduleName))
  593. {
  594. continue;
  595. }
  596. if (!NeedSequenceHome(item))
  597. {
  598. IModuleEntity entity = _moduleEntitiesDic[item];
  599. if (!entity.IsDisable&&!ModuleHelper.IsVPWCell(moduleName))
  600. {
  601. entity.Invoke("HomeAll");
  602. }
  603. }
  604. }
  605. _reservoirCellRoutines.Clear();
  606. foreach (string item in keys)
  607. {
  608. if (Enum.TryParse(item, out ModuleName moduleName))
  609. {
  610. if (ModuleHelper.IsReservoir(moduleName))
  611. {
  612. IModuleEntity moduleEntity = _moduleEntitiesDic[item];
  613. if (!moduleEntity.IsDisable)
  614. {
  615. ReservoirCellHomeRoutine reservoirCellHomeRoutine = new ReservoirCellHomeRoutine(item);
  616. _reservoirCellRoutines.Add(reservoirCellHomeRoutine);
  617. }
  618. }
  619. }
  620. }
  621. foreach (ReservoirCellHomeRoutine item in _reservoirCellRoutines)
  622. {
  623. item.Start();
  624. }
  625. foreach (string item in keys)
  626. {
  627. if (Enum.TryParse(item, out ModuleName moduleName))
  628. {
  629. if (ModuleHelper.IsReservoir(moduleName))
  630. {
  631. IModuleEntity moduleEntity = _moduleEntitiesDic[item];
  632. }
  633. }
  634. }
  635. _homeStopWatch.Restart();
  636. _jobCycle.RemoveAllJob();
  637. return true;
  638. }
  639. /// <summary>
  640. /// 需要按照顺序Home
  641. /// </summary>
  642. /// <returns></returns>
  643. private bool NeedSequenceHome(string item)
  644. {
  645. if(Enum.TryParse(item,out ModuleName moduleName))
  646. {
  647. if (!ModuleHelper.IsPlatingCell(moduleName)&&!ModuleHelper.IsReservoir(moduleName))
  648. {
  649. return false;
  650. }
  651. }
  652. return true;
  653. }
  654. private bool FsmMonitorHome(object[] objs)
  655. {
  656. if (_homeStopWatch.ElapsedMilliseconds <= 500)
  657. {
  658. return false;
  659. }
  660. List<string> keys = LoadInitializeCheckModuleList();
  661. int idleCount = 0;
  662. foreach (string item in keys)
  663. {
  664. IModuleEntity ModuleEntity = _moduleEntitiesDic[item];
  665. if (ModuleEntity.IsError)
  666. {
  667. LOG.Write(eEvent.ERR_ROUTER, ModuleName.System, $"{ModuleEntity.Module} home error");
  668. PostMsg(MSG.ERROR);
  669. return true;
  670. }
  671. else if (ModuleEntity.IsIdle)
  672. {
  673. idleCount++;
  674. }
  675. }
  676. int homeCount = 0;
  677. foreach (ReservoirCellHomeRoutine item in _reservoirCellRoutines)
  678. {
  679. RState ret = item.Monitor();
  680. if (ret != RState.Running)
  681. {
  682. homeCount++;
  683. }
  684. }
  685. if (idleCount == keys.Count)
  686. {
  687. return true;
  688. }
  689. else
  690. {
  691. return false;
  692. }
  693. }
  694. /// <summary>
  695. /// 加载初始化检验模块集合
  696. /// </summary>
  697. /// <returns></returns>
  698. private List<string> LoadInitializeCheckModuleList()
  699. {
  700. string checkModuleList = SC.GetStringValue("System.InitializeCheckModuleList");
  701. List<string> moduleList= checkModuleList.Split(',').ToList();
  702. List<string> modules = new List<string>();
  703. foreach (var item in moduleList)
  704. {
  705. if(Enum.TryParse(item, out ModuleName moduleName))
  706. {
  707. if (ModuleHelper.IsInstalled(moduleName))
  708. {
  709. modules.Add(item);
  710. }
  711. }
  712. }
  713. return modules;
  714. }
  715. private bool FsmEnterAutoRunning(object[] objs)
  716. {
  717. return true;
  718. }
  719. private bool FsmExitAutoTransfer(object[] objs)
  720. {
  721. _jobCycle.Clear();
  722. return true;
  723. }
  724. private bool FsmExitTransfer(object[] objs)
  725. {
  726. return true;
  727. }
  728. private bool FsmExitReturnWafer(object[] objs)
  729. {
  730. return true;
  731. }
  732. private bool FsmError(object[] objs)
  733. {
  734. return true;
  735. }
  736. private bool FsmAbort(object[] objs)
  737. {
  738. //_manualTransfer.Clear();
  739. //_returnWafer.Clear();
  740. //_jobCycle.Clear();
  741. AlarmListManager.Instance.ClearAllAlarm();
  742. return _jobCycle.SystemAbort();
  743. }
  744. private bool FsmStartAutoTransfer(object[] objs)
  745. {
  746. return _jobCycle.Start(objs) == RState.Running;
  747. }
  748. private bool FsmAutoTransfer(object[] objs)
  749. {
  750. if (_jobCycle.CheckJobJustDone(out string jobInfo))
  751. {
  752. //EV.PostPopDialogMessage(EventLevel.InformationNoDelay, "Job complete", jobInfo);
  753. LOG.Write(eEvent.EV_ROUTER, ModuleName.System, $"Job complete,{jobInfo}");
  754. }
  755. if (_jobCycle.CheckAllJobDone())
  756. {
  757. if (!CheckToPostMessage((int)MSG.JobDone))
  758. return false;
  759. }
  760. RState ret = _jobCycle.Monitor();
  761. if (ret == RState.Failed)
  762. {
  763. if (!CheckToPostMessage((int)MSG.ERROR))
  764. return false;
  765. }
  766. return ret == RState.End;
  767. }
  768. private bool FsmAbortAutoTransfer(object[] objs)
  769. {
  770. _jobCycle.Clear();
  771. return true;
  772. }
  773. private bool FsmJobDone(object[] objs)
  774. {
  775. return true;
  776. }
  777. private bool FsmCreateJob(object[] objs)
  778. {
  779. return _jobCycle.CreateJob((Dictionary<string, object>)objs[0],out string reason);
  780. }
  781. private bool FsmStartJob(object[] objs)
  782. {
  783. return _jobCycle.StartJob((string)objs[0],out string reason);
  784. }
  785. private bool FsmPauseJob(object[] objs)
  786. {
  787. return _jobCycle.PauseJob((string)objs[0],out string reason);
  788. }
  789. private bool FsmResumeJob(object[] objs)
  790. {
  791. return _jobCycle.ResumeJob((string)objs[0],out string reason);
  792. }
  793. private bool FsmPauseAllJobs(object[] objs)
  794. {
  795. return _jobCycle.PauseAllJobs();
  796. }
  797. private bool FsmResumeAllJobs(object[] objs)
  798. {
  799. return _jobCycle.ResumeAllJobs();
  800. }
  801. private bool FsmStopJob(object[] objs)
  802. {
  803. return _jobCycle.StopJob((string)objs[0], out string reason);
  804. }
  805. /// <summary>
  806. /// 重试
  807. /// </summary>
  808. /// <param name="objs"></param>
  809. /// <returns></returns>
  810. private bool FsmRetry(object[] objs)
  811. {
  812. string moduleName = objs[0].ToString();
  813. IModuleEntity moduleEntity = GetModule<IModuleEntity>(moduleName);
  814. moduleEntity.Invoke("Retry");
  815. return true;
  816. }
  817. /// <summary>
  818. /// 重试
  819. /// </summary>
  820. /// <param name="objs"></param>
  821. /// <returns></returns>
  822. private bool FsmConfirmComplete(object[] objs)
  823. {
  824. string moduleName = objs[0].ToString();
  825. IModuleEntity moduleEntity = GetModule<IModuleEntity>(moduleName);
  826. moduleEntity.Invoke("ConfirmComplete");
  827. return true;
  828. }
  829. private bool FsmMonitorAutoIdle(object[] objs)
  830. {
  831. RState ret = _jobCycle.Monitor();
  832. return ret == RState.End;
  833. }
  834. private bool FsmStartAbortingSetManualMode(object[] objs)
  835. {
  836. return _jobCycle.RemoveAllJob();
  837. }
  838. private bool FsmStartSetManualMode(object[] objs)
  839. {
  840. if (_jobCycle.HasJobRunning)
  841. {
  842. LOG.Write(eEvent.WARN_ROUTER, "System", "Can not change to manual mode, abort running job first");
  843. return false;
  844. }
  845. return true;
  846. }
  847. /// <summary>
  848. /// 获取模块对象
  849. /// </summary>
  850. /// <typeparam name="T"></typeparam>
  851. /// <param name="name"></param>
  852. /// <returns></returns>
  853. public T GetModule<T>(string name) where T : class, IModuleEntity
  854. {
  855. return _moduleEntitiesDic.ContainsKey(name)?_moduleEntitiesDic[name] as T:default(T);
  856. }
  857. /// <summary>
  858. /// 根据模块类型获取模块集合
  859. /// </summary>
  860. /// <param name="type"></param>
  861. /// <returns></returns>
  862. public List<IModuleEntity> GetModulesByModuleType(ModuleType type)
  863. {
  864. return _modultTypeEntitiesDic.ContainsKey(type) ? _modultTypeEntitiesDic[type] : new List<IModuleEntity>();
  865. }
  866. }
  867. }