#region << 版 本 注 释 >> /*---------------------------------------------------------------- * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob * 创建者:胡童庆 * 创建时间:2024/8/2 16:13:36 * 版本:V1.0.0 * 描述: * * ---------------------------------------------------------------- * 修改人: * 修改时间: * 版本:V1.0.1 * 修改说明: * *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using AutoMapper; using Microsoft.Extensions.Configuration; using Quartz; using SqlSugar; using System.Text.Json; using WIDESEA_Core; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO.TaskInfo; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.Service; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class CommonConveyorLineNewJob : IJob { private readonly ITaskService _taskService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly IRouterService _routerService; private readonly IMapper _mapper; private ConveyorLineDispatchHandler _conveyorLineDispatch; private readonly HttpClientHelper _httpClientHelper; public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, HttpClientHelper httpClientHelper) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; _mapper = mapper; _conveyorLineDispatch = new ConveyorLineDispatchHandler(_taskService, _taskExecuteDetailService, _routerService, _mapper); _httpClientHelper = httpClientHelper; } public Task Execute(IJobExecutionContext context) { try { CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams"); if (conveyorLine != null) { List childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode); if (childDeviceCodes == null || childDeviceCodes.Count == 0) { Console.WriteLine($"输送线 {conveyorLine.DeviceCode} 没有子设备"); return Task.CompletedTask; } // 创建并行选项 var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2), // 合理限制并发数 }; Parallel.For(0, childDeviceCodes.Count, parallelOptions, i => { string childDeviceCode = childDeviceCodes[i]; var correlationId = Guid.NewGuid().ToString("N"); try { ConveyorLineTaskCommandNew command = conveyorLine.ReadCustomer(childDeviceCode); #region 检查特定位置是否有托盘 var checkPalletPositions = App.Configuration.GetSection("CheckPalletPositions") .Get>() ?? new List(); if (checkPalletPositions.Any(x => x.Code == childDeviceCode)) { if (command.CV_State.ObjToBool()) { var existingTask = _taskService.Repository.QueryFirst(x => x.TargetAddress == childDeviceCode); if (existingTask.IsNullOrEmpty()) { var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode); var responseResult = _httpClientHelper.Post("GetOutBoundTrayTaskAsync", new CreateTaskDto() { WarehouseId = position.WarehouseId, TargetAddress = childDeviceCode }.Serialize()); if (responseResult.IsSuccess && responseResult.Data.Status) { var wmsTask = JsonSerializer.Deserialize>(responseResult.Data.Data.Serialize()); if (wmsTask != null) _taskService.ReceiveWMSTask(wmsTask); } } } } #endregion if (command == null || command.PLC_STB != 1) { return; } if (command.Barcode.IsNullOrEmpty()) { //无托盘号时 _conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode); return; } if (command.TaskNo > 0) { Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode); if (task.IsNullOrEmpty()) { _conveyorLineDispatch.RequestInbound(conveyorLine, command, childDeviceCode); return; } // 处理任务状态 ProcessTaskState(conveyorLine, command, task, childDeviceCode); } } catch (Exception innerEx) { Console.Error.WriteLine($"{DateTime.UtcNow:O} [{childDeviceCode}] CorrelationId={correlationId} {innerEx}"); } }); } } catch (Exception ex) { Console.Error.WriteLine(ex); } return Task.CompletedTask; } /// /// 处理任务状态 /// /// 输送线实例对象 /// 读取的请求信息 /// 子设备编号 /// private void ProcessTaskState(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, Dt_Task task, string childDeviceCode) { // 定义状态常量(如果类中已定义则可移除) const int InExecuting = (int)TaskInStatusEnum.Line_InExecuting; const int OutExecuting = (int)TaskOutStatusEnum.Line_OutExecuting; const int InFinish = (int)TaskInStatusEnum.InFinish; const int OutFinish = (int)TaskOutStatusEnum.OutFinish; int state = task.TaskState; bool isTargetAddress = task.TargetAddress == childDeviceCode; // 处理状态逻辑 switch (state) { case InExecuting: //if (isTargetAddress) // _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode); //else _conveyorLineDispatch.RequestInNextAddress(conveyorLine, command, childDeviceCode); break; case OutExecuting: if (isTargetAddress) _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode); else _conveyorLineDispatch.RequestOutNextAddress(conveyorLine, command, childDeviceCode); break; case InFinish: _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode); break; case OutFinish: _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode); break; } } } }