wangxinhui
2025-11-30 f2b85c65234e0dcdd3fcce4dafbe16933b7f1b48
ÏîÄ¿´úÂë/WCSServices/WIDESEAWCS_Tasks/AGV/AGVExtend.cs
@@ -22,191 +22,137 @@
        {
            try
            {
                var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()|| x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.DeviceCode=="AGV").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                if (newTasks.Count>0)
                var newTasksOut = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType==TaskTypeEnum.Outbound.ObjToInt() && x.DeviceCode == "AGV").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                var newTasksIn = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType == TaskTypeEnum.Inbound.ObjToInt() && x.DeviceCode == "AGV").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                #region å‡ºåº“任务下发
                if (newTasksOut.Count>0)
                {
                    foreach (var task in newTasks)
                    List<string> GroupIds = newTasksOut.GroupBy(x => x.GroupId).Select(x => x.Key).ToList();
                    foreach (var GroupId in GroupIds)
                    {
                        var tasks = newTasksOut.Where(x => x.GroupId == GroupId).ToList();
                        try
                        {
                            Guid guid = Guid.NewGuid();
                            AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO()
                            {
                                SysToken=guid.ToString().Replace("-",""),
                                ReceiveTaskID=task.TaskNum.ToString(),
                                MapCode="01",
                                Variables=new List<PointDetail>()
                                MissionData = new List<MissionDataItem>()
                            };
                            if (task.NextAddress== "一楼月台码头")
                            foreach (var task in tasks)
                            {
                                string? address =_taskService.RequestTargetAddress(task.TaskNum);
                                if (address.IsNullOrEmpty())
                                //获取目标点货位
                                Dt_LocationInfo locationInfoStart = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.CurrentAddress);
                                //获取拣选出库站台
                                Dt_StationManger stationMangerEnd = _stationMangerRepository.QueryFirst(x => x.PickStationCode == task.NextAddress);
                                if (locationInfoStart == null || stationMangerEnd == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息");
                                agvTaskSend.RequestId = GroupId;
                                agvTaskSend.MissionCode = GroupId;
                                if (task.Grade == 0)
                                {
                                    task.ExceptionMessage = "未找到AGV站点";
                                    continue;
                                }
                                task.NextAddress = address;
                            }
                            Dt_StationManger stationMangerStart = _stationMangerRepository.QueryFirst(x=>x.StationCode==task.CurrentAddress);
                            Dt_StationManger stationMangerEnd = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress);
                            //添加任务路径及高度
                            if (stationMangerStart !=null && stationMangerEnd != null)
                            {
                                PointDetail pointDetail1 = new PointDetail()
                                {
                                    Code= "GoodPoint",
                                    Value=stationMangerStart.AGVStationCode
                                };
                                //判断是否有前置点
                                if (!string.IsNullOrEmpty(stationMangerStart.AGVFrontCode))
                                {
                                    PointDetail pointDetail2 = new PointDetail()
                                    {
                                        Code = "CostPoint",
                                        Value = stationMangerStart.AGVFrontCode
                                    };
                                    agvTaskSend.Variables.Add(pointDetail2);
                                }
                                PointDetail pointDetail3 = new PointDetail()
                                {
                                    Code = "PointB",
                                    Value = stationMangerEnd.AGVStationCode
                                };
                                //判断是否有前置点
                                if (!string.IsNullOrEmpty(stationMangerEnd.AGVFrontCode))
                                {
                                    PointDetail pointDetail4 = new PointDetail()
                                    {
                                        Code = "PointA",
                                        Value = stationMangerEnd.AGVFrontCode
                                    };
                                    agvTaskSend.Variables.Add(pointDetail4);
                                }
                                PointDetail pointDetail5 = new PointDetail()
                                {
                                    Code = "QUQTH",
                                    Value = stationMangerStart.AGVStationHeight.ToString()
                                };
                                PointDetail pointDetail6 = new PointDetail()
                                {
                                    Code = "FHMH",
                                    Value = stationMangerEnd.AGVStationHeight.ToString()
                                };
                                agvTaskSend.Variables.Add(pointDetail1);
                                agvTaskSend.Variables.Add(pointDetail3);
                                agvTaskSend.Variables.Add(pointDetail5);
                                agvTaskSend.Variables.Add(pointDetail6);
                            }
                            else
                            {
                                throw new Exception("未找到AGV站点");
                            }
                            //取放任务模版
                            if (stationMangerStart.StationCode.Contains("PNT") && !stationMangerEnd.StationCode.Contains("PNT") && stationMangerEnd.IsManual==0)//请求放
                            {
                                agvTaskSend.TaskCode = "DUIGAO1";
                                if (stationMangerStart.StationType == 998 || stationMangerEnd.StationType == 998)//潜伏顶升车
                                {
                                    agvTaskSend.TaskCode = "DUOLUN1";
                                }
                            }
                            else if (!stationMangerStart.StationCode.Contains("PNT") && stationMangerEnd.StationCode.Contains("PNT") && stationMangerStart.IsManual == 0)//请求取
                            {
                                agvTaskSend.TaskCode = "DUIGAO2";
                                if (stationMangerStart.StationType==998 || stationMangerEnd.StationType == 998)//潜伏顶升车
                                {
                                    agvTaskSend.TaskCode = "DUOLUN2";
                                }
                            }
                            else if (!stationMangerStart.StationCode.Contains("PNT") && !stationMangerEnd.StationCode.Contains("PNT") && stationMangerStart.IsManual == 0 && stationMangerStart.IsManual == 0)//请求取放
                            {
                                agvTaskSend.TaskCode = "DUIGAO3";
                                if (stationMangerStart.StationType == 998 || stationMangerEnd.StationType == 998)//潜伏顶升车
                                {
                                    agvTaskSend.TaskCode = "DUOLUN3";
                                }
                            }
                            else//无请求
                            {
                                agvTaskSend.TaskCode = "DUIGAO4";
                                if (stationMangerStart.StationType == 998 || stationMangerEnd.StationType == 998)//潜伏顶升车
                                {
                                    agvTaskSend.TaskCode = "DUOLUN4";
                                }
                            }
                            if (stationMangerStart.StationType == 998 || stationMangerEnd.StationType == 998)//潜伏顶升车
                            {
                                agvTaskSend.Variables.FirstOrDefault(x => x.Code == "FHMH").Value = "0";
                                agvTaskSend.Variables.FirstOrDefault(x => x.Code == "GoodPoint").Value = "";
                                //判断人工手动
                                PointDetail complete = new();
                                if (stationMangerStart.IsManual==1 && stationMangerEnd.IsManual==0)
                                {
                                    complete = new()
                                    {
                                        Code = "Complete",
                                        Value = "1"
                                    };
                                }
                                else if (stationMangerStart.IsManual == 0 && stationMangerEnd.IsManual == 1)
                                {
                                    complete = new()
                                    {
                                        Code = "Complete",
                                        Value = "2"
                                    };
                                }
                                else if (stationMangerStart.IsManual == 1 && stationMangerEnd.IsManual == 1)
                                {
                                    complete = new()
                                    {
                                        Code = "Complete",
                                        Value = "3"
                                    };
                                    agvTaskSend.Priority = 99;
                                }
                                else
                                {
                                    complete = new()
                                    {
                                        Code = "Complete",
                                        Value = "0"
                                    };
                                    agvTaskSend.Priority = 99 - task.Grade;
                                }
                                //下发任务AGV任务集群
                                agvTaskSend.AgvGroupCode = "QianF";
                                PointDetail pointDetail2 = new PointDetail()
                                //料箱子搬运任务
                                MissionDataItem missionDataItem = new MissionDataItem()
                                {
                                    Code = "CostPoint",
                                    Value = stationMangerStart.AGVStationCode
                                    Sequence = task.TaskNum,
                                    BinCode = task.PalletCode,
                                    StartPosition = locationInfoStart.AgvPoint,
                                    StartSlotCode = locationInfoStart.LocationCode,
                                    EndPosition = stationMangerEnd.StationCode,
                                    EndSlotCode = stationMangerEnd.CraneStationCode,
                                    TakeActionConfirm = false,
                                    TakeActionInform = false,
                                    PutActionConfirm = true,
                                    PutActionInform = true,
                                };
                                agvTaskSend.Variables.Add(pointDetail2);
                                agvTaskSend.Variables.Add(complete);
                            }
                            if (task.TaskType==TaskTypeEnum.OutCarton.ObjToInt())
                            {
                                agvTaskSend.AgvGroupCode = "GAOc";
                                agvTaskSend.MissionData.Add(missionDataItem);
                            }
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            task.Dispatchertime = DateTime.Now;
                            _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                            tasks.ForEach(x =>
                            {
                                x.Dispatchertime = DateTime.Now;
                                x.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                            });
                            _taskService.UpdateData(tasks);
                            Thread.Sleep(500);
                        }
                        catch (Exception ex)
                        {
                            tasks.ForEach(x =>
                            {
                                x.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                x.ExceptionMessage = ex.Message;
                            });
                            _taskService.UpdateData(tasks);
                        }
                    }
                }
                #endregion
                #region å…¥åº“任务下发
                if (newTasksIn.Count > 0)
                {
                    foreach (var task in newTasksIn)
                    {
                        try
                        {
                            AgvTaskSendDTO agvTaskSend = new AgvTaskSendDTO()
                            {
                                MissionData = new List<MissionDataItem>()
                            };
                            //获取目标点货位
                            Dt_LocationInfo locationInfoEnd = _locationInfoRepository.QueryFirst(x => x.LocationCode == task.NextAddress);
                            //获取线体入库站台
                            Dt_StationManger stationMangerStart = _stationMangerRepository.QueryFirst(x => x.PickStationCode == task.CurrentAddress);
                            if (locationInfoEnd == null || stationMangerStart == null) throw new Exception($"未找到任务号${task.TaskNum}起始点{task.CurrentAddress}或目标点{task.NextAddress}位置信息");
                            agvTaskSend.RequestId = Guid.NewGuid().ToString().Replace("-", "");
                            agvTaskSend.MissionCode = task.TaskNum.ToString();
                            if (task.Grade == 0)
                            {
                                agvTaskSend.Priority = 99;
                            }
                            else
                            {
                                agvTaskSend.Priority = 99 - task.Grade;
                            }
                            //料箱子搬运任务
                            MissionDataItem missionDataItem = new MissionDataItem()
                            {
                                Sequence = task.TaskNum,
                                BinCode = task.PalletCode,
                                StartPosition = stationMangerStart.StationCode,
                                StartSlotCode = stationMangerStart.CraneStationCode,
                                EndPosition = locationInfoEnd.AgvPoint,
                                EndSlotCode = locationInfoEnd.LocationCode,
                                TakeActionConfirm = false,
                                TakeActionInform = false,
                                PutActionConfirm = true,
                                PutActionInform = true,
                            };
                            agvTaskSend.MissionData.Add(missionDataItem);
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(agvTaskSend, APIEnum.AgvSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            task.Dispatchertime = DateTime.Now; task.TaskState = TaskStatusEnum.AGV_Executing.ObjToInt();
                            _taskService.UpdateData(task);
                            Thread.Sleep(500);
                        }
                        catch (Exception ex)
                        {
                            task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                            task.ExceptionMessage = ex.Message;
                            _taskService.UpdateData(task);
                        }
                    }
                    //更新任务
                    if (newTasks.Count > 0)
                    {
                        _taskService.UpdateData(newTasks);
                    }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {