WaferHolderTaskManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 waferTaskCount = 0;
  64. if (waferInfo==null)
  65. {
  66. waferTaskCount= WaferTaskManager.Instance.CreateWaferTask(jobs, waferHolderInfo, waferHolderTask);
  67. }
  68. else
  69. {
  70. if(!CheckWaferMatchedWaferHolderTaskNotCreated(waferInfo))
  71. {
  72. return false;
  73. }
  74. string waferHasMatch = WaferTaskManager.Instance.CheckWaferHasMatch(waferInfo.WaferID);
  75. if(string.IsNullOrEmpty(waferHasMatch))
  76. {
  77. waferTaskCount = 1;
  78. }
  79. else
  80. {
  81. waferTaskCount = 2;
  82. }
  83. }
  84. if(waferTaskCount == 0)
  85. {
  86. return false;
  87. }
  88. _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask);
  89. lock (_waferHolderTaskObject)
  90. {
  91. _waferTaskList.Add(waferHolderTask);
  92. }
  93. LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create wafer Shuttle {waferHolderInfo.Id} task");
  94. List<SchedulerSequence> sequences = SchedulerSequenceManager.Instance.AnalyWaferHolderAllSchedulerSequence(waferHolderInfo, jobs[0].SequenceRecipe,waferTaskCount);
  95. waferHolderTask.InitialSchedulers(sequences);
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// 查看Wafer对应的WaferHolderTask是否已经创建
  103. /// </summary>
  104. /// <param name="waferInfo"></param>
  105. /// <returns></returns>
  106. private bool CheckWaferMatchedWaferHolderTaskNotCreated(WaferInfo waferInfo)
  107. {
  108. WaferHolderTask waferHolderTask = WaferTaskManager.Instance.GetWaferHolderTaskByWaferId(waferInfo.WaferID);
  109. return waferHolderTask == null || CheckWaferHolderNotExist(waferHolderTask.WaferHolderInfo);
  110. }
  111. /// <summary>
  112. /// 检验是否可以创建WaferHolderTask
  113. /// </summary>
  114. /// <returns></returns>
  115. private bool CheckEnableCreateWaferHolderTask()
  116. {
  117. TransporterEntity loaderTransporterEntity = Singleton<RouteManager>.Instance.GetModule<TransporterEntity>(ModuleName.Transporter2.ToString());
  118. LoaderEntity loaderEntity = Singleton<RouteManager>.Instance.GetModule<LoaderEntity>(ModuleName.Loader1.ToString());
  119. if (loaderEntity == null)
  120. {
  121. return false;
  122. }
  123. if (loaderEntity.IsError)
  124. {
  125. return false;
  126. }
  127. if (loaderTransporterEntity == null)
  128. {
  129. return false;
  130. }
  131. if (loaderEntity.WaferHolderInfo != null)
  132. {
  133. return false;
  134. }
  135. if (loaderTransporterEntity.IsError)
  136. {
  137. return false;
  138. }
  139. //if (loaderTransporterEntity.WaferHolderInfo != null)
  140. //{
  141. // return false;
  142. //}
  143. //if (loaderTransporterEntity.IsBusy)
  144. //{
  145. // return false;
  146. //}
  147. //存在其他任务没有完成Loader调度
  148. if (CheckExistWaferHolderTaskNotOverLoader())
  149. {
  150. return false;
  151. }
  152. return true;
  153. }
  154. /// <summary>
  155. /// 检验Loader前面是否存在Wafer
  156. /// </summary>
  157. /// <returns></returns>
  158. public WaferInfo GetPreLoaderHasWafer()
  159. {
  160. WaferInfo waferInfo = null;
  161. if (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0))
  162. {
  163. waferInfo = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, 0);
  164. return CheckWaferTaskForward(waferInfo);
  165. }
  166. if (WaferManager.Instance.CheckHasWafer(ModuleName.Aligner1, 0))
  167. {
  168. waferInfo = WaferManager.Instance.GetWafer(ModuleName.Aligner1, 0);
  169. return CheckWaferTaskForward(waferInfo);
  170. }
  171. //PUF1 B面
  172. if(WaferManager.Instance.CheckHasWafer(ModuleName.PUF1,1))
  173. {
  174. waferInfo = WaferManager.Instance.GetWafer(ModuleName.PUF1, 1);
  175. return CheckWaferTaskForward(waferInfo);
  176. }
  177. return waferInfo;
  178. }
  179. /// <summary>
  180. /// 检验Wafer是否在于Loader之前
  181. /// </summary>
  182. /// <param name="waferInfo"></param>
  183. /// <returns></returns>
  184. private WaferInfo CheckWaferTaskForward(WaferInfo waferInfo)
  185. {
  186. if (!WaferTaskManager.Instance.Contains(waferInfo.WaferID))
  187. {
  188. return null;
  189. }
  190. WaferTask waferTask = WaferTaskManager.Instance.GetWaferTask(waferInfo.WaferID);
  191. if (waferTask == null)
  192. {
  193. return null;
  194. }
  195. if (waferTask.IsSchedulerForward())
  196. {
  197. return waferInfo;
  198. }
  199. else
  200. {
  201. return null;
  202. }
  203. }
  204. /// <summary>
  205. /// 创建Dummy WaferHolder Task
  206. /// </summary>
  207. public void CreateDummyWaferHolderTask()
  208. {
  209. List<WaferHolderInfo> waferHolderInfos = DummyWaferManager.Instance.LoadCompletedWaferHolderInfo();
  210. foreach (WaferHolderInfo waferHolderInfo in waferHolderInfos)
  211. {
  212. if (CheckWaferHolderNotExist(waferHolderInfo))
  213. {
  214. if (CheckEnableCreateWaferHolderTask())
  215. {
  216. int waferTaskCount = 0;
  217. WaferInfo waferInfo = GetPreLoaderHasWafer();
  218. if (waferInfo == null)
  219. {
  220. List<WaferInfo> waferInfos = DummyWaferManager.Instance.LoadDummyWafersByWaferHolder(waferHolderInfo);
  221. if (waferInfos.Count != 0)
  222. {
  223. WaferHolderTask waferHolderTask = new WaferHolderTask(waferHolderInfo, null);
  224. waferHolderTask.OnTaskComplete += WaferHolderTask_OnTaskComplete;
  225. waferTaskCount = WaferTaskManager.Instance.CreateDummyWaferTask(waferInfos, waferHolderInfo.SequenceRecipe, waferHolderTask);
  226. if (waferTaskCount == 0)
  227. {
  228. break;
  229. }
  230. _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask);
  231. lock (_waferHolderTaskObject)
  232. {
  233. _waferTaskList.Add(waferHolderTask);
  234. }
  235. LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create dummy wafer Shuttle {waferHolderInfo.Id} task");
  236. List<SchedulerSequence> sequences = SchedulerSequenceManager.Instance.AnalyseDummyWaferHolderAllSchedulerSequence(waferHolderInfo, waferTaskCount);
  237. waferHolderTask.InitialSchedulers(sequences);
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// wafer Holder Task完成事件
  247. /// </summary>
  248. /// <param name="id"></param>
  249. private void WaferHolderTask_OnTaskComplete(string id)
  250. {
  251. RemoveById(id);
  252. }
  253. /// <summary>
  254. /// 检验WaferHolderTask是否完成Loader调度
  255. /// </summary>
  256. /// <returns></returns>
  257. private bool CheckExistWaferHolderTaskNotOverLoader()
  258. {
  259. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  260. foreach(var task in waferHolderTasks)
  261. {
  262. if (task.IsNotPassLoader())
  263. {
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /// <summary>
  270. /// 是否包含该任务
  271. /// </summary>
  272. /// <param name="id"></param>
  273. /// <returns></returns>
  274. public bool CheckWaferHolderNotExist(WaferHolderInfo waferHolderInfo)
  275. {
  276. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  277. return waferHolderTasks.Find(O => O.WaferHolderInfo.Id == waferHolderInfo.Id) == null;
  278. }
  279. /// <summary>
  280. /// 移除
  281. /// </summary>
  282. /// <param name="id"></param>
  283. public void RemoveById(string id)
  284. {
  285. if (_waferHolderTaskDic.ContainsKey(id))
  286. {
  287. _waferHolderTaskDic.TryRemove(id, out var waferHolderTask);
  288. waferHolderTask.Dispose();
  289. waferHolderTask.OnTaskComplete -= WaferHolderTask_OnTaskComplete;
  290. LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task");
  291. }
  292. lock (_waferHolderTaskObject)
  293. {
  294. int index = _waferTaskList.FindIndex(O => O.ID == id);
  295. if (index != -1)
  296. {
  297. _waferTaskList.RemoveAt(index);
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// 加载WaferHolder任务集合
  303. /// </summary>
  304. /// <returns></returns>
  305. public List<WaferHolderTask> LoaderWaferHolderTaskList()
  306. {
  307. lock (_waferHolderTaskObject)
  308. {
  309. return _waferTaskList;
  310. }
  311. }
  312. /// <summary>
  313. /// 检验所有task是否完成
  314. /// </summary>
  315. /// <returns></returns>
  316. public bool CheckAllTaskComplete()
  317. {
  318. return _waferTaskList.Count == 0;
  319. }
  320. /// <summary>
  321. /// 暂停
  322. /// </summary>
  323. public void PauseAllTask()
  324. {
  325. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  326. foreach (WaferHolderTask item in waferHolderTasks)
  327. {
  328. item.Pause();
  329. }
  330. }
  331. /// <summary>
  332. /// 恢复所有任务
  333. /// </summary>
  334. public void ResumeAllTask()
  335. {
  336. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  337. foreach (WaferHolderTask item in waferHolderTasks)
  338. {
  339. item.Resume();
  340. }
  341. }
  342. /// <summary>
  343. /// 移除所有任务
  344. /// </summary>
  345. public void RemoveAllTask()
  346. {
  347. List<WaferHolderTask> waferHolderTasks = _waferHolderTaskDic.Values.ToList();
  348. foreach (WaferHolderTask item in waferHolderTasks)
  349. {
  350. RemoveById(item.ID);
  351. }
  352. }
  353. /// <summary>
  354. /// 中止WaferHolderTask
  355. /// </summary>
  356. /// <param name="processJobInfo"></param>
  357. public void AbortWaferHolderTaskByProcessJob(ProcessJobInfo processJobInfo)
  358. {
  359. List<string> keys = _waferHolderTaskDic.Keys.ToList();
  360. foreach (string key in keys)
  361. {
  362. WaferHolderTask waferHolderTask = _waferHolderTaskDic[key];
  363. if(waferHolderTask!=null&&waferHolderTask.ProcessJobInfo!=null&&waferHolderTask.ProcessJobInfo.InnerId == processJobInfo.InnerId)
  364. {
  365. _waferHolderTaskDic.TryRemove(key, out waferHolderTask);
  366. RemoveById(waferHolderTask.ID);
  367. LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task");
  368. }
  369. }
  370. }
  371. /// <summary>
  372. /// 是否存在其他WaferTask没有
  373. /// </summary>
  374. /// <returns></returns>
  375. private bool CheckExistWaferHolderTaskNotStart()
  376. {
  377. List<WaferHolderTask> tmp = _waferHolderTaskDic.Values.ToList();
  378. foreach (WaferHolderTask item in tmp)
  379. {
  380. if (item.State == WaferHolderTaskState.Created)
  381. {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. }
  388. }