using System; using System.Linq; using System.Threading; using WIDESEA_Common; using WIDESEA_Common.Tools; using WIDESEA_Core.EFDbContext; using WIDESEA_Core.Utilities; using WIDESEA_Entity.DomainModels; using WIDESEA_Services.IRepositories; using WIDESEA_Services.Repositories; using WIDESEA_Services.Services; using WIDESEA_WCS.WCSClient; namespace WIDESEA_WCS.SchedulerExecute.AGV { public partial class AGVSchedulerExecute { private static int _readUpdateAGVTaskSignalso = 0; public static void UpdateState(PLCClient plcClient) { if (Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 1) == 0) { try { if (plcClient == null) { return; } VOLContext context = new VOLContext(); Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(context); Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context); Idt_agvtask_htyRepository agvtask_HtyRepository = new dt_agvtask_htyRepository(context); Ibase_ware_locationRepository locationRepository = new base_ware_locationRepository(context); Ibill_group_stockRepository group_StockRepository = new bill_group_stockRepository(context); dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep); int taskFbInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveR.ToString())); int taskFbInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); //0初始状态 1 RCS更新了一条任务状态 if (1 == taskFbInteractive && taskFbInteractiveW == 0) { string taskId = plcClient.ReadValue(TaskDBName.taskIDFb.ToString()).ToString();//任务ID if (taskId.Contains("\b")) { taskId = taskId.Remove(0, 1); } int taskState = int.Parse(plcClient.ReadValue(TaskDBName.taskStatusFb.ToString()).ToString());//1起点执行中,2起点已完成,3终点执行中,4终点已完成 if (0 == taskState)//无含义 return; //WriteLog.GetLog().Write("任务号" + taskId + "任务状态" + taskState, "AGV更新任务状态"); dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_tasknum == taskId).FirstOrDefault(); if (1 == taskState)//任务起点执行中 { if (null == agvTask) { throw new Exception("起点执行失败未找到任务,ID:" + taskId); } if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点执行中状态确认1", "AGV更新任务状态"); } agvTask.agv_taskstate = AGVTaskStateEnum.Executing.ToString(); agvTask.agv_finishedtime = DateTime.Now; agvtaskRepository.Update(agvTask, true); plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + "任务起点执行中状态确认1", "AGV更新任务状态"); } else if (2 == taskState)//任务起点已完成 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.GetLog().Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); return; } if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing.ToString()) { throw new Exception("任务状态更改为2起点已完成失败,任务ID:" + agvTask.agv_tasknum + DateTime.Now + ",任务状态不是起点执行中!"); } if (agvTask.agv_tasknum.Contains(":")) { if (agvTask.agv_fromaddress.Contains("ZZLJ")) { var loc = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_toaddress); loc.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(loc, true); } //agvtask_HtyRepository.AddTaskHistory(agvTask, OperateType.Finished.ToString()); agvtaskRepository.Delete(agvTask, true); plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } else { agvTask.agv_taskstate = AGVTaskStateEnum.Complete.ToString(); //货位状态改成空,location_id为空 bill_group_stock stock = new bill_group_stock(); if (agvTask.agv_fromaddress.Contains("0201") && agvTask.agv_toaddress.Contains("ZZLJ")) { } else if (agvTask.agv_fromaddress.Contains("WLX") && agvTask.agv_toaddress.Contains("0101")) { } else if (agvTask.agv_toaddress.Contains("GMHX")) { } else if (agvTask.agv_toaddress.Contains("HXWLX")) { if (!agvTask.agv_fromaddress.Contains("KPHLX")) { var materbarcode = ""; /*agvTask.agv_materbarcode.Split(";");*/ for (int i = 0; i < materbarcode.Count(); i++) { stock = null;/*group_StockRepository.Find(f => f.BarCode == materbarcode[i]).OrderByDescending(f => f.created_time).FirstOrDefault();*/ if (stock != null) { stock.location_id = null; group_StockRepository.Update(stock, true); } if (materbarcode[0].Equals(materbarcode[1])) { break; } } } } else { stock = null;// group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault(); if (stock != null) { stock.location_id = null; group_StockRepository.Update(stock, true); } } base_ware_location location = locationRepository.Find(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_fromaddress).FirstOrDefault(); if (location != null) { location.location_state = LocationStateEnum.LocationState_Empty.ToString(); locationRepository.Update(location, true); if (agvTask.agv_fromaddress.Contains("HCJ") || agvTask.agv_fromaddress.Contains("CBJ") || agvTask.agv_fromaddress.Contains("JJK") || agvTask.agv_fromaddress.Contains("ZZLJ")) { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0); //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0); } } //如果是从库内出库,需要给WMS反馈出库物料确认搬走 if (agvTask.agv_fromaddress.Contains("JJK")) { MESback WMSbackresult = new MESback(); if (agvTask.agv_fromaddress.Contains("Z")) { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 1, 0); } else if (agvTask.agv_fromaddress.Contains("F")) { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 2, 0); } if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } WriteLog.GetLog().Write("WCS反馈WMS任务号" + taskId + "任务起点完", "AGV更新任务状态"); } else if (agvTask.agv_fromaddress.Contains("0101") || agvTask.agv_fromaddress.Contains("0201")) { MESback WMSbackresult = new MESback(); if (agvTask.agv_toaddress.Contains("ZZLJ")) { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, "空托盘", 3, 0); } else { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 3, 0); } if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } WriteLog.GetLog().Write("WCS反馈WMS任务号" + taskId + "任务起点完", "AGV更新任务状态"); } else if (agvTask.agv_fromaddress.Contains("FBT")) { var iswork = agvtaskRepository.FindFirst(f => f.agv_remark == "true" && f.agv_tasknum == taskId); if (iswork != null) { // var sum = "";//agvtaskRepository.Find(f => f.agv_materbarcode.Contains(iswork.agv_materbarcode.Substring(0, 2)) && f.agv_toaddress.Contains(iswork.agv_toaddress.Substring(0, 2))).Count(); MESback WMSbackresult = new MESback(); if (agvTask.agv_fromaddress.Contains("Z")) { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, sum); } else if (agvTask.agv_fromaddress.Contains("F")) { //WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, sum); } if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } } agvtaskRepository.Update(agvTask, true); plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } } else if (3 == taskState)//任务终点执行中 { if (null == agvTask) { throw new Exception("终点执行失败未找到任务,ID:" + taskId); } if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.GetLog().Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态"); } if (agvTask.agv_tasknum.Contains(":")) { string[] taskidsplit = agvTask.agv_tasknum.Split(":"); var alltask = agvtaskRepository.Find(v => v.agv_tasknum.Contains(taskidsplit[0])).ToList(); foreach (var item in alltask) { //agvtask_HtyRepository.AddTaskHistory(item, OperateType.Abnormal.ToString()); agvtaskRepository.Delete(item, true); if (item.agv_fromaddress.Contains("ZZLJ")) { var loc = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_toaddress); loc.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(loc, true); } } plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "分段任务异常确认1", "AGV更新任务状态"); } else { agvTask.agv_taskstate = AGVTaskStateEnum.Executing1.ToString(); //agvTask.agv_realesstime = DateTime.Now; agvtaskRepository.Update(agvTask, true); plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态"); } } else if (4 == taskState)//任务终点完成 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.GetLog().Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态"); return; } else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing1.ToString()) { //plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); throw new Exception("任务状态更改为4终点已完成失败,任务ID:" + agvTask.agv_tasknum + DateTime.Now + ",任务状态不是终点执行中!"); } //如果是设备就不用更改,是缓存架货位状态为已存储,(库存表)location_id=(货位表)的货位 base_ware_location location = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_toaddress || f.down_code == agvTask.agv_toaddress); bill_group_stock stock = new bill_group_stock(); if (agvTask.agv_fromaddress.Contains("0201") && agvTask.agv_toaddress.Contains("ZZLJ")) { //location.location_state = LocationStateEnum.LocationState_Wait.ToString(); locationRepository.Update(location, true); } else if (agvTask.agv_toaddress.Contains("LJHCX")) { } else if (agvTask.agv_fromaddress.Contains("WLX") && agvTask.agv_toaddress.Contains("0101")) { location.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(location, true); //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, "空托盘", agvTask.agv_tasknum, "OK", 3); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } else if (agvTask.agv_toaddress.Contains("GMHX")) { //var materbarcode = agvTask.agv_materbarcode.Split(","); //List materials = new List(); //for (int i = 0; i < materbarcode.Count(); i++) //{ // BakingClass bakingClass = new BakingClass(); // bakingClass.BarCode = materbarcode[i]; // bakingClass.MaterialType = agvTask.agv_materielid; // materials.Add(bakingClass); // WriteLog.GetLog("烘烤").Write("materials" + materials[i].BarCode + "\t" + materials[i].MaterialType + "\t" + bakingClass.BarCode + "\t" + bakingClass.MaterialType + DateTime.Now, "烘烤物料条码"); //} //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } else if (agvTask.agv_toaddress.Contains("HXWLX")) { //如果是进烘箱的任务,需要告诉WMS两个进烘箱物料的条码和位置,需要问胡工怎么给他值 if (agvTask.agv_fromaddress.Contains("KPHLX")) { //var materials = WebApiHelper.ParseFromJson>(agvTask.agv_materbarcode); //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } else { WebResponseContent content = new WebResponseContent(); group_StockRepository.DbContextBeginTransaction(() => { //var materbarcode = agvTask.agv_materbarcode.Split(";"); //List materials = new List(); //for (int i = 0; i < materbarcode.Count(); i++) //{ // BakingClass bakingClass = new BakingClass(); // stock = group_StockRepository.Find(f => f.BarCode == materbarcode[i]).OrderByDescending(f => f.created_time).FirstOrDefault(); // bakingClass.BarCode = materbarcode[i]; // bakingClass.MaterialType = stock.MaterialType; // materials.Add(bakingClass); // group_StockRepository.Delete(stock, true); // WriteLog.GetLog("烘烤").Write("materials" + materials[i].BarCode + "\t" + materials[i].MaterialType + "\t" + bakingClass.BarCode + "\t" + bakingClass.MaterialType + DateTime.Now, "烘烤物料条码"); //} //MESback WMSbackresult = MESAPIInvoke.BakingFeedingBinding(agvTask.agv_toaddress, materials); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return WebResponseContent.Instance.Error(WMSbackresult.Message); } //如果终点地址是去烘烤箱的删除库存 location.location_state = LocationStateEnum.LocationState_Empty.ToString(); locationRepository.Update(location, true); return WebResponseContent.Instance.OK(); }); } } else { //stock = group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault(); if (!agvTask.agv_toaddress.Contains("SB") && !agvTask.agv_fromaddress.Contains("WLX") && !agvTask.agv_toaddress.Contains("JJK") && !agvTask.agv_toaddress.Contains("WLX")) { if (stock == null) { //bill_group_stock newstock = new bill_group_stock //{ // BarCode = agvTask.agv_materbarcode, // MaterialType = agvTask.agv_materielid, // MaterialStatus = "nou", // first_tb = 0, // location_id = location.id, // TB_Status = "", // FQ_Status = "", // GY_Status = "", // QX_Status = "", // created_time = DateTime.Now, // created_user = "WCS", // updated_time = DateTime.Now, // updated_user = "WCS" //}; //group_StockRepository.Add(newstock, true); } else { if (agvTask.agv_fromaddress.Contains("JJK")) { stock.TB_Status = ""; stock.FQ_Status = ""; stock.GY_Status = ""; stock.QX_Status = ""; } stock.location_id = location.id; stock.created_time = DateTime.Now; stock.updated_time = DateTime.Now; group_StockRepository.Update(stock, true); } location.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(location, true); if (agvTask.agv_toaddress.Contains("HCJ") || agvTask.agv_toaddress.Contains("CBJ") || agvTask.agv_toaddress.Contains("ZZLJ")) { if (!agvTask.agv_fromaddress.Contains("SB")) { if (stock.MaterialStatus == "OK") { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1); } else { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_toaddress, 1); } } } //如果终点地址是库内,需要给WMS反馈入库物料绑定 if (agvTask.agv_toaddress.Length < 12) { //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 3); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } } else if (agvTask.agv_toaddress.Contains("SB")) { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0); //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0); //如果终点地址是去设备 group_StockRepository.Delete(stock, true); } else if (agvTask.agv_toaddress.Contains("WLX001")) { if (agvTask.agv_code.Contains("正")) { //添加物料条码,三楼输送线出口取前两个条码 var barcode = tasknumberRep.FindFirst(v => v.taskno == 1); //barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";"; tasknumberRep.Update(barcode, true); } else { //添加物料条码,三楼输送线出口取前两个条码 var barcode = tasknumberRep.FindFirst(v => v.taskno == 2); //barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";"; tasknumberRep.Update(barcode, true); } } else if (agvTask.agv_toaddress.Contains("JJK")) { location.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(location, true); if (stock.MaterialStatus == "OK") { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0); } else { //plcClient.WriteValue(ConveyorLineGetLogDBName.MaterNG.ToString(), agvTask.agv_toaddress, 0); } if (agvTask.agv_toaddress.Contains("Z")) { //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 1); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); } } else if (agvTask.agv_toaddress.Contains("F")) { //MESback WMSbackresult = MESAPIInvoke.InStockMaterBind(agvTask.agv_toaddress, stock.MaterialType, stock.BarCode, stock.MaterialStatus, 2); //if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); } } } } agvtaskRepository.Delete(agvTask, true); plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); } else { break; } } WriteLog.GetLog().Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态"); } } //if (taskFbInteractive == 1 && taskFbInteractiveW == 1)//反馈没收到重新反馈 // plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0); if (taskFbInteractive == 0 && taskFbInteractiveW == 1) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0); for (int i = 0; i < 5; i++) { Thread.Sleep(300); var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskFbInteractiveW.ToString())); if (agvnumber != 0) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 0); } else { break; } } } } catch (Exception ex) { WriteLog.GetLog().Write("WCS接收AGV任务反馈异常" + ex.Message.ToString() + DateTime.Now, "AGV更新任务状态"); } finally { Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 0); } } } } }