| | |
| | | private readonly ITaskExecuteDetailService _taskExecuteDetailService; |
| | | private readonly ITaskRepository _taskRepository; |
| | | private readonly IRouterService _routerService; |
| | | private readonly StackerCraneCommandConfig _config; |
| | | |
| | | public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService) |
| | | { |
| | |
| | | _taskExecuteDetailService = taskExecuteDetailService; |
| | | _taskRepository = taskRepository; |
| | | _routerService = routerService; |
| | | _config = LoadConfig(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 加载配置(优先级:配置文件 > 默认配置) |
| | | /// </summary> |
| | | private static StackerCraneCommandConfig LoadConfig() |
| | | { |
| | | try |
| | | { |
| | | string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StackerCraneJob", "stackercrane-command-config.json"); |
| | | if (File.Exists(configPath)) |
| | | { |
| | | string json = File.ReadAllText(configPath); |
| | | return System.Text.Json.JsonSerializer.Deserialize<StackerCraneCommandConfig>(json) ?? new StackerCraneCommandConfig(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"配置加载失败: {ex.Message},使用默认配置"); |
| | | } |
| | | return new StackerCraneCommandConfig(); |
| | | } |
| | | |
| | | public Task Execute(IJobExecutionContext context) |
| | |
| | | Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " CommonStackerCraneJob Start"); |
| | | |
| | | bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); |
| | | if (!flag || value is not CommonStackerCrane commonStackerCrane) |
| | | if (!flag || value is not IStackerCrane commonStackerCrane) |
| | | { |
| | | return Task.CompletedTask; |
| | | } |
| | |
| | | commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler; |
| | | } |
| | | |
| | | if (commonStackerCrane.StackerCraneAutoStatusValue != StackerCraneAutoStatus.Automatic |
| | | || commonStackerCrane.StackerCraneStatusValue != StackerCraneStatus.Normal) |
| | | if (commonStackerCrane.IsCanSendTask(commonStackerCrane.Communicator, commonStackerCrane.DeviceProDTOs, commonStackerCrane.DeviceProtocolDetailDTOs)) |
| | | { |
| | | return Task.CompletedTask; |
| | | } |
| | | commonStackerCrane.CheckStackerCraneTaskCompleted(); |
| | | |
| | | commonStackerCrane.CheckStackerCraneTaskCompleted(); |
| | | |
| | | if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) |
| | | { |
| | | Dt_Task? task = GetTask(commonStackerCrane); |
| | | if (task != null) |
| | | { |
| | | StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); |
| | | var stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); |
| | | if (stackerCraneTaskCommand != null) |
| | | { |
| | | bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand); |
| | |
| | | /// </summary> |
| | | /// <param name="commonStackerCrane">堆垛机对象</param> |
| | | /// <returns></returns> |
| | | private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane) |
| | | private Dt_Task? GetTask(IStackerCrane commonStackerCrane) |
| | | { |
| | | Dt_Task? task = null; |
| | | if (commonStackerCrane.LastTaskType == null) |
| | |
| | | /// <summary> |
| | | /// 任务实体转换成命令Model |
| | | /// </summary> |
| | | /// <param name="task">任务实体</param> |
| | | /// <returns></returns> |
| | | public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task) |
| | | public dynamic? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task) |
| | | { |
| | | StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand |
| | | // 根据配置判断命令类型 |
| | | string commandType = GetCommandType(task.Roadway); |
| | | |
| | | // 创建并构建命令 |
| | | return commandType switch |
| | | { |
| | | "Formation" => BuildCommand(task, CreateFormationCommand(task)), |
| | | _ => BuildCommand(task, CreateStandardCommand(task)) |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据 Roadway 获取命令类型 |
| | | /// </summary> |
| | | private string GetCommandType(string roadway) |
| | | { |
| | | foreach (var mapping in _config.RoadwayCommandMapping) |
| | | { |
| | | if (roadway.Contains(mapping.Key)) |
| | | { |
| | | return mapping.Value; |
| | | } |
| | | } |
| | | return _config.DefaultCommandType; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建标准堆垛机命令 |
| | | /// </summary> |
| | | private static StackerCraneTaskCommand CreateStandardCommand(Dt_Task task) |
| | | { |
| | | return new StackerCraneTaskCommand |
| | | { |
| | | Barcode = task.PalletCode, |
| | | TaskNum = task.TaskNum, |
| | | WorkType = 1, |
| | | TrayType = 0 |
| | | }; |
| | | } |
| | | |
| | | TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup(); |
| | | return taskTypeGroup switch |
| | | /// <summary> |
| | | /// 创建组盘堆垛机命令 |
| | | /// </summary> |
| | | private static FormationStackerCraneTaskCommand CreateFormationCommand(Dt_Task task) |
| | | { |
| | | return new FormationStackerCraneTaskCommand |
| | | { |
| | | TaskTypeGroup.InboundGroup => BuildInboundCommand(task, stackerCraneTaskCommand), |
| | | TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, stackerCraneTaskCommand), |
| | | TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, stackerCraneTaskCommand), |
| | | _ => stackerCraneTaskCommand |
| | | Barcode = task.PalletCode, |
| | | TaskNum = task.TaskNum, |
| | | WorkType = 1, |
| | | WorkAction = 1, |
| | | FireAlarm = 0, |
| | | HeartBeat = 0, |
| | | FieldName = string.Empty |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 构建入库命令 |
| | | /// 通用命令构建方法 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildInboundCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | private T? BuildCommand<T>(Dt_Task task, T command) where T : class |
| | | { |
| | | TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup(); |
| | | |
| | | return taskTypeGroup switch |
| | | { |
| | | TaskTypeGroup.InboundGroup => BuildInboundCommand(task, command), |
| | | TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, command), |
| | | TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, command), |
| | | _ => command |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 通用入库命令构建 |
| | | /// </summary> |
| | | private T? BuildInboundCommand<T>(Dt_Task task, T command) where T : class |
| | | { |
| | | Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, task.TaskType); |
| | | if (router == null) |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.StartRow = Convert.ToInt16(router.SrmRow); |
| | | command.StartColumn = Convert.ToInt16(router.SrmColumn); |
| | | command.StartLayer = Convert.ToInt16(router.SrmLayer); |
| | | SetCommandProperty(command, "StartRow", Convert.ToInt16(router.SrmRow)); |
| | | SetCommandProperty(command, "StartColumn", Convert.ToInt16(router.SrmColumn)); |
| | | SetCommandProperty(command, "StartLayer", Convert.ToInt16(router.SrmLayer)); |
| | | |
| | | if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) |
| | | { |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = endRow; |
| | | command.EndColumn = endColumn; |
| | | command.EndLayer = endLayer; |
| | | SetCommandProperty(command, "EndRow", endRow); |
| | | SetCommandProperty(command, "EndColumn", endColumn); |
| | | SetCommandProperty(command, "EndLayer", endLayer); |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 构建出库命令 |
| | | /// 通用出库命令构建 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildOutboundCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | private T? BuildOutboundCommand<T>(Dt_Task task, T command) where T : class |
| | | { |
| | | Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, task.TaskType); |
| | | if (router == null) |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = Convert.ToInt16(router.SrmRow); |
| | | command.EndColumn = Convert.ToInt16(router.SrmColumn); |
| | | command.EndLayer = Convert.ToInt16(router.SrmLayer); |
| | | SetCommandProperty(command, "EndRow", Convert.ToInt16(router.SrmRow)); |
| | | SetCommandProperty(command, "EndColumn", Convert.ToInt16(router.SrmColumn)); |
| | | SetCommandProperty(command, "EndLayer", Convert.ToInt16(router.SrmLayer)); |
| | | |
| | | if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) |
| | | { |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.StartRow = startRow; |
| | | command.StartColumn = startColumn; |
| | | command.StartLayer = startLayer; |
| | | SetCommandProperty(command, "StartRow", startRow); |
| | | SetCommandProperty(command, "StartColumn", startColumn); |
| | | SetCommandProperty(command, "StartLayer", startLayer); |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 构建移库命令 |
| | | /// 通用移库命令构建 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildRelocationCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | private T? BuildRelocationCommand<T>(Dt_Task task, T command) where T : class |
| | | { |
| | | if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) |
| | | { |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = endRow; |
| | | command.EndColumn = endColumn; |
| | | command.EndLayer = endLayer; |
| | | SetCommandProperty(command, "EndRow", endRow); |
| | | SetCommandProperty(command, "EndColumn", endColumn); |
| | | SetCommandProperty(command, "EndLayer", endLayer); |
| | | |
| | | if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) |
| | | { |
| | |
| | | return null; |
| | | } |
| | | |
| | | command.StartRow = startRow; |
| | | command.StartColumn = startColumn; |
| | | command.StartLayer = startLayer; |
| | | SetCommandProperty(command, "StartRow", startRow); |
| | | SetCommandProperty(command, "StartColumn", startColumn); |
| | | SetCommandProperty(command, "StartLayer", startLayer); |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 使用反射设置命令属性 |
| | | /// </summary> |
| | | private static void SetCommandProperty<T>(T command, string propertyName, object value) where T : class |
| | | { |
| | | var property = typeof(T).GetProperty(propertyName); |
| | | property?.SetValue(command, value); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | && short.TryParse(parts[2], out layer); |
| | | } |
| | | } |
| | | } |
| | | } |