| 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.Enums; | 
| using WIDESEAWCS_Core.Helper; | 
| using WIDESEAWCS_IBasicInfoRepository; | 
| using WIDESEAWCS_ITaskInfoRepository; | 
| using WIDESEAWCS_ITaskInfoService; | 
| using WIDESEAWCS_Model.Models; | 
| using WIDESEAWCS_QuartzJob; | 
| using WIDESEAWCS_Tasks; | 
|   | 
| namespace WIDESEAWCS_Server.Controllers | 
| { | 
|     [Route("api/[controller]")] | 
|     [ApiController] | 
|     public class ZH_AGVController : ControllerBase | 
|     { | 
|         private readonly IStationMangerRepository _stationMangerRepository; | 
|         private readonly ITaskService _taskService; | 
|         private readonly ITaskRepository _taskRepository; | 
|   | 
|         public ZH_AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository) | 
|         { | 
|             _stationMangerRepository = stationMangerRepository; | 
|             _taskService = taskService; | 
|             _taskRepository = taskRepository; | 
|         } | 
|   | 
|   | 
|   | 
|         /// <summary> | 
|         /// 安全信号申请 AGV-WCS | 
|         /// </summary> | 
|         /// <param name="secureApplyModel"></param> | 
|         /// <returns></returns> | 
|         [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 = TakeRequest(task.CurrentAddress); | 
|                     //if (!content.Status) | 
|                     //{ | 
|                     //    throw new Exception(content.Message); | 
|                     //} | 
|                 } | 
|                 else | 
|                 { | 
|                     var content = PutRequest(task.NextAddress, task.PalletType); | 
|                     //if (!content.Status) | 
|                     //{ | 
|                     //    throw new Exception(content.Message); | 
|                     //} | 
|                 } | 
|                 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); | 
|         } | 
|         /// <summary> | 
|         /// AGV任务更新/完成 | 
|         /// </summary> | 
|         /// <param name="agvUpdateModel"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, HttpGet, Route("AgvCallback"), AllowAnonymous] | 
|         public AgvResponseContent AgvUpdateTask([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); | 
|                 if (task == null && agvUpdateModel.Method!= "begin") throw new Exception($"未找到任务,任务号【{agvUpdateModel.TaskCode}】"); | 
|                 switch (agvUpdateModel.Method) | 
|                 { | 
|                     case "begin": | 
|                         WebResponseContent requestZH = _taskService.RequestWMSZHTask(agvUpdateModel.TaskCode, agvUpdateModel.PodCode,agvUpdateModel.PodTyp,agvUpdateModel.MaterialLot); | 
|                         agvResponseContent.Code = requestZH.Status ? "0" : "1"; | 
|                         agvResponseContent.Message = requestZH.Status ? "成功" : "失败"; | 
|                         return agvResponseContent; | 
|                     case "outbin"://出库根据这个信号判断取货完成 | 
|                         if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|                         { | 
|                             var content = TakeFinish(task.CurrentAddress); | 
|                             if (!content.Status) throw new Exception(content.Message); | 
|                             task.TaskState = TaskStatusEnum.AGV_Finish.ObjToInt(); | 
|                             var up = _taskRepository.DeleteAndMoveIntoHty(task, OperateTypeEnum.自动完成); | 
|                             _taskService.TaskCompleted(task.TaskNum); | 
|                             agvResponseContent.Code = up ? "0" : "1"; | 
|                             agvResponseContent.Message = up ? "成功" : "失败"; | 
|                             return agvResponseContent; | 
|                         } | 
|                         break; | 
|                     case "getSafetySignal"://安全信号申请 | 
|                         { | 
|                             if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|                             { | 
|                                 var content = TakeRequest(task.CurrentAddress); | 
|                                 //if (!content.Status) throw new Exception(content.Message); | 
|                             } | 
|                             else | 
|                             { | 
|                                 var content = PutRequest(task.NextAddress, task.PalletType); | 
|                                 //if (!content.Status) throw new Exception(content.Message); | 
|                             } | 
|                             task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt(); | 
|                             var up = _taskRepository.UpdateData(task); | 
|                             agvResponseContent.Code = up ? "0" : "1"; | 
|                             agvResponseContent.Message = up ? "成功" : "失败"; | 
|                             return agvResponseContent; | 
|                         } | 
|                     case "end"://入库根据这个信号判断放货完成 | 
|                         if (task.TaskType != TaskTypeEnum.Outbound.ObjToInt()) | 
|                         { | 
|                             var content = PutFinish(task.NextAddress); | 
|                             if (!content.Status) throw new Exception(content.Message); | 
|                             Dt_StationManger dt_Station = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == task.NextAddress); | 
|                             if (dt_Station == null) | 
|                             { | 
|                                 throw new Exception($"{task.NextAddress}站台未定义"); | 
|                             } | 
|                             task.CurrentAddress = dt_Station.StationCode; | 
|                             task.NextAddress = ""; | 
|                             task.TaskState = TaskStatusEnum.AGV_Finish.ObjToInt(); | 
|                             task.DeviceCode = "SC01_ZH"; | 
|                             var up = _taskRepository.UpdateData(task); | 
|                             agvResponseContent.Code = up ? "0" : "1"; | 
|                             agvResponseContent.Message = up ? "成功" : "失败"; | 
|                             return agvResponseContent; | 
|                         } | 
|                         break; | 
|                     case "cancel": | 
|                         task.TaskState = TaskStatusEnum.Cancel.ObjToInt(); | 
|                         _taskRepository.UpdateData(task); | 
|                         break; | 
|                     default: | 
|                         throw new Exception($"未定义方法名【{agvUpdateModel.Method}】"); | 
|                 } | 
|                 agvResponseContent.Code = "0"; | 
|                 agvResponseContent.Message = "成功"; | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 agvResponseContent.Code = "1"; | 
|                 agvResponseContent.Message = ex.Message; | 
|             } | 
|             return agvResponseContent; | 
|             //return _taskService.AgvUpdateTask(agvUpdateModel); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 放货请求 | 
|         /// </summary> | 
|         /// <param name="code"></param> | 
|         /// <param name="palletType"></param> | 
|         /// <returns></returns> | 
|         [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, bool>(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, bool>(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] | 
|         /// <summary> | 
|         /// 放货完成 | 
|         /// </summary> | 
|         /// <param name="code"></param> | 
|         /// <returns></returns> | 
|         [HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous] | 
|         public WebResponseContent PutFinish(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_PutFinish, true, stationManger.StationCode); | 
|                 return WebResponseContent.Instance.OK(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|   | 
|         //[HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous] | 
|         /// <summary> | 
|         /// 取货请求 | 
|         /// </summary> | 
|         /// <param name="code"></param> | 
|         /// <returns></returns> | 
|         [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, bool>(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, bool>(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] | 
|         /// <summary> | 
|         /// 取货完成 | 
|         /// </summary> | 
|         /// <param name="code"></param> | 
|         /// <returns></returns> | 
|         [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); | 
|             } | 
|         } | 
|   | 
|         //阻焊仓出库任务空储位异常手动重发AGV任务 | 
|         [HttpPost, HttpGet,Route("AgvZHTaskResend"),AllowAnonymous] | 
|         public WebResponseContent AgvZHTaskResend(int taskNum) | 
|         { | 
|             try | 
|             { | 
|                 Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum); | 
|                 if (task == null) | 
|                 { | 
|                     return WebResponseContent.Instance.Error("未找到相关任务信息"); | 
|                 } | 
|                 if (task.TaskState == TaskStatusEnum.Exception.ObjToInt() && task.DeviceCode == "AGV_ZH") | 
|                 { | 
|                     _taskService.UpdateTask(task, TaskStatusEnum.AGV_Execute); | 
|                 } | 
|                 else | 
|                 { | 
|                     return WebResponseContent.Instance.Error("任务设备编号不正确或任务状态非空储位异常"); | 
|                 } | 
|                 return WebResponseContent.Instance.OK(); | 
|             } | 
|             catch(Exception ex) | 
|             { | 
|                 return WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|         } | 
|     } | 
| } |