wangxinhui
23 小时以前 2c889b38311883ceb703c4e0034c798fdaa2e21c
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/³ÉÆ·²Ö/AGV_CPExtend.cs
@@ -21,122 +21,385 @@
        {
            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) && x.TaskType!=999).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList();
                if (newTasks.Count>0)
                var AllTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (nameof(AGV_CPJob).Contains(x.DeviceCode))).ToList();
                var stationMangers = _stationMangerRepository.QueryData();
                #region å…¥åº“任务推送
                {
                    foreach (var task in newTasks)
                    var newTasksIn = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()) && x.TaskType>=TaskTypeEnum.Inbound.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 (newTasksIn.Count>0)
                    {
                        try
                        foreach (var task in newTasksIn)
                        {
                            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                                task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                            else
                                task.NextAddress = GetAGVAddress(task.NextAddress);
                            AgvTaskDTO taskDTO = new AgvTaskDTO()
                            try
                            {
                                TaskCode = task.AgvTaskNum,
                                ReqCode = DateTime.Now.ToString("yyMMdd") + task.AgvTaskNum,
                                TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR",
                                ctnrCode = task.PalletCode,
                                PositionCodePath = new List<CodePath>()
                                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                                    task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                                else
                                    task.NextAddress = GetAGVAddress(task.NextAddress);
                                AgvTaskDTO taskDTO = new AgvTaskDTO()
                                {
                                    new CodePath()
                                    TaskCode = task.AgvTaskNum,
                                    ReqCode = DateTime.Now.ToString("yyMMdd") + task.AgvTaskNum,
                                    TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR",
                                    ctnrCode = task.PalletCode,
                                    PositionCodePath = new List<CodePath>()
                                    {
                                        type = "05",
                                        positionCode = task.CurrentAddress
                                    },
                                    new CodePath()
                                        new CodePath()
                                        {
                                            type = "05",
                                            positionCode = task.CurrentAddress
                                        },
                                        new CodePath()
                                        {
                                            type = "05",
                                            positionCode = task.NextAddress
                                        }
                                    }
                                };
                                if (taskDTO.TaskTyp== "CPC")
                                {
                                    taskDTO.Priority = task.Grade.ToString();
                                }
                                if (task.GroupId.IsNotEmptyOrNull())
                                {
                                    taskDTO.GroupId=task.GroupId;
                                }
                                if (task.TaskType==TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                                {
                                    taskDTO.Priority = "127";
                                }
                                //发送AGV任务
                                WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                                if (!content.Status)
                                    throw new Exception(content.Message);
                                if (taskDTO.TaskTyp == "CPC")
                                {
                                    task.Dispatchertime = DateTime.Now;
                                    _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                                }
                                else
                                {
                                    task.Dispatchertime = DateTime.Now;
                                    _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                                }
                                Thread.Sleep(500);
                            }
                            catch (Exception ex)
                            {
                                task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                task.ExceptionMessage = ex.Message;
                            }
                        }
                        if (newTasksIn.Count > 0)
                        {
                            _taskService.UpdateData(newTasksIn);
                        }
                    }
                }
                #endregion
                #region å‡ºåº“推送任务1线
                {
                    var newTasksOut1 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress=="5236").OrderBy(x => x.Grade).ThenBy(x => x.GroupId).Take(5).ToList();
                    var downTasksOut1 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5236").ToList();
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5236");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.112");
                        if (newTasksOut1.Count > 0 && downTasksOut1.Count <= 0 && stationCurrentStatus == 1)
                        {
                            foreach (var task in newTasksOut1)
                            {
                                try
                                {
                                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                                        task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                                    else
                                        task.NextAddress = GetAGVAddress(task.NextAddress);
                                    AgvTaskDTO taskDTO = new AgvTaskDTO()
                                    {
                                        type = "05",
                                        positionCode = task.NextAddress
                                        TaskCode = task.AgvTaskNum,
                                        ReqCode = DateTime.Now.ToString("yyMMdd") + 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 = task.Grade.ToString();
                                    }
                                    if (task.GroupId.IsNotEmptyOrNull())
                                    {
                                        taskDTO.GroupId = task.GroupId;
                                    }
                                    if (task.TaskType == TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                                    {
                                        taskDTO.Priority = "127";
                                    }
                                    //发送AGV任务
                                    WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                                    if (!content.Status)
                                        throw new Exception(content.Message);
                                    if (taskDTO.TaskTyp == "CPC")
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                                    }
                                    else
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                                    }
                                }
                            };
                            if (taskDTO.TaskTyp== "CPC")
                            {
                                taskDTO.Priority = task.Grade.ToString();
                                catch (Exception ex)
                                {
                                    task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                    task.ExceptionMessage = ex.Message;
                                }
                            }
                            if (task.GroupId.IsNotEmptyOrNull())
                            if (newTasksOut1.Count > 0)
                            {
                                taskDTO.GroupId=task.GroupId;
                                _taskService.UpdateData(newTasksOut1);
                            }
                            if (task.TaskType==TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                        }
                    }
                }
                #endregion
                #region å‡ºåº“推送任务2线
                {
                    var newTasksOut2 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5243").OrderBy(x => x.Grade).ThenBy(x => x.GroupId).Take(5).ToList();
                    var downTasksOut2 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5243").ToList();
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5243");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.126");
                        if (newTasksOut2.Count > 0 && downTasksOut2.Count <= 0 && stationCurrentStatus == 1)
                        {
                            foreach (var task in newTasksOut2)
                            {
                                taskDTO.Priority = "127";
                                try
                                {
                                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                                        task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                                    else
                                        task.NextAddress = GetAGVAddress(task.NextAddress);
                                    AgvTaskDTO taskDTO = new AgvTaskDTO()
                                    {
                                        TaskCode = task.AgvTaskNum,
                                        ReqCode = DateTime.Now.ToString("yyMMdd") + 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 = task.Grade.ToString();
                                    }
                                    if (task.GroupId.IsNotEmptyOrNull())
                                    {
                                        taskDTO.GroupId = task.GroupId;
                                    }
                                    if (task.TaskType == TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                                    {
                                        taskDTO.Priority = "127";
                                    }
                                    //发送AGV任务
                                    WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                                    if (!content.Status)
                                        throw new Exception(content.Message);
                                    if (taskDTO.TaskTyp == "CPC")
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                                    }
                                    else
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                    task.ExceptionMessage = ex.Message;
                                }
                            }
                            //发送AGV任务
                            WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                            if (!content.Status)
                                throw new Exception(content.Message);
                            if (taskDTO.TaskTyp == "CPC")
                            if (newTasksOut2.Count > 0)
                            {
                                task.Dispatchertime = DateTime.Now;
                                _taskService.UpdateData(newTasksOut2);
                            }
                        }
                        //else
                        //{
                        //    WriteInfo(nameof(AGV_CPJob), $"2线无任务,状态{stationCurrentStatus}");
                        //}
                    }
                }
                #endregion
                #region å‡ºåº“推送任务3线
                {
                    var newTasksOut3 = AllTasks.Where(x => (x.TaskState == TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5250").OrderBy(x => x.Grade).ThenBy(x => x.GroupId).Take(5).ToList();
                    var downTasksOut3 = AllTasks.Where(x => (x.TaskState > TaskStatusEnum.New.ObjToInt()) && x.TaskType < TaskTypeEnum.Inbound.ObjToInt() && x.NextAddress == "5250").ToList();
                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.AGVStationCode == "5250");
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
                    if (device != null)
                    {
                        OtherDevice otherDevice = (OtherDevice)device;
                        short stationCurrentStatus = device.Communicator.Read<short>("DB29.140");
                        if (newTasksOut3.Count > 0 && downTasksOut3.Count <= 0 && stationCurrentStatus == 1)
                        {
                            foreach (var task in newTasksOut3)
                            {
                                try
                                {
                                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                                        task.CurrentAddress = GetAGVAddress(task.CurrentAddress);
                                    else
                                        task.NextAddress = GetAGVAddress(task.NextAddress);
                                    AgvTaskDTO taskDTO = new AgvTaskDTO()
                                    {
                                        TaskCode = task.AgvTaskNum,
                                        ReqCode = DateTime.Now.ToString("yyMMdd") + 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 = task.Grade.ToString();
                                    }
                                    if (task.GroupId.IsNotEmptyOrNull())
                                    {
                                        taskDTO.GroupId = task.GroupId;
                                    }
                                    if (task.TaskType == TaskTypeEnum.OutMesRworkProduct.ObjToInt())
                                    {
                                        taskDTO.Priority = "127";
                                    }
                                    //发送AGV任务
                                    WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask);
                                    if (!content.Status)
                                        throw new Exception(content.Message);
                                    if (taskDTO.TaskTyp == "CPC")
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing);
                                    }
                                    else
                                    {
                                        task.Dispatchertime = DateTime.Now;
                                        _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                    task.ExceptionMessage = ex.Message;
                                }
                            }
                            if (newTasksOut3.Count > 0)
                            {
                                _taskService.UpdateData(newTasksOut3);
                            }
                        }
                        //else
                        //{
                        //    WriteInfo(nameof(AGV_CPJob), $"3线无任务,状态{stationCurrentStatus}");
                        //}
                    }
                }
                #endregion
                #region ç©ºæ¡†æ¬è¿
                {
                    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("yyMMdd") + 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);
                            }
                            else
                            catch (Exception ex)
                            {
                                task.Dispatchertime = DateTime.Now;
                                _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing);
                                task.TaskState = TaskStatusEnum.Exception.ObjToInt();
                                task.ExceptionMessage = ex.Message;
                            }
                            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("yyMMdd") + 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;
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {