private void timer1_updateJobStatus(object sender) { t_AGVWork agvWork1 = null; if (autoUpdateJobStatus) { using (var ctx = new AMSContext()) { var transaction = ctx.Database.BeginTransaction(); try { //read List <t_AGVWork> agvWorks = ctx.t_AGVWork //.OrderBy(b => b.BlogId) .Where(b => b.JobStatus == 5 || b.JobStatus == 8 || b.JobStatus == 11 || b.JobStatus == 14 || b.JobStatus == 100) .ToList(); //Console.WriteLine(agvWork1); if (agvWorks != null && agvWorks.Count > 0) { //按照任务状态 5,8,11,14更新不同的数值 agvWork1 = agvWorks.First(); if (agvWork1.JobStatus == 5) { agvWork1.JobStatus = 6; } else if (agvWork1.JobStatus == 8) { agvWork1.JobStatus = 9; //stationRelease = true; //取货完成确认后才允许接收新的搬送请求 } else if (agvWork1.JobStatus == 11) { //检查站台状态,如果发现站台不可卸货,则修改目的地 if (checkDestinationStation()) { agvWork1.JobStatus = 12; } else { updateTask2Clearing(agvWork1); } } else if (agvWork1.JobStatus == 14) { agvWork1.JobStatus = 15; } ctx.t_AGVWork.Attach(agvWork1); if (agvWork1.JobStatus == 100) //删除 { ctx.t_AGVWork.Remove(agvWork1); ctx.SaveChanges(); t_Station tStation5 = ctx.t_Station .Where(b => b.StationNo == agvWork1.Destination).SingleOrDefault(); ctx.t_Station.Attach(tStation5); tStation5.OccupiedStatus = 2; tStation5.ModifyProgID = 201; tStation5.ModifyTime = DateTime.Now; var setEntry = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(tStation5); setEntry.SetModifiedProperty("OccupiedStatus"); setEntry.SetModifiedProperty("ModifyProgID"); setEntry.SetModifiedProperty("ModifyTime"); ctx.SaveChanges(); //updateGUIStationStautus(tStation5, 2); toolStripStatusLabel1.Text = "任务从 " + agvWork1.Origination + " 到 " + agvWork1.Destination + " 已经正常完成,站台 " + tStation5.StationNo + " 当前已有托盘"; } else { agvWork1.ModifyProgID = 201; var setEntry = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(agvWork1); //只修改实体的Name属性和Age属性 setEntry.SetModifiedProperty("JobStatus"); setEntry.SetModifiedProperty("ModifyProgID"); ctx.SaveChanges(); } //Console.WriteLine("update status:", updateFlag); transaction.Commit(); String jobStatus = agvWork1.JobStatus.ToString(); String jobStatusName = ctx.t_Code .Where(b => b.codetype == jobStatus && b.code == "jobstatus").First().name; toolStripStatusLabel1.Text = "从 " + agvWork1.Origination + " 到 " + agvWork1.Destination + " 的任务 " + jobStatusName; } } catch (Exception ex) { transaction.Rollback(); Console.WriteLine(DateTime.Now.ToString() + " timer1_updateJobStatus 更新任务状态失败" + agvWork1.ID); MessageBox.Show(ex.ToString()); } finally { transaction.Dispose(); } } } //System.Threading.Thread.Sleep(2000); //用一个线程控制,睡2秒后重新开始 }
//创建任务-事务,插入作业任务agvwork数据和更新站台状态agvstation在同一个事务内完成 private Boolean createTask(t_AGVWork agvWork2) { t_Station agvStation1; //创建任务 using (var ctx = new AMSContext()) { //检查是否有目的地为同一个站台的任务还在执行中 List <t_AGVWork> agvWorks = ctx.t_AGVWork .Where(b => b.Destination == agvWork2.Destination) .ToList(); //Console.WriteLine(agvWork1); if (agvWorks.Count > 0) { toolStripStatusLabel1.Text = DateTime.Now.ToString() + " 已经存在一条目的地为" + agvWork2.Destination + "的任务了,不能重复创建"; Console.WriteLine(DateTime.Now.ToString() + " createTask:已经存在一条目的地为" + agvWork2.Destination + "的任务了,不能重复创建"); return(false); } var transaction = ctx.Database.BeginTransaction(); try { agvWork2.ModifyProgID = 101; agvWork2.ModifyTime = DateTime.Now; ctx.t_AGVWork.Add(agvWork2); //插入agv作业任务 ctx.SaveChanges(); agvWork2.JobId = agvWork2.ID; var setEntry = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(agvWork2); setEntry.SetModifiedProperty("JobId"); ctx.SaveChanges(); //更新站台状态为预约 agvStation1 = ctx.t_Station .Where(b => b.StationNo == agvWork2.Destination) .First(); agvStation1.OccupiedStatus = 1; //空满状态 0: 空 1:预约中 2:满 agvStation1.ModifyProgID = 101; agvStation1.ModifyTime = DateTime.Now; ctx.t_Station.Attach(agvStation1); var setEntry1 = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(agvStation1); setEntry1.SetModifiedProperty("OccupiedStatus"); setEntry1.SetModifiedProperty("ModifyProgID"); setEntry1.SetModifiedProperty("ModifyTime"); ctx.SaveChanges(); transaction.Commit(); Console.WriteLine(DateTime.Now.ToString() + " createTask:任务" + agvWork2.ID + " 已创建,站台 " + agvWork2.Destination + " 状态更新为(预约中)"); toolStripStatusLabel1.Text = " 从 " + agvWork2.Origination + " 到 " + agvWork2.Destination + " 的搬运任务已创建"; return(true); } catch (Exception) { transaction.Rollback(); Console.WriteLine(DateTime.Now.ToString() + " createTask:失败了,事务回滚"); } finally { transaction.Dispose(); } } return(false); }
//更新任务,将AGV送到clearing station ProgID 105 private Boolean updateTask2Clearing(t_AGVWork agvWork2) { t_Station agvStation1; //更新任务 using (var ctx = new AMSContext()) { var transaction = ctx.Database.BeginTransaction(); try { agvWork2.ModifyProgID = 105; agvWork2.ModifyTime = DateTime.Now; agvWork2.RedirectFlag = 1; agvWork2.RedirectPosition = 9999; agvWork2.JobStatus = 61; ctx.t_AGVWork.Attach(agvWork2); var setEntry = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(agvWork2); setEntry.SetModifiedProperty("ModifyProgID"); setEntry.SetModifiedProperty("ModifyTime"); setEntry.SetModifiedProperty("RedirectFlag"); setEntry.SetModifiedProperty("RedirectPosition"); setEntry.SetModifiedProperty("JobStatus"); ctx.SaveChanges(); //更新新的站台为预约 //更新站台状态为疑问 agvStation1 = ctx.t_Station .Where(b => b.StationNo == agvWork2.Destination) .SingleOrDefault(); agvStation1.OccupiedStatus = 3; //空满状态 0: 空 1:预约中 2:满 3: 存疑 agvStation1.ModifyProgID = 105; agvStation1.ModifyTime = DateTime.Now; ctx.t_Station.Attach(agvStation1); var setEntry1 = ((IObjectContextAdapter)ctx).ObjectContext.ObjectStateManager.GetObjectStateEntry(agvStation1); setEntry1.SetModifiedProperty("OccupiedStatus"); setEntry1.SetModifiedProperty("ModifyProgID"); setEntry1.SetModifiedProperty("ModifyTime"); ctx.SaveChanges(); transaction.Commit(); Console.WriteLine(DateTime.Now.ToString() + " updateTask:任务" + agvWork2.ID + " 已更新,站台 " + agvWork2.Destination + " 状态更新为(存疑)"); toolStripStatusLabel1.Text = " 从 " + agvWork2.Origination + " 到 " + agvWork2.Destination + " 的搬运任务已更新"; return(true); } catch (Exception) { transaction.Rollback(); Console.WriteLine(DateTime.Now.ToString() + " createTask:失败了,事务回滚"); } finally { transaction.Dispose(); } } return(false); }
//触发任务 private Boolean triggerTask(int materialType, int stationNo) { t_AGVPath agvPath1; t_Station agvStation1; //根据规则找到找路径,一般只能找到一条,如果找到多条,按照规则选一条 using (var ctx = new AMSContext()) { //read agvStation1 = ctx.t_Station .Where(b => b.StationNo == stationNo) .SingleOrDefault(); agvPath1 = getPath(agvStation1, materialType); } if (agvPath1 == null) { Console.WriteLine(DateTime.Now.ToString() + " triggerTask:根据" + agvStation1.StationNo + " 和拉动信号类型 " + materialType + " 找不到对应的路径"); return(false); } //如果路径下有多个站台,按照策略选一个站台 t_Station agvStation2 = getAvailabeStation(agvPath1); //构建任务实体 if (agvStation2 != null) { t_AGVWork agvWork1 = new t_AGVWork { JobType = 0, Origination = agvStation1.StationNo, Destination = agvStation2.StationNo, //JobId = 10000, Priority = 0, JobStatus = 0, TUType = 0, AGVCancelFlag = 0, CancelFlag = 0, WMSCancelFlag = 0, RedirectFlag = 0 }; //创建任务并更新货位状态(货位预约,空满状态 0: 空 1:预约中 2:满) if (createTask(agvWork1)) { //更新界面颜色,将对应货位的颜色改为黄色(预约中) //updateGUIStationStautus(agvStation1, 1); stationRelease = false; checkbox_stationRelease.Checked = false; //站台不可再接收其他的搬运指令 checkbox_stationRelease.Enabled = false; //而且不允许更改 } else { toolStripStatusLabel1.Text = "创建任务失败"; Console.WriteLine(DateTime.Now.ToString() + " triggerTask:根据起始站台: " + agvStation1.StationNo + " 和目的站台 " + agvStation2.StationNo + " 创建任务失败"); return(false); } //更新路径上任务数量-本次不实现(控制一条路径上的流量) } else { //toolStripStatusLabel1.Text = "无可用站台"; Console.WriteLine(DateTime.Now.ToString() + " triggerTask:根据起始站台: " + stationNo + " 和目的通道 " + agvPath1.Destination + " 找不到可用的站台"); return(false); } return(true); }