using System; using System.Collections.Generic; using System.Linq; using System.Text; 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; 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())); int resetTaskFbInteractiver = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskFbInteractiveR.ToString())); //0初始状态 1 RCS更新了一条任务状态 if (resetTaskFbInteractiver == 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; } } } 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.Info("AGV更新任务状态").Write("任务号" + taskId + "任务状态" + taskState, "AGV更新任务状态"); dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_tasknum == taskId).FirstOrDefault(); if (1 == taskState)//任务起点执行中 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点执行中状态确认1", "AGV更新任务状态"); } if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + "任务起点执行中状态确认1", "AGV更新任务状态"); } else if (2 == taskState)//任务起点已完成 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); return; } if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS再次写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } else if (agvTask.agv_taskstate != AGVTaskStateEnum.Executing.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号的任务状态不是起点执行中," + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } 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.Info("AGV更新任务状态").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 = 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 = 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")) { if (stock.MaterialStatus=="OK") { plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0); } else { plcClient.WriteValue(ConveyorLineInfoDBName.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.Info("AGV更新任务状态").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.Info("AGV更新任务状态").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) { MESback WMSbackresult = new MESback(); WriteLog.Info("AGV更新任务状态").Write("分拨台调用OutStockMaterMove" + taskId , "AGV更新任务状态"); WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, Convert.ToInt32(iswork.agv_qty)); if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); } } } 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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务起点完成状态确认1", "AGV更新任务状态"); } } else if (3 == taskState)//任务终点执行中 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态"); } if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString()) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").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.Info("AGV更新任务状态").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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点执行中状态确认1", "AGV更新任务状态"); } } else if (4 == taskState)//任务终点完成 { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").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("JR")) { } 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.Info("烘烤").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 { 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); WriteLog.Info("烘烤").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) { throw new Exception(WMSbackresult.Message); } WebResponseContent content = new WebResponseContent(); content =group_StockRepository.DbContextBeginTransaction(() => { var hxstock1 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[0]); group_StockRepository.Delete(hxstock1, true); var hxstock2 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[1]); group_StockRepository.Delete(hxstock2, true); //如果终点地址是去烘烤箱的删除库存 //location.location_state = LocationStateEnum.LocationState_Empty.ToString(); // locationRepository.Update(location, true); return WebResponseContent.Instance.OK(); }); if (content.Status == false) { throw new Exception("删除库存不成功"); } } } else { stock = group_StockRepository.Find(f => f.BarCode == agvTask.agv_materbarcode).OrderByDescending(f => f.created_time).FirstOrDefault(); if ((agvTask.agv_toaddress.Contains("JJK") || agvTask.agv_toaddress.Contains("0101"))&&(stock==null||stock.MaterialStatus=="nou")) { WriteLog.Info("入库库存数据错误").Write(stock.MaterialStatus+"\t"+stock.BarCode+"\t" + taskId, "入库库存数据错误"); stock.MaterialStatus = agvTask.agv_materbarcode.Split('*')[6]; group_StockRepository.Update(stock,true); throw new Exception("库存数据不对"); } 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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1); } else { plcClient.WriteValue(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_toaddress, 1); } } if (agvTask.agv_toaddress.Contains("TBHCJ")) { group_StockRepository.Delete(stock, true); } } //如果终点地址是库内,需要给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(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0); plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_fromaddress, 0); if (agvTask.agv_toaddress.Contains("TBSB")) { char[] batchnos = new char[100]; string materialtype = agvTask.agv_materbarcode.Split('*')[0]; for (int i = 0; i < materialtype.Length; i++) { batchnos[i] = materialtype[i]; } PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress); string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_TBSB_BatchNo.ToString(), agvTask.agv_toaddress, batchnos).ToString(); WriteLog.Info("W_TBSB_BatchNo").Write("W_TBSB_BatchNo" + agvTask.agv_tasknum +"\t"+agvTask.agv_toaddress+"\t"+materialtype + DateTime.Now, "W_TBSB_BatchNo"); } else if(agvTask.agv_toaddress.Contains("GYSB")) { PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress); plc.WriteValue(ConveyorLineInfoDBName.W_GYSB_BatchNo.ToString(), agvTask.agv_toaddress, " ").ToString(); string materialtype = agvTask.agv_materbarcode.Split('*')[0]; string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_GYSB_BatchNo.ToString(), agvTask.agv_toaddress, materialtype).ToString(); WriteLog.Info("W_GYSB_BatchNo").Write("W_GYSB_BatchNo" + agvTask.agv_tasknum + "\t" + agvTask.agv_toaddress + "\t" + materialtype + DateTime.Now, "W_GYSB_BatchNo"); } else if (agvTask.agv_toaddress.Contains("FTSB")) { PLCClient plc = WCSService.Clients.Find(v => v.PLCDescroption == agvTask.agv_toaddress); plc.WriteValue(ConveyorLineInfoDBName.W_FQSB_BatchNo.ToString(), agvTask.agv_toaddress, " ").ToString(); string materialtype = agvTask.agv_materbarcode.Split('*')[0]; string ismateriala = plc.WriteValue(ConveyorLineInfoDBName.W_FQSB_BatchNo.ToString(), agvTask.agv_toaddress, materialtype).ToString(); WriteLog.Info("W_FQSB_BatchNo").Write("W_FQSB_BatchNo" + agvTask.agv_tasknum + "\t" + agvTask.agv_toaddress + "\t" + materialtype + DateTime.Now, "W_FQSB_BatchNo"); } //如果终点地址是去设备 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); WriteLog.Info("正极烘箱保存条码").Write(barcode.numtype+"\t" + DateTime.Now, "正极烘箱保存条码"); } else { //添加物料条码,三楼输送线出口取前两个条码 var barcode = tasknumberRep.FindFirst(v => v.taskno == 2); barcode.numtype = barcode.numtype + agvTask.agv_materbarcode + ";"; tasknumberRep.Update(barcode, true); WriteLog.Info("负极烘箱保存条码").Write(barcode.numtype + "\t" + DateTime.Now, "负极烘箱保存条码"); } } else if (agvTask.agv_toaddress.Contains("JJK")) { location.location_state = LocationStateEnum.LocationState_Stored.ToString(); locationRepository.Update(location, true); if (stock.MaterialStatus == "OK") { plcClient.WriteValue(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0); } else { plcClient.WriteValue(ConveyorLineInfoDBName.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) { return; } } 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) { return; } } } } // 上传AGV运行数据 by xiaoyang SendMESTask.SendMesTask(agvTask, 1); 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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务终点完成状态确认1", "AGV更新任务状态"); } else if (5 == taskState) { if (null == agvTask) { plcClient.WriteValue(TaskDBName.taskFbInteractiveW.ToString(), 1); WriteLog.Info("AGV更新任务状态").Write("WCS写入AGV任务号,当前任务号不存在任务列表里," + taskId + DateTime.Now + "任务取消状态确认1", "AGV更新任务状态"); } if (agvTask.agv_taskstate == AGVTaskStateEnum.Executing.ToString()) { dt_agvtask agv = new dt_agvtask() { agv_id = new Guid(), agv_barcode = agvTask.agv_barcode, agv_code = agvTask.agv_code, agv_createtime = DateTime.Now, agv_fromaddress = agvTask.agv_fromaddress, agv_grade = agvTask.agv_grade, agv_materbarcode = agvTask.agv_materbarcode, agv_materielid = agvTask.agv_materielid, agv_qty = agvTask.agv_qty, agv_remark = agvTask.agv_remark, agv_taskstate = "Create", agv_tasktype = agvTask.agv_tasktype, agv_toaddress = agvTask.agv_toaddress, agv_userid = agvTask.agv_userid, agv_worktype = agvTask.agv_worktype }; if (agv.agv_fromaddress.Contains("JJK")&&agv.agv_toaddress.Contains("TBHCJ")) { agv.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep)+agvTask.agv_tasknum.Substring(agvTask.agv_tasknum.Length-2,2); } else { agv.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep); } agvtaskRepository.Add(agv, true); } else if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString() || agvTask.agv_taskstate == AGVTaskStateEnum.Executing1.ToString() || agvTask.agv_taskstate == AGVTaskStateEnum.Complete1.ToString()) { //如果是设备就不用更改,是缓存架货位状态为已存储,(库存表)location_id=(货位表)的货位 base_ware_location fromlocation = locationRepository.FindFirst(f => f.upper_code == agvTask.agv_fromaddress || f.down_code == agvTask.agv_fromaddress); if (fromlocation!=null) { fromlocation.location_state = LocationStateEnum.LocationState_Empty.ToString(); locationRepository.Update(fromlocation,true); } 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_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("JR")) { if (agvTask.agv_taskstate == AGVTaskStateEnum.Complete.ToString()) { var iswork = agvtaskRepository.FindFirst(f => f.agv_remark == "true" && f.agv_tasknum == taskId); if (iswork != null) { MESback WMSbackresult = new MESback(); WMSbackresult = MESAPIInvoke.OutStockMaterMove(agvTask.agv_fromaddress, agvTask.agv_materielid, 4, Convert.ToInt32(iswork.agv_qty), agvTask.agv_tasknum); if (WMSbackresult.Code > 0) { new Exception(WMSbackresult.Message); return; } } } } else if (agvTask.agv_toaddress.Contains("LJHCX")) { } 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 { 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); WriteLog.Info("烘烤").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) { throw new Exception(WMSbackresult.Message); } WebResponseContent content = new WebResponseContent(); content = group_StockRepository.DbContextBeginTransaction(() => { var hxstock1 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[0]); group_StockRepository.Delete(hxstock1, true); var hxstock2 = group_StockRepository.FindFirst(f => f.BarCode == materbarcode[1]); group_StockRepository.Delete(hxstock2, true); //如果终点地址是去烘烤箱的删除库存 //location.location_state = LocationStateEnum.LocationState_Empty.ToString(); // locationRepository.Update(location, true); return WebResponseContent.Instance.OK(); }); if (content.Status == false) { throw new Exception("删除库存不成功"); } } } 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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 1); } else { plcClient.WriteValue(ConveyorLineInfoDBName.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(ConveyorLineInfoDBName.MaterNG.ToString(), agvTask.agv_fromaddress, 0); plcClient.WriteValue(ConveyorLineInfoDBName.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(ConveyorLineInfoDBName.MaterOK.ToString(), agvTask.agv_toaddress, 0); } else { plcClient.WriteValue(ConveyorLineInfoDBName.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) { return; } } 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) { return; } } } } } agvtask_HtyRepository.AddTaskHistory(agvTask, OperateType.Cancel.ToString()); 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.Info("AGV更新任务状态").Write("WCS写入AGV任务号" + taskId + DateTime.Now + "任务异常状态确认1", "AGV更新任务状态"); agvtaskRepository.Delete(agvTask, true); } } //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.Info("AGV更新任务状态").Write("WCS接收AGV任务反馈异常" + ex.Message.ToString() + DateTime.Now, "AGV更新任务状态"); } finally { Interlocked.Exchange(ref _readUpdateAGVTaskSignalso, 0); } } } } }