wangxinhui
2025-04-29 95e39ae7aecd6e1016c71cf5ae70a680d8f569bb
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/³ÉÆ·²Ö/AGV_CPExtend.cs
@@ -21,51 +21,115 @@
        {
            try
            {
                var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()|| x.TaskState == TaskStatusEnum.New.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var task in newTasks)
                {
                    try
                    {
                        if (task.TaskType == TaskTypeEnum.OutProduct.ObjToInt())
                            task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                        else
                            task.NextAddress = GetAGVAddress(task.NextAddress);
                        AgvTaskDTO taskDTO = new AgvTaskDTO()
                        {
                            TaskCode = task.AgvTaskNum,
                            ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum,
                            TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR",
                            ctnrCode = task.PalletCode,
                            PositionCodePath = new List<CodePath>()
                        {
                            new CodePath()
                            {
                                type="05",
                                positionCode=task.CurrentAddress
                            },
                            new CodePath()
                            {
                                type="05",
                                positionCode=task.NextAddress
                            }
                        },
                        };
                        //发送AGV任务
                        WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                        if (!content.Status)
                            throw new Exception(content.Message);
                        task.TaskState = TaskStatusEnum.AGV_Takeing.ObjToInt();
                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                    }
                    catch (Exception ex)
                    {
                        task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                        task.ExceptionMessage = ex.Message;
                    }
                }
                var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()|| x.TaskState == TaskStatusEnum.New.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.TaskType!=999).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                if (newTasks.Count>0)
                {
                    _taskService.UpdateData(newTasks);
                    foreach (var task in newTasks)
                    {
                        try
                        {
                            if (task.TaskType == TaskTypeEnum.OutProduct.ObjToInt())
                                task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                            else
                                task.NextAddress = GetAGVAddress(task.NextAddress);
                            AgvTaskDTO taskDTO = new AgvTaskDTO()
                            {
                                TaskCode = task.AgvTaskNum,
                                ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum,
                                TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR",
                                ctnrCode = task.PalletCode,
                                PositionCodePath = new List<CodePath>()
                                {
                                    new CodePath()
                                    {
                                        type = "05",
                                        positionCode = task.CurrentAddress
                                    },
                                    new CodePath()
                                    {
                                        type = "05",
                                        positionCode = task.NextAddress
                                    }
                                }
                            };
                            if (taskDTO.TaskTyp== "CPC")
                            {
                                taskDTO.Priority = (24 - DateTime.Now.Hour).ToString();
                            }
                            if (task.GroupId.IsNotEmptyOrNull())
                            {
                                taskDTO.GroupId=task.GroupId;
                            }
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            if (taskDTO.TaskTyp == "CPC")
                            {
                                _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                            }
                            else
                            {
                                _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                            }
                            Thread.Sleep(500);
                        }
                        catch (Exception ex)
                        {
                            task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                            task.ExceptionMessage = ex.Message;
                        }
                    }
                    if (newTasks.Count > 0)
                    {
                        _taskService.UpdateData(newTasks);
                    }
                }
                var newTasksLocations = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.New.ObjToInt() && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.Remark == "测试空框").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                if (newTasksLocations.Count>0)
                {
                    foreach (var task in newTasksLocations)
                    {
                        try
                        {
                            string CurrentAddress = GetAGVAddress(task.CurrentAddress);
                            string NextAddress = GetAGVAddress(task.NextAddress);
                            AgvTaskDTO taskDTO = new AgvTaskDTO()
                            {
                                TaskCode = task.AgvTaskNum,
                                ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum,
                                TaskTyp = "CP",
                                ctnrCode = task.PalletCode,
                                PositionCodePath = new List<CodePath>()
                                {
                                    new CodePath()
                                    {
                                        type="05",
                                        positionCode=CurrentAddress
                                    },
                                    new CodePath()
                                    {
                                        type="05",
                                        positionCode=NextAddress
                                    }
                                },
                            };
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            task.TaskState = TaskStatusEnum.AGV_Takeing.ObjToInt();
                            task.CurrentAddress = CurrentAddress;
                            task.NextAddress = NextAddress;
                            _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                        }
                        catch (Exception ex)
                        {
                            task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                            task.ExceptionMessage = ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
@@ -81,27 +145,27 @@
            try
            {
                //获取是否有安全申请中的任务
                var continueTask = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).FirstOrDefault();
                if (continueTask!=null)
                var continueTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                foreach (var continueTask in continueTasks)
                {
                    //取货回调
                    if (continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.CurrentAddress || x.StationCode== continueTask.CurrentAddress);
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.CurrentAddress || x.StationCode == continueTask.CurrentAddress);
                        if (stationManger == null)
                        {
                            throw new Exception($"未找到站台{continueTask.CurrentAddress}信息");
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            throw new Exception($"未找到设备{stationManger.StationDeviceCode}信息");
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        short canTake = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanTake, stationManger.StationCode);
                        if (canTake != 1)
                        {
                            return;
                            continue;
                        }
                    }
                    else//放货回调
@@ -109,18 +173,18 @@
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.NextAddress);
                        if (stationManger == null)
                        {
                            throw new Exception($"未找到站台{continueTask.NextAddress}信息");
                            continue;
                        }
                        IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                        if (device == null)
                        {
                            throw new Exception($"未找到设备{stationManger.StationDeviceCode}信息");
                            continue;
                        }
                        OtherDevice otherDevice = (OtherDevice)device;
                        short canPut = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                        if (canPut != 1)
                        {
                            return;
                            continue;
                        }
                    }
                    //获取调入参数
@@ -140,23 +204,21 @@
                    }
                    //请求料箱回调接口
                    WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO);
                    if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup && boxApplyPassDTO.TaskCode == continueTask.AgvTaskNum)
                    {
                        continueTask.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Executing);
                    }
                    else if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                    else if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && boxApplyPassDTO.TaskCode== continueTask.AgvTaskNum)
                    {
                        continueTask.TaskState = TaskStatusEnum.AGV_Puting.ObjToInt();
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Puting);
                    }
                    else
                    {
                        continueTask.TaskState = TaskStatusEnum.Exception.ObjToInt();
                        continueTask.ExceptionMessage = content.Message;
                        _taskService.UpdateTask(continueTask, TaskStatusEnum.Exception);
                    }
                }
            }
            catch (Exception ex)
            {