RouteManager.cs 38 KB

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