using HslCommunication; using Microsoft.EntityFrameworkCore; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using WIDESEA_Common; using WIDESEA_Common.DBHelper; using WIDESEA_Common.Tools; using WIDESEA_Core.EFDbContext; using WIDESEA_Entity.DomainModels; using WIDESEA_Services.IRepositories; using WIDESEA_Services.Repositories; using WIDESEA_WCS.EquipBaseInfo; using WIDESEA_WCS.WCSClient; namespace WIDESEA_WCS.SchedulerExecute.AGV { public partial class AGVSchedulerExecute { private static int _readSendAGVTaskSignalso = 0; private static bool isTrue1 = false; private static string name1 = ""; public static void SendAGVTask1(PLCClient plcClient) { try { if (plcClient == null) { return; } VOLContext context = new VOLContext(); Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context); Ibase_ware_locationRepository locationRepository = new base_ware_locationRepository(context); int TaskInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));//1收到 int TaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));//0初始/1下发/2取消/3更改 int resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString())); if (TaskInteractive == 0 && TaskInteractiveW == 0) { dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_taskstate == "Create" && r.agv_code == plcClient.PLCName && r.agv_tasktype.Contains("TaskType_Outbound")).OrderBy(r => r.agv_createtime).OrderByDescending(r => r.agv_grade).FirstOrDefault(); if (null != agvTask) { //写入任务信息 string rel = AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum); if (string.IsNullOrEmpty(rel)) { Thread.Sleep(500); for (int i = 0; i < 5; i++) { Thread.Sleep(200); string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString(); if (taskId != agvTask.agv_tasknum) { if (i < 4) AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum); else return; } else { break; } } //写入任务确认1 bool taskInteractiveW = plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1); for (int i = 0; i < 5; i++) { Thread.Sleep(200); object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()); if (readValue != null && readValue.ToString() == "1") { break; } else { plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1); } } } } } else if (TaskInteractive == 11 && TaskInteractiveW == 1) { plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0); for (int i = 0; i < 5; i++) { Thread.Sleep(200); object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()); if (readValue != null && readValue.ToString() == "0") { break; } else { plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0); } } } else if (1 == TaskInteractive && TaskInteractiveW == 1) { string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString(); var agvTask = agvtaskRepository.FindFirst(x => x.agv_tasknum == taskId); if (agvTask != null) { if (agvTask.agv_taskstate != AGVTaskStateEnum.SendOut.ToString()) agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString(); agvTask.agv_realesstime = DateTime.Now; agvtaskRepository.Update(agvTask, true); plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0); for (int i = 0; i < 5; i++) { Thread.Sleep(200); object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()); if (readValue != null && readValue.ToString() == "0") { break; } else { plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0); } } } } } catch (Exception ex) { //throw new Exception(ex.Message); WriteLog.Info($"{plcClient.PLCName}SendAGVTask").Write(ex.Message, $"{plcClient.PLCName}SendAGVTask"); } } private static void updateloction(dt_agvtask aTask, Ibase_ware_locationRepository locationRepository, Idt_agvtaskRepository agvtaskRepository) { if (aTask != null) { if (aTask.agv_toaddress.Contains("HCJ") && aTask.agv_fromaddress.Contains("SB")) { if (aTask.agv_remark.Contains("辊分下料任务")) { if (aTask.agv_toaddress.Contains("ZJ-GFHCJ10-")) { string NUM = aTask.agv_toaddress.Remove(11) + "4"; base_ware_location location = locationRepository.FindFirst(x => x.upper_code == NUM); if (location != null) { string lotionCode = aTask.agv_toaddress.Remove(11); dt_agvtask Agvtask = agvtaskRepository.FindFirst(d => d.agv_tasknum != aTask.agv_tasknum && d.agv_toaddress.Contains(lotionCode)); if (Agvtask == null) { location.logic_col = 0; location.logic_row = "0"; location.task = 2; locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true); } } } else { string NUM = aTask.agv_toaddress.Remove(10) + "4"; base_ware_location location = locationRepository.FindFirst(x => x.upper_code == NUM); if (location != null) { string lotionCode = aTask.agv_toaddress.Remove(10); dt_agvtask Agvtask = agvtaskRepository.FindFirst(d => d.agv_tasknum != aTask.agv_tasknum && d.agv_toaddress.Contains(lotionCode)); if (Agvtask == null) { location.logic_col = 0; location.logic_row = "0"; location.task = 2; locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true); } } } } else { base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_toaddress); location.logic_col = 0; location.logic_row = "0"; location.task = 2; locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true); } } else if (aTask.agv_toaddress.Contains("SB") && aTask.agv_fromaddress.Contains("HCJ")) { base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_fromaddress); location.logic_col = 0; location.logic_row = "0"; location.task = 2; locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true); } else if (aTask.agv_toaddress.Contains("HCJ") && aTask.agv_fromaddress.Contains("HCJ")) { base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_toaddress); location.logic_col = 0; location.logic_row = "0"; location.task = 2; locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true); base_ware_location location1 = locationRepository.FindFirst(x => x.upper_code == aTask.agv_fromaddress); location1.logic_col = 0; location1.logic_row = "0"; location1.task = 2; var entry = locationRepository.DbContext.ChangeTracker.Entries().FirstOrDefault(); if (entry != null) { entry.State = EntityState.Detached; } int a = locationRepository.Update(location1, x => new { x.logic_col, x.logic_row, x.task }, true); if (a != 1) { ////WriteLog.Info("AGV任务反馈5").Write($"任务号:{aTask.agv_tasknum},起点地址:{aTask.agv_fromaddress},修改失败:{a},时间: {DateTime.Now}", "AGV任务反馈5"); } } } } } }