using Aitex.Core.Common; using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.WaferHolder; using CyberX8_RT.Modules.Loader; using CyberX8_RT.Modules; using CyberX8_RT.Schedulers; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MECF.Framework.Common.Jobs; using CyberX8_RT.Modules.PUF; using CyberX8_Core; using CyberX8_RT.Modules.Transporter; using MECF.Framework.Common.SubstrateTrackings; using MECF.Framework.Common.RecipeCenter; namespace CyberX8_RT.Dispatch { public class WaferHolderTaskManager : Singleton { #region 内部变量 /// /// wafer Holder task字典(key-id,value-waferHolderTask实例) /// private ConcurrentDictionary _waferHolderTaskDic = new ConcurrentDictionary(); /// /// WaferTask集合 /// private List _waferTaskList = new List(); /// /// WaferTask锁 /// private object _waferHolderTaskObject = new object(); #endregion /// /// 创建任务 /// public bool CreateWaferHolderTask(List jobs) { SequenceRecipe sequenceRecipe = jobs[0].SequenceRecipe; List chemistries = SchedulerSequenceRecipeManager.Instance.GetSequenceChemistry(sequenceRecipe); WaferSize waferSize = (WaferSize)sequenceRecipe.SubstrateSize; WaferHolderInfo waferHolderInfo = SchedulerManager.Instance.GetAvaibleBufferToLoaderWaferHolder(waferSize,sequenceRecipe.CrsType,chemistries); if (waferHolderInfo != null && CheckWaferHolderNotExist(waferHolderInfo)) { if (CheckEnableCreateWaferHolderTask()) { if(!Enum.TryParse(waferHolderInfo.CurrentLocation,out ModuleName locationModuleName)) { return false; } if (ModuleHelper.IsBuffer(locationModuleName)&&string.IsNullOrEmpty(waferHolderInfo.OriginalBuffer)) { waferHolderInfo.OriginalBuffer = waferHolderInfo.CurrentLocation; } WaferHolderTask waferHolderTask = new WaferHolderTask(waferHolderInfo, jobs[0]); waferHolderTask.OnTaskComplete += WaferHolderTask_OnTaskComplete; WaferInfo waferInfo = GetPreLoaderHasWafer(); int waferTaskCount = 0; if (waferInfo==null) { waferTaskCount= WaferTaskManager.Instance.CreateWaferTask(jobs, waferHolderInfo, waferHolderTask); } else { if(!CheckWaferMatchedWaferHolderTaskNotCreated(waferInfo)) { return false; } string waferHasMatch = WaferTaskManager.Instance.CheckWaferHasMatch(waferInfo.WaferID); if(string.IsNullOrEmpty(waferHasMatch)) { waferTaskCount = 1; } else { waferTaskCount = 2; } } if(waferTaskCount == 0) { return false; } _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask); lock (_waferHolderTaskObject) { _waferTaskList.Add(waferHolderTask); } LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create wafer Shuttle {waferHolderInfo.Id} task"); List sequences = SchedulerSequenceManager.Instance.AnalyWaferHolderAllSchedulerSequence(waferHolderInfo, jobs[0].SequenceRecipe,waferTaskCount); waferHolderTask.InitialSchedulers(sequences); return true; } } return false; } /// /// 查看Wafer对应的WaferHolderTask是否已经创建 /// /// /// private bool CheckWaferMatchedWaferHolderTaskNotCreated(WaferInfo waferInfo) { WaferHolderTask waferHolderTask = WaferTaskManager.Instance.GetWaferHolderTaskByWaferId(waferInfo.WaferID); return waferHolderTask == null || CheckWaferHolderNotExist(waferHolderTask.WaferHolderInfo); } /// /// 检验是否可以创建WaferHolderTask /// /// private bool CheckEnableCreateWaferHolderTask() { TransporterEntity loaderTransporterEntity = Singleton.Instance.GetModule(ModuleName.Transporter2.ToString()); LoaderEntity loaderEntity = Singleton.Instance.GetModule(ModuleName.Loader1.ToString()); if (loaderEntity == null) { return false; } if (loaderEntity.IsError) { return false; } if (loaderTransporterEntity == null) { return false; } if (loaderEntity.WaferHolderInfo != null) { return false; } if (loaderTransporterEntity.IsError) { return false; } //if (loaderTransporterEntity.WaferHolderInfo != null) //{ // return false; //} //if (loaderTransporterEntity.IsBusy) //{ // return false; //} //存在其他任务没有完成Loader调度 if (CheckExistWaferHolderTaskNotOverLoader()) { return false; } return true; } /// /// 检验Loader前面是否存在Wafer /// /// public WaferInfo GetPreLoaderHasWafer() { WaferInfo waferInfo = null; if (WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0)) { waferInfo = WaferManager.Instance.GetWafer(ModuleName.EfemRobot, 0); return CheckWaferTaskForward(waferInfo); } if (WaferManager.Instance.CheckHasWafer(ModuleName.Aligner1, 0)) { waferInfo = WaferManager.Instance.GetWafer(ModuleName.Aligner1, 0); return CheckWaferTaskForward(waferInfo); } //PUF1 B面 if(WaferManager.Instance.CheckHasWafer(ModuleName.PUF1,1)) { waferInfo = WaferManager.Instance.GetWafer(ModuleName.PUF1, 1); return CheckWaferTaskForward(waferInfo); } return waferInfo; } /// /// 检验Wafer是否在于Loader之前 /// /// /// private WaferInfo CheckWaferTaskForward(WaferInfo waferInfo) { if (!WaferTaskManager.Instance.Contains(waferInfo.WaferID)) { return null; } WaferTask waferTask = WaferTaskManager.Instance.GetWaferTask(waferInfo.WaferID); if (waferTask == null) { return null; } if (waferTask.IsSchedulerForward()) { return waferInfo; } else { return null; } } /// /// 创建Dummy WaferHolder Task /// public void CreateDummyWaferHolderTask() { List waferHolderInfos = DummyWaferManager.Instance.LoadCompletedWaferHolderInfo(); foreach (WaferHolderInfo waferHolderInfo in waferHolderInfos) { if (CheckWaferHolderNotExist(waferHolderInfo)) { if (CheckEnableCreateWaferHolderTask()) { int waferTaskCount = 0; WaferInfo waferInfo = GetPreLoaderHasWafer(); if (waferInfo == null) { List waferInfos = DummyWaferManager.Instance.LoadDummyWafersByWaferHolder(waferHolderInfo); if (waferInfos.Count != 0) { WaferHolderTask waferHolderTask = new WaferHolderTask(waferHolderInfo, null); waferHolderTask.OnTaskComplete += WaferHolderTask_OnTaskComplete; waferTaskCount = WaferTaskManager.Instance.CreateDummyWaferTask(waferInfos, waferHolderInfo.SequenceRecipe, waferHolderTask); if (waferTaskCount == 0) { break; } _waferHolderTaskDic.TryAdd(waferHolderTask.ID, waferHolderTask); lock (_waferHolderTaskObject) { _waferTaskList.Add(waferHolderTask); } LOG.WriteLog(eEvent.EV_SEQUENCE, "Scheduler", $"Create dummy wafer Shuttle {waferHolderInfo.Id} task"); List sequences = SchedulerSequenceManager.Instance.AnalyseDummyWaferHolderAllSchedulerSequence(waferHolderInfo, waferTaskCount); waferHolderTask.InitialSchedulers(sequences); break; } } } } } } /// /// wafer Holder Task完成事件 /// /// private void WaferHolderTask_OnTaskComplete(string id) { RemoveById(id); } /// /// 检验WaferHolderTask是否完成Loader调度 /// /// private bool CheckExistWaferHolderTaskNotOverLoader() { List waferHolderTasks = _waferHolderTaskDic.Values.ToList(); foreach(var task in waferHolderTasks) { if (task.IsNotPassLoader()) { return true; } } return false; } /// /// 是否包含该任务 /// /// /// public bool CheckWaferHolderNotExist(WaferHolderInfo waferHolderInfo) { List waferHolderTasks = _waferHolderTaskDic.Values.ToList(); return waferHolderTasks.Find(O => O.WaferHolderInfo.Id == waferHolderInfo.Id) == null; } /// /// 移除 /// /// public void RemoveById(string id) { if (_waferHolderTaskDic.ContainsKey(id)) { _waferHolderTaskDic.TryRemove(id, out var waferHolderTask); waferHolderTask.Dispose(); waferHolderTask.OnTaskComplete -= WaferHolderTask_OnTaskComplete; LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task"); } lock (_waferHolderTaskObject) { int index = _waferTaskList.FindIndex(O => O.ID == id); if (index != -1) { _waferTaskList.RemoveAt(index); } } } /// /// 加载WaferHolder任务集合 /// /// public List LoaderWaferHolderTaskList() { lock (_waferHolderTaskObject) { return _waferTaskList; } } /// /// 检验所有task是否完成 /// /// public bool CheckAllTaskComplete() { return _waferTaskList.Count == 0; } /// /// 暂停 /// public void PauseAllTask() { List waferHolderTasks = _waferHolderTaskDic.Values.ToList(); foreach (WaferHolderTask item in waferHolderTasks) { item.Pause(); } } /// /// 恢复所有任务 /// public void ResumeAllTask() { List waferHolderTasks = _waferHolderTaskDic.Values.ToList(); foreach (WaferHolderTask item in waferHolderTasks) { item.Resume(); } } /// /// 移除所有任务 /// public void RemoveAllTask() { List waferHolderTasks = _waferHolderTaskDic.Values.ToList(); foreach (WaferHolderTask item in waferHolderTasks) { RemoveById(item.ID); } } /// /// 中止WaferHolderTask /// /// public void AbortWaferHolderTaskByProcessJob(ProcessJobInfo processJobInfo) { List keys = _waferHolderTaskDic.Keys.ToList(); foreach (string key in keys) { WaferHolderTask waferHolderTask = _waferHolderTaskDic[key]; if(waferHolderTask!=null&&waferHolderTask.ProcessJobInfo!=null&&waferHolderTask.ProcessJobInfo.InnerId == processJobInfo.InnerId) { _waferHolderTaskDic.TryRemove(key, out waferHolderTask); RemoveById(waferHolderTask.ID); LOG.WriteBackgroundLog(eEvent.EV_SCHEDULER, "Scheduler", $"remove wafer Shuttle {waferHolderTask.WaferHolderInfo.Id} task"); } } } /// /// 是否存在其他WaferTask没有 /// /// private bool CheckExistWaferHolderTaskNotStart() { List tmp = _waferHolderTaskDic.Values.ToList(); foreach (WaferHolderTask item in tmp) { if (item.State == WaferHolderTaskState.Created) { return true; } } return false; } } }