1
huangxiaoqiang
2025-04-08 c814a981db2a078a3d1021263ad349e2c1de9188
Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
@@ -77,79 +77,115 @@
                CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                if (conveyorLine != null)
                {
                    //ConsoleHelper.WriteColorLine("开始:" + DateTime.Now, ConsoleColor.Cyan);
                    // 查询所有子设备的位置
                    List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                    // 获取所有站点管理器
                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    // 并行处理每个子设备
                    var tasks = childDeviceCodes.Select(childDeviceCode => ProcessDeviceAsync(conveyorLine, childDeviceCode)).ToList();
                    // 并行处理每个站点管理器
                    tasks = stationManagers.Select(station => Task.Run(async () =>
                    {
                        var (command, commandWrite) = ReadCommands(station, conveyorLine);
                        if (command == null || commandWrite == null)
                        {
                            return;
                        }
                        IStationHandler handler = StationHandlerFactory.GetHandler(station.stationType, this);
                        await handler.HandleStationAsync(conveyorLine, station, command, commandWrite);
                    })).ToList();
                    #region
                    //foreach (var station in stationManagers)
                    //{
                    //    #region
                    //    //tasks.Add(Task.Run(async () =>
                    //    //{
                    //    //    if (station.stationType == 11)
                    //    //    {
                    //    //        // 读取任务命令和设备命令
                    //    //        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
                    //    //        ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
                    //    //        if (command != null && commandWrite != null)
                    //    //        {
                    //    //            // 将交互信号转换为布尔数组
                    //    //            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                    //    //            if (structs[0] == true)
                    //    //            {
                    //    //                // 输出警告信息
                    //    //                var log = $"【{conveyorLine.DeviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{station.stationChildCode}】空托盘请求扫码入库";
                    //    //                ConsoleHelper.WriteWarningLine(log);
                    //    //                await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    //    //                WriteInfo(conveyorLine.DeviceName, log);
                    //    //                // 处理入库请求
                    //    //                NGRequestTaskInbound(conveyorLine, command, station, 0);
                    //    //            }
                    //    //            else
                    //    //            {
                    //    //                // 发送完成信号
                    //    //                ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
                    //    //            }
                    //    //        }
                    //    //    }
                    //    //    else if (station.stationType == 5)
                    //    //    {
                    //    //        // 读取任务命令和设备命令
                    //    //        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
                    //    //        ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
                    //    //        if (command != null && commandWrite != null)
                    //    //        {
                    //    //            // 将交互信号转换为布尔数组
                    //    //            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                    //    //            if (structs[0] == true)
                    //    //            {
                    //    //                // 检查是否存在任务
                    //    //                if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
                    //    //                {
                    //    //                    return;
                    //    //                }
                    //    //                // 输出警告信息
                    //    //                var log = $"【{conveyorLine.DeviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{station.stationChildCode}】异常口请求扫码入库";
                    //    //                ConsoleHelper.WriteWarningLine(log);
                    //    //                await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    //    //                WriteInfo(conveyorLine.DeviceName, log);
                    //    //                // 处理新任务
                    //    //                await HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
                    //    //            }
                    //    //            else
                    //    //            {
                    //    //                // 发送完成信号
                    //    //                ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
                    //    //            }
                    //    //        }
                    //    //    }
                    //    //}));
                    //    #endregion
                    //    tasks.Add(Task.Run(async () =>
                    //    {
                    //        var (command, commandWrite) = ReadCommands(station, conveyorLine);
                    //        if (command == null || commandWrite == null)
                    //        {
                    //            return;
                    //        }
                    //        IStationHandler handler = StationHandlerFactory.GetHandler(station.stationType, this);
                    //        await handler.HandleStationAsync(conveyorLine, station, command, commandWrite);
                    //    }));
                    //}
                    #endregion
                    await Task.WhenAll(tasks);
                    // 获取所有站点管理器
                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
                    foreach (var station in stationManagers)
                    {
                        if (station.stationType == 11)
                        {
                            // 读取任务命令和设备命令
                            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
                            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
                            if (command != null && commandWrite != null)
                            {
                                // 将交互信号转换为布尔数组
                                var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                                if (structs[0] == true)
                                {
                                    // 输出警告信息
                                    var log = $"【{conveyorLine.DeviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{station.stationChildCode}】空托盘请求扫码入库";
                                    ConsoleHelper.WriteWarningLine(log);
                                    await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                                    WriteInfo(conveyorLine.DeviceName, log);
                                    // 处理入库请求
                                    NGRequestTaskInbound(conveyorLine, command, station, 0);
                                }
                                else
                                {
                                    // 发送完成信号
                                    ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
                                }
                            }
                        }
                        else if (station.stationType == 5)
                        {
                            // 读取任务命令和设备命令
                            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
                            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
                            if (command != null && commandWrite != null)
                            {
                                // 将交互信号转换为布尔数组
                                var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
                                if (structs[0] == true)
                                {
                                    // 检查是否存在任务
                                    if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
                                    {
                                        return;
                                    }
                                    // 输出警告信息
                                    var log = $"【{conveyorLine.DeviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{station.stationChildCode}】异常口请求扫码入库";
                                    ConsoleHelper.WriteWarningLine(log);
                                    await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                                    WriteInfo(conveyorLine.DeviceName, log);
                                    // 处理新任务
                                    await HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
                                }
                                else
                                {
                                    // 发送完成信号
                                    ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
                                }
                            }
                        }
                    }
                    //ConsoleHelper.WriteColorLine("结束:" + DateTime.Now, ConsoleColor.Cyan);
                }
            }
            catch (Exception ex)
@@ -221,7 +257,7 @@
                    }
                    // 查询平台信息
                    Platform platform = _platFormRepository.QueryFirst(x => x.PLCCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                    Platform platform = _platFormRepository.QueryFirst(x => x.DeviceCode == conveyorLine.DeviceCode && x.PlatCode == childDeviceCode && x.Status == "Active");
                    if (platform != null && !string.IsNullOrEmpty(platform.Location))
                    {
                        var strings = platform.Location.Split(',').ToList();
@@ -732,7 +768,7 @@
                }
                var wmsIpAddress = wmsBase + requestTrayOutTask;
                var device = _deviceInfoRepository.QueryData(x => x.DeviceStatus == "1" && x.DeviceRemark == platform.Id.ToString());
                var device = _deviceInfoRepository.QueryData(x => x.DeviceStatus == "1");
                var deviceCode = device.Select(x => x.DeviceCode).ToList();
                // 发送HTTP POST请求获取任务数据
@@ -762,7 +798,7 @@
            var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
            if (content.Status)
            {
                Console.WriteLine($"{taskDTO.TaskType}呼叫成功");
                Console.WriteLine($"{taskDTO.TargetAddress}呼叫成功");
            }
            return content;
        }
@@ -853,5 +889,20 @@
        }
        #endregion 设备NG口入库
        public async Task LogAndWarn(string deviceName, string log, string color = "red")
        {
            ConsoleHelper.WriteWarningLine(log);
            await _noticeService.Logs(userTokenIds, new { deviceName, log = log, time = DateTime.Now.ToString("G"), color = color });
            WriteInfo(deviceName, log);
        }
        private (ConveyorLineTaskCommand, ConveyorLineTaskCommandWrite) ReadCommands(Dt_StationManager station, CommonConveyorLine conveyorLine)
        {
            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
            return (command, commandWrite);
        }
    }
}