WaferHolderTaskManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Equipment;
  5. using MECF.Framework.Common.WaferHolder;
  6. using CyberX8_RT.Modules.Loader;
  7. using CyberX8_RT.Modules;
  8. using CyberX8_RT.Schedulers;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using MECF.Framework.Common.Jobs;
  16. using CyberX8_RT.Modules.PUF;
  17. using CyberX8_Core;
  18. using CyberX8_RT.Modules.Transporter;
  19. using MECF.Framework.Common.SubstrateTrackings;
  20. using MECF.Framework.Common.RecipeCenter;
  21. namespace CyberX8_RT.Dispatch
  22. {
  23. public class WaferHolderTaskManager : Singleton<WaferHolderTaskManager>
  24. {
  25. #region 内部变量
  26. /// <summary>
  27. /// wafer Holder task字典(key-id,value-waferHolderTask实例)
  28. /// </summary>
  29. private ConcurrentDictionary<string, WaferHolderTask> _waferHolderTaskDic = new ConcurrentDictionary<string, WaferHolderTask>();
  30. /// <summary>
  31. /// WaferTask集合
  32. /// </summary>
  33. private List<WaferHolderTask> _waferTaskList = new List<WaferHolderTask>();
  34. /// <summary>
  35. /// WaferTask锁
  36. /// </summary>
  37. private object _waferHolderTaskObject = new object();
  38. #endregion
  39. /// <summary>
  40. /// 创建任务
  41. /// </summary>
  42. public bool CreateWaferHolderTask(List<ProcessJobInfo> jobs)
  43. {
  44. SequenceRecipe sequenceRecipe = jobs[0].SequenceRecipe;
  45. List<string> chemistries = SchedulerSequenceRecipeManager.Instance.GetSequenceChemistry(sequenceRecipe);
  46. WaferSize waferSize = (WaferSize)sequenceRecipe.SubstrateSize;
  47. WaferHolderInfo waferHolderInfo = SchedulerManager.Instance.GetAvaibleBufferToLoaderWaferHolder(waferSize,sequenceRecipe.CrsType,chemistries);
  48. if (waferHolderInfo != null && CheckWaferHolderNotExist(waferHolderInfo))
  49. {
  50. if (CheckEnableCreateWaferHolderTask())
  51. {
  52. if(!Enum.TryParse(waferHolderInfo.CurrentLocation,out ModuleName locationModuleName))
  53. {
  54. return false;
  55. }
  56. if (ModuleHelper.IsBuffer(locationModuleName)&&string.IsNullOrEmpty(waferHolderInfo.OriginalBuffer))
  57. {
  58. waferHolderInfo.OriginalBuffer = waferHolderInfo.CurrentLocation;
  59. }
  60. WaferHolderTask waferHolderTask = new WaferHolderTask(waferHolderInfo, jobs[0]);
  61. waferHolderTask.OnTaskComplete += WaferHolderTask_OnTaskComplete;
  62. WaferInfo waferInfo = GetPreLoaderHasWafer();
  63. int productWaferCount = 0;
  64. int dummyWaferCount = 0;
  65. if (waferInfo==null)
  66. {
  67. var result= WaferTaskManager.Instance.CreateWaferTask(jobs, waferHolderInfo, waferHolderTask);
  68. productWaferCount = result.productWaferCount;
  69. dummyWaferCount= result.dummyWaferCount;
  70. }
  71. else
  72. {
  73. if(!CheckWaferMatchedWaferHolderTaskNotCreated(waferInfo))
  74. {
  75. return false;
  76. }
  77. string waferHasMatch = WaferTaskManager.Instance.CheckWaferHasMatch(waferInfo.WaferID);
  78. if(string.IsNullOrEmpty(waferHasMatch))
  79. {
  80. if (waferInfo.WaferType == WaferType.Production)
  81. {
  82. productWaferCount = 1;
  83. dummyWaferCount = 0;
  84. }
  85. else
  86. {
  87. productWaferCount = 0;
  88. dummyWaferCount = 1;
  89. }
  90. }
  91. else
  92. {
  93. WaferInfo matchWafer = WaferManager.Instance.GetWaferByWaferId(waferHasMatch);
  94. if (matchWafer == null || matchWafer.IsEmpty)
  95. {
  96. if (waferInfo.WaferType == WaferType.Production)
  97. {
  98. productWaferCount = 1;
  99. }
  100. else
  101. {
  102. dummyWaferCount = 1;
  103. }
  104. }
  105. else
  106. {
  107. if (waferInfo.WaferType == WaferType.Production)
  108. {
  109. productWaferCount = 1;
  110. }
  111. else
  112. {
  113. dummyWaferCount = 1;
  114. }
  115. if (matchWafer.WaferType == WaferType.Production)
  116. {
  117. productWaferCount += 1;
  118. }
  119. else
  120. {
  121. dummyWaferCount += 1;
  122. }
  123. }
  124. }
  125. }
  126. if(productWaferCount+dummyWaferCount == 0)
  127. {
  128. return false;
  129. }
  130. _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask);
  131. lock (_waferHolderTaskObject)
  132. {
  133. _waferTaskList.Add(waferHolderTask);
  134. }
  135. LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create wafer Shuttle {waferHolderInfo.Id} task");
  136. List<SchedulerSequence> sequences = SchedulerSequenceManager.Instance.AnalyWaferHolderAllSchedulerSequence(waferHolderInfo, jobs[0].SequenceRecipe,productWaferCount,dummyWaferCount);
  137. waferHolderTask.InitialSchedulers(sequences);
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// 查看Wafer对应的WaferHolderTask是否已经创建
  145. /// </summary>
  146. /// <param name="waferInfo"></param>
  147. /// <returns></returns>
  148. private bool CheckWaferMatchedWaferHolderTaskNotCreated(WaferInfo waferInfo)
  149. {
  150. WaferHolderTask waferHolderTask = WaferTaskManager.Instance.GetWaferHolderTaskByWaferId(waferInfo.WaferID);
  151. return waferHolderTask == null || CheckWaferHolderNotExist(waferHolderTask.WaferHolderInfo);
  152. }
  153. /// <summary>
  154. /// 检验是否可以创建WaferHolderTask
  155. /// </summary>
  156. /// <returns></returns>
  157. private bool CheckEnableCreateWaferHolderTask()
  158. {
  159. TransporterEntity loaderTransporterEntity = Singleton<RouteManager>.Instance.GetModule<TransporterEntity>(ModuleName.Transporter2.ToString());
  160. LoaderEntity loaderEntity = Singleton<RouteManager>.Instance.GetModule<LoaderEntity>(ModuleName.Loader1.ToString());
  161. if (loaderEntity == null)
  162. {
  163. return false;
  164. }
  165. if (loaderEntity.IsError)
  166. {
  167. return false;
  168. }
  169. if (loaderTransporterEntity == null)
  170. {
  171. return false;
  172. }
  173. if (loaderEntity.WaferHolderInfo != null)
  174. {
  175. return false;
  176. }
  177. if (loaderTransporterEntity.IsError)
  178. {
  179. return false;
  180. }
  181. //if (loaderTransporterEntity.WaferHolderInfo != null)
  182. //{
  183. // return false;
  184. //}
  185. //if (loaderTransporterEntity.IsBusy)
  186. //{
  187. // return false;
  188. //}
  189. //存在其他任务没有完成Loader调度
  190. if (CheckExistWaferHolderTaskNotOverLoader())
  191. {
  192. return false;
  193. }
  194. return true;
  195. }
  196. /// <summary>
  197. /// 检验Loader前面是否存在Wafer
  198. /// </summary>
  199. /// <returns></returns>
  200. public WaferInfo GetPreLoaderHasWafer()
  201. {
  202. WaferInfo waferInfo = null;
  203. if (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0))
  204. {
  205. waferInfo = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, 0);
  206. return CheckWaferTaskForward(waferInfo);
  207. }
  208. if (WaferManager.Instance.CheckHasWafer(ModuleName.Aligner1, 0))
  209. {
  210. waferInfo = WaferManager.Instance.GetWafer(ModuleName.Aligner1, 0);
  211. return CheckWaferTaskForward(waferInfo);
  212. }
  213. //PUF1 B面
  214. if(WaferManager.Instance.CheckHasWafer(ModuleName.PUF1,1))
  215. {
  216. waferInfo = WaferManager.Instance.GetWafer(ModuleName.PUF1, 1);
  217. return CheckWaferTaskForward(waferInfo);
  218. }
  219. return waferInfo;
  220. }
  221. /// <summary>
  222. /// 检验Wafer是否在于Loader之前
  223. /// </summary>
  224. /// <param name="waferInfo"></param>
  225. /// <returns></returns>
  226. private WaferInfo CheckWaferTaskForward(WaferInfo waferInfo)
  227. {
  228. if (!WaferTaskManager.Instance.Contains(waferInfo.WaferID))
  229. {
  230. return null;
  231. }
  232. WaferTask waferTask = WaferTaskManager.Instance.GetWaferTask(waferInfo.WaferID);
  233. if (waferTask == null)
  234. {
  235. return null;
  236. }
  237. if (waferTask.IsSchedulerForward())
  238. {
  239. return waferInfo;
  240. }
  241. else
  242. {
  243. return null;
  244. }
  245. }
  246. /// <summary>
  247. /// 创建Dummy WaferHolder Task
  248. /// </summary>
  249. public void CreateDummyWaferHolderTask()
  250. {
  251. List<WaferHolderInfo> waferHolderInfos = DummyWaferManager.Instance.LoadCompletedWaferHolderInfo();
  252. foreach (WaferHolderInfo waferHolderInfo in waferHolderInfos)
  253. {
  254. if (CheckWaferHolderNotExist(waferHolderInfo))
  255. {
  256. if (CheckEnableCreateWaferHolderTask())
  257. {
  258. int waferTaskCount = 0;
  259. WaferInfo waferInfo = GetPreLoaderHasWafer();
  260. if (waferInfo == null)
  261. {
  262. List<WaferInfo> waferInfos = DummyWaferManager.Instance.LoadDummyWafersByWaferHolder(waferHolderInfo);
  263. if (waferInfos.Count != 0)
  264. {
  265. WaferHolderTask waferHolderTask = new WaferHolderTask(waferHolderInfo, null);
  266. waferHolderTask.OnTaskComplete += WaferHolderTask_OnTaskComplete;
  267. waferTaskCount = WaferTaskManager.Instance.CreateDummyWaferTask(waferInfos, waferHolderInfo.SequenceRecipe, waferHolderTask);
  268. if (waferTaskCount == 0)
  269. {
  270. break;
  271. }
  272. _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask);
  273. lock (_waferHolderTaskObject)
  274. {
  275. _waferTaskList.Add(waferHolderTask);
  276. }
  277. LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create dummy wafer Shuttle {waferHolderInfo.Id} task");
  278. List<SchedulerSequence> sequences = SchedulerSequenceManager.Instance.AnalyseDummyWaferHolderAllSchedulerSequence(waferHolderInfo,0, waferTaskCount);
  279. waferHolderTask.InitialSchedulers(sequences);
  280. break;
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// wafer Holder Task完成事件
  289. /// </summary>
  290. /// <param name="id"></param>
  291. private void WaferHolderTask_OnTaskComplete(string id)
  292. {
  293. RemoveById(id);
  294. }
  295. /// <summary>
  296. /// 检验WaferHolderTask是否完成Loader调度
  297. /// </summary>
  298. /// <returns></returns>
  299. private bool CheckExistWaferHolderTaskNotOverLoader()
  300. {
  301. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  302. foreach(var task in waferHolderTasks)
  303. {
  304. if (task.IsNotPassLoader())
  305. {
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. /// <summary>
  312. /// 是否包含该任务
  313. /// </summary>
  314. /// <param name="id"></param>
  315. /// <returns></returns>
  316. public bool CheckWaferHolderNotExist(WaferHolderInfo waferHolderInfo)
  317. {
  318. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  319. return waferHolderTasks.Find(O => O.WaferHolderInfo.Id == waferHolderInfo.Id) == null;
  320. }
  321. /// <summary>
  322. /// 移除
  323. /// </summary>
  324. /// <param name="id"></param>
  325. public void RemoveById(string id)
  326. {
  327. if (_waferHolderTaskDic.ContainsKey(id))
  328. {
  329. _waferHolderTaskDic.TryRemove(id, out var waferHolderTask);
  330. waferHolderTask.Dispose();
  331. waferHolderTask.OnTaskComplete -= WaferHolderTask_OnTaskComplete;
  332. LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task");
  333. }
  334. lock (_waferHolderTaskObject)
  335. {
  336. int index = _waferTaskList.FindIndex(O => O.ID == id);
  337. if (index != -1)
  338. {
  339. _waferTaskList.RemoveAt(index);
  340. }
  341. }
  342. }
  343. /// <summary>
  344. /// 加载WaferHolder任务集合
  345. /// </summary>
  346. /// <returns></returns>
  347. public List<WaferHolderTask> LoaderWaferHolderTaskList()
  348. {
  349. lock (_waferHolderTaskObject)
  350. {
  351. return _waferTaskList;
  352. }
  353. }
  354. /// <summary>
  355. /// 检验所有task是否完成
  356. /// </summary>
  357. /// <returns></returns>
  358. public bool CheckAllTaskComplete()
  359. {
  360. return _waferTaskList.Count == 0;
  361. }
  362. /// <summary>
  363. /// 暂停
  364. /// </summary>
  365. public void PauseAllTask()
  366. {
  367. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  368. foreach (WaferHolderTask item in waferHolderTasks)
  369. {
  370. item.Pause();
  371. }
  372. }
  373. /// <summary>
  374. /// 恢复所有任务
  375. /// </summary>
  376. public void ResumeAllTask()
  377. {
  378. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  379. foreach (WaferHolderTask item in waferHolderTasks)
  380. {
  381. item.Resume();
  382. }
  383. }
  384. /// <summary>
  385. /// 移除所有任务
  386. /// </summary>
  387. public void RemoveAllTask()
  388. {
  389. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  390. foreach (WaferHolderTask item in waferHolderTasks)
  391. {
  392. RemoveById(item.ID);
  393. }
  394. }
  395. /// <summary>
  396. /// 中止WaferHolderTask
  397. /// </summary>
  398. /// <param name="processJobInfo"></param>
  399. public void AbortWaferHolderTaskByProcessJob(ProcessJobInfo processJobInfo)
  400. {
  401. List<string> keys = _waferHolderTaskDic.Keys.ToList();
  402. foreach (string key in keys)
  403. {
  404. WaferHolderTask waferHolderTask = _waferHolderTaskDic[key];
  405. if(waferHolderTask!=null&&waferHolderTask.ProcessJobInfo!=null&&waferHolderTask.ProcessJobInfo.InnerId == processJobInfo.InnerId)
  406. {
  407. _waferHolderTaskDic.TryRemove(key, out waferHolderTask);
  408. RemoveById(waferHolderTask.ID);
  409. LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task");
  410. }
  411. }
  412. }
  413. /// <summary>
  414. /// 是否存在其他WaferTask没有
  415. /// </summary>
  416. /// <returns></returns>
  417. private bool CheckExistWaferHolderTaskNotStart()
  418. {
  419. List<WaferHolderTask> tmp = _waferHolderTaskDic.Values.ToList();
  420. foreach (WaferHolderTask item in tmp)
  421. {
  422. if (item.State == WaferHolderTaskState.Created)
  423. {
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. }
  430. }