| | |
| | | using MapsterMapper; |
| | | using Masuit.Tools; |
| | | using Microsoft.Extensions.Configuration; |
| | | using Microsoft.Extensions.Logging; |
| | | using Newtonsoft.Json; |
| | |
| | | /// 任务服务 |
| | | /// </summary> |
| | | private readonly ITaskService _taskService; |
| | | |
| | | |
| | | /// <summary> |
| | | /// 机器人任务服务 |
| | | /// </summary> |
| | | private readonly IRobotTaskService _robotTaskService; |
| | | |
| | | /// <summary> |
| | | /// 任务执行明细服务 |
| | |
| | | /// <param name="mapper">对象映射器</param> |
| | | /// <param name="httpClientHelper">HTTP 客户端帮助类</param> |
| | | /// <param name="logger">日志记录器</param> |
| | | public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, HttpClientHelper httpClientHelper, ILogger<CommonConveyorLineNewJob> logger) |
| | | public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, HttpClientHelper httpClientHelper, ILogger<CommonConveyorLineNewJob> logger, IRobotTaskService robotTaskService) |
| | | { |
| | | _taskService = taskService; |
| | | _taskExecuteDetailService = taskExecuteDetailService; |
| | |
| | | _mapper = mapper; |
| | | _httpClientHelper = httpClientHelper; |
| | | _logger = logger; |
| | | _robotTaskService = robotTaskService; |
| | | |
| | | // 初始化调度处理器 |
| | | _conveyorLineDispatch = new ConveyorLineDispatchHandler(_taskService, _taskExecuteDetailService, _routerService, _mapper, _logger); |
| | |
| | | if (childDeviceCodes == null || childDeviceCodes.Count == 0) |
| | | { |
| | | // 没有子设备,直接返回 |
| | | _logger.LogInformation("输送线 {DeviceCode} 没有子设备", conveyorLine.DeviceCode); |
| | | QuartzLogger.Info($"输送线 {conveyorLine.DeviceCode} 没有子设备", conveyorLine.DeviceCode); |
| | | QuartzLogHelper.LogInfo(_logger, "输送线 {DeviceCode} 没有子设备", $"输送线 {conveyorLine.DeviceCode} 没有子设备", conveyorLine.DeviceCode, conveyorLine.DeviceCode); |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | |
| | | // // 限制并发数:子设备数量和 CPU 核心数*2 的较小值 |
| | | // MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2), |
| | | //}; |
| | | |
| | | //_logger.LogDebug("Execute:开始并行处理输送线 {DeviceCode},子设备数量: {Count}", conveyorLine.DeviceCode, childDeviceCodes.Count); |
| | | //QuartzLogger.Debug($"开始并行处理输送线,子设备数量: {childDeviceCodes.Count}", conveyorLine.DeviceCode); |
| | | |
| | | // 并行处理每个子设备 |
| | | //Parallel.For(0, childDeviceCodes.Count, parallelOptions, i => |
| | |
| | | // 如果命令为空,跳过 |
| | | if (command == null) |
| | | { |
| | | _logger.LogDebug("Execute:子设备 {ChildDeviceCode} 命令为空,跳过", childDeviceCode); |
| | | QuartzLogger.Debug($"子设备 {childDeviceCode} 命令为空,跳过", conveyorLine.DeviceCode); |
| | | continue; |
| | | } |
| | | |
| | | #region 检测是否需要空托盘 |
| | | |
| | | // ========== 检查特定位置是否有托盘 ========== |
| | | // 从配置中读取需要检查托盘的位置列表 |
| | |
| | | { |
| | | // 没有任务,向 WMS 请求出库托盘任务 |
| | | var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode); |
| | | _logger.LogInformation("Execute:检查托盘位置 {ChildDeviceCode},请求WMS出库托盘任务", childDeviceCode); |
| | | QuartzLogger.Info($"检查托盘位置 {childDeviceCode},请求WMS出库托盘任务", conveyorLine.DeviceCode); |
| | | QuartzLogHelper.LogInfo(_logger, "Execute:检查托盘位置 {ChildDeviceCode},请求WMS出库托盘任务", $"检查托盘位置 {childDeviceCode},请求WMS出库托盘任务", conveyorLine.DeviceCode, childDeviceCode); |
| | | |
| | | var responseResult = _httpClientHelper.Post<WebResponseContent>("GetOutBoundTrayTaskAsync", new CreateTaskDto() |
| | | { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | // ========== 检查 PLC_STB 标志 ========== |
| | | // 只有当 PLC_STB 为 1 时才处理任务 |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _logger.LogError(ex, "处理手动入库任务异常"); |
| | | QuartzLogger.Error($"处理手动入库任务异常: {ex.Message}", "CommonConveyorLineNewJob", ex); |
| | | QuartzLogHelper.LogError(_logger, ex, "处理手动入库任务异常", $"处理手动入库任务异常: {ex.Message}", "CommonConveyorLineNewJob"); |
| | | } |
| | | continue; |
| | | } |
| | |
| | | // 无托盘条码时,请求出库任务 |
| | | if (command.Barcode.IsNullOrEmpty() || command.Barcode.Replace("\0", "") == "") |
| | | { |
| | | _logger.LogDebug("Execute:子设备 {ChildDeviceCode} 无托盘条码,请求出库任务", childDeviceCode); |
| | | QuartzLogger.Debug($"子设备 {childDeviceCode} 无托盘条码,请求出库任务", conveyorLine.DeviceCode); |
| | | _conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode); |
| | | continue; |
| | | } |
| | |
| | | Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode); |
| | | if (!task.IsNullOrEmpty()) |
| | | { |
| | | _logger.LogInformation("Execute:子设备 {ChildDeviceCode} 处理任务 {TaskNum},状态: {Status}", childDeviceCode, task.TaskNum, task.TaskStatus); |
| | | QuartzLogger.Info($"处理任务 {task.TaskNum},状态: {task.TaskStatus}", conveyorLine.DeviceCode); |
| | | QuartzLogHelper.LogInfo(_logger, "Execute:子设备 {ChildDeviceCode} 处理任务 {TaskNum},状态: {Status}", $"处理任务 {task.TaskNum},状态: {task.TaskStatus}", conveyorLine.DeviceCode, childDeviceCode, task.TaskNum, task.TaskStatus); |
| | | // 处理任务状态(根据状态分发到不同方法) |
| | | ProcessTaskState(conveyorLine, command, task, childDeviceCode); |
| | | return Task.CompletedTask; |
| | | } |
| | | else if (command.TaskNo == 1 && !command.Barcode.IsNullOrEmpty() && childDeviceCode == "11068") |
| | | { |
| | | if (_robotTaskService.Db.Queryable<Dt_RobotTask>().Any(x => x.RobotTargetAddressPalletCode == command.Barcode)) |
| | | { |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | Random rnd = new Random(); |
| | | int num = rnd.StrictNext();//产生真随机数 |
| | | // 没有任务号但有条码并且在11068位置,直接添加机械手组盘任务 |
| | | Dt_RobotTask robotTask = new Dt_RobotTask |
| | | { |
| | | RobotTargetAddressPalletCode = command.Barcode, |
| | | RobotSourceAddress = "1", |
| | | RobotTargetAddress = "2", // 机械手目标地址 |
| | | RobotTaskType = (int)RobotTaskTypeEnum.GroupPallet, // 组盘任务 |
| | | RobotTaskState = (int)TaskRobotStatusEnum.RobotNew, // 待执行 |
| | | RobotTaskTotalNum = 48, |
| | | RobotGrade = 1, |
| | | RobotRoadway = "注液组盘机械手", |
| | | RobotTargetAddressLineCode = childDeviceCode, |
| | | RobotTaskNum = num, // 生成任务号 |
| | | RobotDispatchertime = DateTime.Now, |
| | | |
| | | }; |
| | | if (_robotTaskService.AddData(robotTask).Status) |
| | | { |
| | | conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, (short)1, childDeviceCode); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception innerEx) |
| | | { |
| | | // 记录异常,但不影响其他子设备的处理 |
| | | _logger.LogError(innerEx, "Execute:子设备 {ChildDeviceCode} 处理异常,CorrelationId: {CorrelationId}", childDeviceCode, correlationId); |
| | | QuartzLogger.Error($"子设备处理异常: {innerEx.Message}", conveyorLine.DeviceCode, innerEx); |
| | | QuartzLogHelper.LogError(_logger, innerEx, "Execute:子设备 {ChildDeviceCode} 处理异常,CorrelationId: {CorrelationId}", $"子设备处理异常: {innerEx.Message}", conveyorLine.DeviceCode, childDeviceCode, correlationId); |
| | | } |
| | | } |
| | | } |
| | |
| | | catch (Exception ex) |
| | | { |
| | | // 记录整体异常 |
| | | _logger.LogError(ex, "Execute:输送线 {DeviceCode} 执行异常", ex.Message); |
| | | QuartzLogger.Error($"输送线执行异常: {ex.Message}", "CommonConveyorLineNewJob", ex); |
| | | QuartzLogHelper.LogError(_logger, ex, "Execute:输送线 {DeviceCode} 执行异常", $"输送线执行异常: {ex.Message}", "CommonConveyorLineNewJob", ex.Message); |
| | | } |
| | | return Task.CompletedTask; |
| | | } |