using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using WIDESEA_DTO.Agv; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.Enums; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.Agv; using WIDESEAWCS_IBasicInfoRepository; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_Tasks; using WIDESEAWCS_Tasks.ConveyorLineJob; using static Dm.net.buffer.ByteArrayBuffer; namespace WIDESEAWCS_Server.Controllers { [Route("api/[controller]")] [ApiController] public class CTU_AGVController : ControllerBase { private readonly IStationMangerRepository _stationMangerRepository; private readonly ITaskService _taskService; private readonly ITaskRepository _taskRepository; private readonly IUnitOfWorkManage _unitOfWorkManage; public CTU_AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository,IUnitOfWorkManage unitOfWorkManage) { _stationMangerRepository = stationMangerRepository; _taskService = taskService; _taskRepository = taskRepository; _unitOfWorkManage = unitOfWorkManage; } [HttpPost, HttpGet, Route("AGVFinish"), AllowAnonymous] public WebResponseContent AGVFinish(string barcode) { WebResponseContent content = new WebResponseContent(); try { var task = _taskRepository.QueryFirst(x => x.PalletCode == barcode); if (task == null) throw new Exception($"未找到任务,托盘号【{barcode}】"); AgvUpdateDTO updateDTO = new AgvUpdateDTO() { TaskCode = task.AgvTaskNum, Method = "end" }; var agvResponseContent = CtuCallback(updateDTO); if (agvResponseContent.Code == "1") throw new Exception(agvResponseContent.Message); content.OK(); } catch (Exception ex) { content.Error(ex.Message); } return content; } #region 安全信号申请 /// /// 安全信号申请 AGV-WCS /// /// /// [HttpPost, HttpGet, Route("AgvSecureApply"), AllowAnonymous] public AgvResponseContent AgvSecureApply([FromBody] AgvSecureApplyDTO secureApplyModel) { AgvResponseContent agvResponseContent = new AgvResponseContent(); agvResponseContent.ReqCode = secureApplyModel.ReqCode; try { var task = _taskRepository.QueryFirst(x => secureApplyModel.TaskCode == x.AgvTaskNum); if (task == null) throw new Exception("未找到任务"); if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt()) { var content = PutRequest(task.NextAddress, task.PalletType); } else { var content = TakeRequest(task.CurrentAddress); } task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt(); var up = _taskRepository.UpdateData(task); agvResponseContent.Code = up ? "0" : "1"; agvResponseContent.Message = up ? "成功" : "失败"; } catch (Exception ex) { agvResponseContent.Code = "1"; agvResponseContent.Message = ex.Message; } return agvResponseContent; //return _taskService.AgvSecureApply(secureApplyModel); } #endregion /// /// 料箱判断 /// /// [HttpPost, HttpGet, Route("AgvCallbackBoxNo"), AllowAnonymous] public AgvResponseContent AgvCallbackBoxNo([FromBody] AgvCallbackBoxNoDTO agvCallbackBox) { AgvResponseContent agvResponseContent = new AgvResponseContent(); try { var task = _taskRepository.QueryFirst(x => agvCallbackBox.TaskNo == x.AgvTaskNum); if (task==null) { agvResponseContent.Code = "1"; agvResponseContent.Message = $"未找到任务{agvCallbackBox.TaskNo}"; return agvResponseContent; } if (task.PalletCode!= agvCallbackBox.ContainerCode) { string Address = task?.CurrentAddress switch { "5206" => "5105", "5212" => "5111", "5218" => "5117", "8005" => "8001", "5135" => "5236", _ => throw new Exception("未找到地址信息"), }; AgvCTUCancelDTO agvCTUCancel=new AgvCTUCancelDTO() { ForceCancel="1", MatterArea= task.CurrentAddress, TaskCode=task.AgvTaskNum, ReqCode = DateTime.Now.ToString("yyyyMMddHHmmss") + task.AgvTaskNum, ReqTime= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), }; WebResponseContent content = _taskService.AgvBoxApplyPass(agvCTUCancel); //取消任务 if (content.Status) { agvResponseContent.Code = "0"; agvResponseContent.Message = $"取消{task.AgvTaskNum}成功"; task.ExceptionMessage = $"实际箱号{task.PalletCode}扫描箱{agvCallbackBox.ContainerCode}"; _taskService.UpdateTask(task, TaskStatusEnum.Cancel); } else { agvResponseContent.Code = "1"; agvResponseContent.Message = content.Message; } } else { agvResponseContent.Code = "0"; agvResponseContent.Message = "成功"; } } catch (Exception ex) { agvResponseContent.Code = "1"; agvResponseContent.Message = ex.Message; } return agvResponseContent; } /// /// CtuAGV任务更新/完成 /// /// /// [HttpPost, HttpGet, Route("CtuCallback"), AllowAnonymous] public AgvResponseContent CtuCallback([FromBody] AgvUpdateDTO agvUpdateModel) { AgvResponseContent agvResponseContent = new AgvResponseContent(); try { if (agvUpdateModel == null) throw new Exception("未获取到请求参数"); agvResponseContent.ReqCode = agvUpdateModel.ReqCode; var task = _taskRepository.QueryFirst(x => agvUpdateModel.TaskCode == x.AgvTaskNum) ?? throw new Exception($"未找到任务,任务号【{agvUpdateModel.TaskCode}】"); switch (agvUpdateModel.Method.ToUpper()) { case "END": if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.OutEmpty.ObjToInt() || task.TaskType == TaskTypeEnum.OutAllocate.ObjToInt() || task.TaskType == TaskTypeEnum.OutProduct.ObjToInt()) PutFinish(task.NextAddress); _taskService.TaskCompleted(task.TaskNum); break; case "APPLYTOAGV": //更改成品入库AGV取货任务状态 if (task.TaskType == TaskTypeEnum.InProduct.ObjToInt() || task.TaskType == TaskTypeEnum.InProductBack.ObjToInt()) { //是否存在输送线体待AGV搬运任务 var taskExecutes = _taskRepository.QueryData(x => x.NextAddress == task.CurrentAddress && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.TaskType == TaskTypeEnum.InProduct.ObjToInt()); if (taskExecutes.Count > 0) { string address = task.CurrentAddress switch { "5206" => "5105", "5212" => "5111", "5218" => "5117" }; //调用AGV预调度接口 AgvScheduleTaskDTO agvScheduleTask = new AgvScheduleTaskDTO() { PositionCode = address, NextTask = "90", UseableLayers = "1", CacheCount = "1", Update = "0", AgvTyp = "11", PreTaskQty = "1", ReqCode = Guid.NewGuid().ToString().Replace("-", ""), ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), }; WebResponseContent content = _taskService.AgvPreScheduleTask(agvScheduleTask); if (!content.Status) { agvResponseContent.Code = "1"; agvResponseContent.Message = content.Message; return agvResponseContent; } } Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.CurrentAddress); if (stationManger==null) { agvResponseContent.Code = "1"; agvResponseContent.Message = "未找到站台"; return agvResponseContent; } IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); if (device == null) { agvResponseContent.Code = "1"; agvResponseContent.Message = $"未找设备{stationManger.StationDeviceCode}"; return agvResponseContent; } OtherDevice otherDevice = (OtherDevice)device; short canTake = otherDevice.GetValue(GroundStationDBName.R_IsCanTake, stationManger.StationCode); if (canTake == 1) { //获取调入参数 AGVBoxApplyPassDTO boxApplyPassDTO = new AGVBoxApplyPassDTO() { ReqCode = Guid.NewGuid().ToString().Replace("-", ""), ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TaskCode = task.AgvTaskNum }; if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) { boxApplyPassDTO.Type = "1"; } else { boxApplyPassDTO.Type = "2"; } //请求料箱回调接口 WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO); if (content.Status && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) { _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); } else { task.ExceptionMessage = content.Message; _taskService.UpdateTask(task, TaskStatusEnum.Exception); } } else { task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt(); task.Remark = task.TaskState.ToString(); _taskRepository.UpdateData(task); } } else //更改辅料取货 { //是否存在输送线体待AGV搬运任务 var taskExecutesFL = _taskRepository.QueryData(x => x.TaskState == TaskStatusEnum.New.ObjToInt() && x.DeviceCode == "AGV_FL" && !string.IsNullOrEmpty(x.DeviceCode)); if (taskExecutesFL.Count > 0) { //调用AGV预调度接口 AgvScheduleTaskDTO agvScheduleTask = new AgvScheduleTaskDTO() { PositionCode = "8001", NextTask = "60", UseableLayers = "1", CacheCount = "1", Update = "0", AgvTyp = "10", PreTaskQty = "1", ReqCode = Guid.NewGuid().ToString().Replace("-", ""), ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), }; WebResponseContent contentFL = _taskService.AgvPreScheduleTask(agvScheduleTask); if (!contentFL.Status) { agvResponseContent.Code = "1"; agvResponseContent.Message = contentFL.Message; return agvResponseContent; } } else //线体无任务 { //调用AGV预调度接口 AgvScheduleTaskDTO agvScheduleTask = new AgvScheduleTaskDTO() { PositionCode = "8001", NextTask = "15", UseableLayers = "1", CacheCount = "1", Update = "0", AgvTyp = "10", PreTaskQty = "1", ReqCode = Guid.NewGuid().ToString().Replace("-", ""), ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), }; WebResponseContent contentFL = _taskService.AgvPreScheduleTask(agvScheduleTask); if (!contentFL.Status) { agvResponseContent.Code = "1"; agvResponseContent.Message = contentFL.Message; return agvResponseContent; } } //获取调入参数 AGVBoxApplyPassDTO boxApplyPassDTO = new AGVBoxApplyPassDTO() { ReqCode = Guid.NewGuid().ToString().Replace("-", ""), ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TaskCode = task.AgvTaskNum }; boxApplyPassDTO.Type = "1"; //请求料箱回调接口 WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO); if (content.Status && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) { _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); } else { task.ExceptionMessage = content.Message; _taskService.UpdateTask(task, TaskStatusEnum.Exception); } } break; case "APPLYFROMAGV": //更改成品出AGV放货任务状态 //if (task.TaskType == TaskTypeEnum.OutProduct.ObjToInt()) //{ _taskService.UpdateTask(task, TaskStatusEnum.AGV_WaitToExecute); //} break; case "OUTBIN": if (task.TaskType == TaskTypeEnum.InProduct.ObjToInt() || task.TaskType == TaskTypeEnum.InProductBack.ObjToInt()) { TakeFinish(task.CurrentAddress); //_taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); } else if(task.TaskType == TaskTypeEnum.OutProduct.ObjToInt()) { _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); } break; default: throw new Exception($"方法{agvUpdateModel.Method}参数不存在"); } agvResponseContent.Code = "0"; agvResponseContent.Message = "成功"; } catch (Exception ex) { agvResponseContent.Code = "1"; agvResponseContent.Message = ex.Message; } return agvResponseContent; } /// /// 放货请求 /// /// /// /// [HttpPost, HttpGet, Route("PutRequest"), AllowAnonymous] public WebResponseContent PutRequest(string code, int palletType) { try { Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code); if (stationManger == null) { return WebResponseContent.Instance.Error($"未找到站台配置"); } IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); if (device == null) { return WebResponseContent.Instance.Error($"未找到对应设备"); } OtherDevice otherDevice = (OtherDevice)device; bool canPut = otherDevice.GetValue(GroundStationDBName.R_IsCanPut, stationManger.StationCode); if (canPut) { otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode); return WebResponseContent.Instance.OK(); } else { otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode); Thread.Sleep(1000); canPut = otherDevice.GetValue(GroundStationDBName.R_IsCanPut, stationManger.StationCode); if (canPut) { return WebResponseContent.Instance.OK(); } else { return WebResponseContent.Instance.Error($"放货申请中"); } } } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } //[HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous] /// /// 放货完成 /// /// /// [HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous] public WebResponseContent PutFinish(string code,string barCode="",string taskNum="") { try { Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code); if (stationManger == null) { return WebResponseContent.Instance.Error($"未找到站台配置"); } IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); if (device == null) { return WebResponseContent.Instance.Error($"未找到对应设备"); } OtherDevice otherDevice = (OtherDevice)device; if (!barCode.IsNullOrEmpty() && !taskNum.IsNullOrEmpty()) { otherDevice.SetValue(W_ConveyorLineFLDB.Barcode, barCode, stationManger.StationCode); Thread.Sleep(500); otherDevice.SetValue(W_ConveyorLineFLDB.TaskNum, taskNum, stationManger.StationCode); Thread.Sleep(500); } otherDevice.SetValue(GroundStationDBName.W_PutFinish, true, stationManger.StationCode); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } //[HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous] /// /// 取货请求 /// /// /// [HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous] public WebResponseContent TakeRequest(string code) { try { Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code); if (stationManger == null) { return WebResponseContent.Instance.Error($"未找到站台配置"); } IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); if (device == null) { return WebResponseContent.Instance.Error($"未找到对应设备"); } OtherDevice otherDevice = (OtherDevice)device; bool canPut = otherDevice.GetValue(GroundStationDBName.R_IsCanTake, stationManger.StationCode); if (canPut) { otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode); return WebResponseContent.Instance.OK(); } else { otherDevice.SetValue(GroundStationDBName.W_TakeRequest, true, stationManger.StationCode); Thread.Sleep(1000); canPut = otherDevice.GetValue(GroundStationDBName.R_IsCanTake, stationManger.StationCode); if (canPut) { return WebResponseContent.Instance.OK(); } else { return WebResponseContent.Instance.Error($"取货申请中"); } } } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } //[HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous] /// /// 取货完成 /// /// /// [HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous] public WebResponseContent TakeFinish(string code) { try { Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == code); if (stationManger == null) { return WebResponseContent.Instance.Error($"未找到站台配置"); } IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); if (device == null) { return WebResponseContent.Instance.Error($"未找到对应设备"); } OtherDevice otherDevice = (OtherDevice)device; otherDevice.SetValue(GroundStationDBName.W_TakeFinish, true, stationManger.StationCode); Thread.Sleep(1000); otherDevice.SetValue(GroundStationDBName.W_TakeFinish, false, stationManger.StationCode); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } } }