wangxinhui
4 天以前 a0a0df2e824b6fe7e5a3c0afce78127fecf84fc9
ÏîÄ¿´úÂë/WCS/WCSServices/WIDESEAWCS_Tasks/³ÉÆ·²Ö/ConveyorLineJob_CPB.cs
@@ -63,180 +63,187 @@
        public Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            try
            {
                OtherDevice device = (OtherDevice)value;
                List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList();
                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData();
                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
                if (flag && value != null)
                {
                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                    OtherDevice device = (OtherDevice)value;
                    List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList();
                    List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData();
                    foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                    {
                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                        //二楼入库扫码请求工位
                        if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 5 && conveyorLineInfoRead.TaskNo > 0)//&& !string.IsNullOrEmpty(conveyorLineInfoRead.PalletCode) todo临时删除条件限制
                        DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                        DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineCPDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                        if (item.StationType == StationTypeEnum.StationType_InStartAndOutEnd.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                        {
                            //查找对应任务
                            Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                            if (task != null)
                            R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                            //二楼入库扫码请求工位
                            if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 5 && conveyorLineInfoRead.TaskNo > 0)//&& !string.IsNullOrEmpty(conveyorLineInfoRead.PalletCode) todo临时删除条件限制
                            {
                                //分配巷道 å­˜å…¥æ‰˜ç›˜æ•°æ®
                                //string roadWay = _taskService.AssignCPRoadwayNo().Data.ToString();
                                //if (!string.IsNullOrEmpty(roadWay))
                                //{
                                //查找对应任务
                                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                                if (task != null)
                                {
                                    //分配巷道 å­˜å…¥æ‰˜ç›˜æ•°æ®
                                    //string roadWay = _taskService.AssignCPRoadwayNo().Data.ToString();
                                    //if (!string.IsNullOrEmpty(roadWay))
                                    //{
                                //}
                                Dt_StationManger stationManger = stationMangers.FirstOrDefault(x=>x.StationCode== task.SourceAddress);
                                if (stationManger != null)
                                {
                                    string roadWay = "";
                                    switch (stationManger.AGVStationCode.ObjToInt() % 5)
                                    //}
                                    Dt_StationManger stationManger = stationMangers.FirstOrDefault(x => x.StationCode == task.SourceAddress);
                                    if (stationManger != null)
                                    {
                                        case 1:
                                            roadWay = "SC01_CP";
                                            break;
                                        case 2:
                                            roadWay = "SC02_CP";
                                            break;
                                        case 3:
                                            roadWay = "SC03_CP";
                                            break;
                                        case 4:
                                            roadWay = "SC04_CP";
                                            break;
                                        case 0:
                                            roadWay = "SC05_CP";
                                            break;
                                        default:
                                            WriteError(item.StationName, $"未找到可分配巷道{task.TaskNum}");
                                            break;
                                    }
                                    if (!string.IsNullOrEmpty(roadWay))
                                    {
                                        List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, roadWay, task.TaskType);
                                        Dt_Router? router = routers.FirstOrDefault();
                                        if (routers == null || routers.Count == 0 || router == null)
                                        string roadWay = "";
                                        switch (stationManger.AGVStationCode.ObjToInt() % 5)
                                        {
                                            WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                            continue;
                                            case 1:
                                                roadWay = "SC01_CP";
                                                break;
                                            case 2:
                                                roadWay = "SC02_CP";
                                                break;
                                            case 3:
                                                roadWay = "SC03_CP";
                                                break;
                                            case 4:
                                                roadWay = "SC04_CP";
                                                break;
                                            case 0:
                                                roadWay = "SC05_CP";
                                                break;
                                            default:
                                                WriteError(item.StationName, $"未找到可分配巷道{task.TaskNum}");
                                                break;
                                        }
                                        if (routers.Count > 1)
                                        if (!string.IsNullOrEmpty(roadWay))
                                        {
                                            WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                            continue;
                                            List<Dt_Router> routers = _routerService.QueryNextRoutes(item.StationCode, roadWay, task.TaskType);
                                            Dt_Router? router = routers.FirstOrDefault();
                                            if (routers == null || routers.Count == 0 || router == null)
                                            {
                                                WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                continue;
                                            }
                                            if (routers.Count > 1)
                                            {
                                                WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                continue;
                                            }
                                            device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
                                            device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
                                            device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
                                            task.CurrentAddress = item.StationCode;
                                            task.NextAddress = router.NextPosi;
                                            task.PalletCode = DateTime.Now.ToString("yyMMddHHmmss");//todo临时
                                            task.Roadway = roadWay;
                                            task.DeviceCode = router.ChildPosiDeviceCode;
                                            _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
                                            WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                                        }
                                        device.SetValue(W_ConveyorLineCPDB.W_TargetAddress, router.NextPosi, item.StationCode);
                                        device.SetValue(W_ConveyorLineCPDB.W_TaskNo, task.TaskNum, item.StationCode);
                                        device.SetValue(W_ConveyorLineCPDB.W_Command, 1, item.StationCode);
                                        task.CurrentAddress = item.StationCode;
                                        task.NextAddress = router.NextPosi;
                                        task.PalletCode = DateTime.Now.ToString("yyMMddHHmmss");//todo临时
                                        task.Roadway = roadWay;
                                        task.DeviceCode = router.ChildPosiDeviceCode;
                                        _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
                                        WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                                    }
                                }
                            }
                        }
                        else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 2 && conveyorLineInfoRead.TaskNo > 0)
                        {
                            Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                            if (task != null)
                            else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 2 && conveyorLineInfoRead.TaskNo > 0)
                            {
                                //分配巷道 å­˜å…¥æ‰˜ç›˜æ•°æ®
                                List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.NextAddress == x.StartPosi);
                                Dt_Router router = routers.FirstOrDefault();
                                if (router == null)
                                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.NextAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                                if (task != null)
                                {
                                    WriteError(item.StationName, $"任务号:{task.TaskNum}未找到路由配置信息");
                                    return Task.CompletedTask;
                                    //分配巷道 å­˜å…¥æ‰˜ç›˜æ•°æ®
                                    List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.NextAddress == x.StartPosi);
                                    Dt_Router router = routers.FirstOrDefault();
                                    if (router == null)
                                    {
                                        WriteError(item.StationName, $"任务号:{task.TaskNum}未找到路由配置信息");
                                        return Task.CompletedTask;
                                    }
                                    //更新任务信息
                                    task.CurrentAddress = router.StartPosi;
                                    task.NextAddress = task.TargetAddress;
                                    task.DeviceCode = router.NextPosi;
                                    _taskService.UpdateTask(task, TaskStatusEnum.AGV_Execute);
                                    WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                                }
                                //更新任务信息
                                task.CurrentAddress = router.StartPosi;
                                task.NextAddress = task.TargetAddress;
                                task.DeviceCode = router.NextPosi;
                                _taskService.UpdateTask(task, TaskStatusEnum.AGV_Execute);
                                WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                            }
                        }
                    }
                    else if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt() && deviceProRead != null)
                    {//入库RGV接驳位交互
                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                        if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo > 0)
                        {
                            Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == device.DeviceCode);
                            if (task != null)
                        else if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt() && deviceProRead != null)
                        {//入库RGV接驳位交互
                            R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                            if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 4 && conveyorLineInfoRead.TaskNo > 0)
                            {
                                List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi);
                                Dt_Router? router = routers.FirstOrDefault();
                                if (routers == null || routers.Count == 0 || router == null)
                                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.CurrentAddress == item.StationCode && _taskService.TaskInboundTypes.Contains(x.TaskType) && x.TaskState == TaskStatusEnum.Line_Execute.ObjToInt() && x.DeviceCode == device.DeviceCode);
                                if (task != null)
                                {
                                    WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                    continue;
                                    List<Dt_Router> routers = _routerRepository.QueryData(x => x.InOutType == task.TaskType && task.CurrentAddress == x.StartPosi);
                                    Dt_Router? router = routers.FirstOrDefault();
                                    if (routers == null || routers.Count == 0 || router == null)
                                    {
                                        WriteError(item.StationName, $"未找到对应路由信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                        continue;
                                    }
                                    if (routers.Count > 1)
                                    {
                                        WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                        continue;
                                    }
                                    device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode);
                                    task.NextAddress = router.NextPosi;
                                    _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
                                    WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                                }
                                if (routers.Count > 1)
                                {
                                    WriteError(item.StationName, $"路由信息配置错误,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                    continue;
                                }
                                device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode);
                                task.NextAddress = router.NextPosi;
                                _taskService.UpdateTask(task, TaskStatusEnum.Line_Executing);
                                WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}下一步");
                            }
                        }
                    }
                    else if (item.StationType == StationTypeEnum.StationType_StackingPlates.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                    {//叠盘机工位叫料
                        R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                        //if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 7 && conveyorLineInfoRead.TaskNo <= 0)
                        //{
                        //    Dt_Task task = _taskRepository.QueryFirst(x =>  x.TargetAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType));
                        //    if (task == null)
                        //    {
                        //        //请求任务
                        //        WMSTaskDTO taskDTO = new WMSTaskDTO()
                        //        {
                        //            Id = 1,
                        //            SourceAddress = "SC02_CP-001-064-001-01",
                        //            TaskNum = DateTime.Now.ToString("mmss").ObjToInt(),
                        //            PalletCode = "" + DateTime.Now.ToString("mmss"),
                        //            RoadWay = "SC02_CP",
                        //            TaskType = 160,
                        //            TaskStatus = TaskStatusEnum.New.ObjToInt(),
                        //            TargetAddress = item.StationCode,
                        //            Grade = 0,
                        //            WarehouseId = 1,
                        //            PalletType = 1
                        //        };
                        //        WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}生成,下一步");
                        //        _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                        //    }
                        //}
                        ////叠盘任务完成交互
                        //else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 8 && conveyorLineInfoRead.TaskNo > 0)
                        //{
                        //    Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                        //    if (task != null)
                        //    {
                        //        //任务完成
                        //        device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode);
                        //        WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}叠盘位出库完成");
                        //    }
                        //}
                    }
                    else
                    {
                        WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息");
                        else if (item.StationType == StationTypeEnum.StationType_StackingPlates.ObjToInt() && deviceProRead != null && deviceProWrite != null)
                        {//叠盘机工位叫料
                            R_ConveyorLineCPInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineCPInfo>(deviceProRead.DeviceProAddress);
                            //if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 7 && conveyorLineInfoRead.TaskNo <= 0)
                            //{
                            //    Dt_Task task = _taskRepository.QueryFirst(x =>  x.TargetAddress == item.StationCode && _taskService.TaskOutboundTypes.Contains(x.TaskType));
                            //    if (task == null)
                            //    {
                            //        //请求任务
                            //        WMSTaskDTO taskDTO = new WMSTaskDTO()
                            //        {
                            //            Id = 1,
                            //            SourceAddress = "SC02_CP-001-064-001-01",
                            //            TaskNum = DateTime.Now.ToString("mmss").ObjToInt(),
                            //            PalletCode = "" + DateTime.Now.ToString("mmss"),
                            //            RoadWay = "SC02_CP",
                            //            TaskType = 160,
                            //            TaskStatus = TaskStatusEnum.New.ObjToInt(),
                            //            TargetAddress = item.StationCode,
                            //            Grade = 0,
                            //            WarehouseId = 1,
                            //            PalletType = 1
                            //        };
                            //        WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}生成,下一步");
                            //        _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                            //    }
                            //}
                            ////叠盘任务完成交互
                            //else if (conveyorLineInfoRead != null && conveyorLineInfoRead.Command == 8 && conveyorLineInfoRead.TaskNo > 0)
                            //{
                            //    Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == conveyorLineInfoRead.TaskNo && x.TaskState == TaskStatusEnum.Line_Executing.ObjToInt() && x.DeviceCode == device.DeviceCode);
                            //    if (task != null)
                            //    {
                            //        //任务完成
                            //        device.SetValue(W_ConveyorLineCPDB.W_Command, 2, item.StationCode);
                            //        WriteInfo(item.StationName, $"任务号{conveyorLineInfoRead.TaskNo}叠盘位出库完成");
                            //    }
                            //}
                        }
                        else
                        {
                            WriteError(item.StationName, $"未找到设备子编号{item.StationCode}的协议信息");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteError(nameof(ConveyorLineJob_CPB),ex.Message);
            }
            return Task.CompletedTask;
        }
    }