| | |
| | | { |
| | | try |
| | | { |
| | | List<Dt_Task> tasks = _taskService.Repository.QueryData(); |
| | | |
| | | 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 != null && value is CommonStackerCrane commonStackerCrane) |
| | | { |
| | | if (!commonStackerCrane.IsEventSubscribed) |
| | | { |
| | | commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件 |
| | | } |
| | | if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal) |
| | | { |
| | | commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次 |
| | | |
| | | if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) |
| | | bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); |
| | | if (!flag || value is not CommonStackerCrane commonStackerCrane) |
| | | { |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | if (!commonStackerCrane.IsEventSubscribed) |
| | | { |
| | | commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler; |
| | | } |
| | | |
| | | if (commonStackerCrane.StackerCraneAutoStatusValue != StackerCraneAutoStatus.Automatic |
| | | || commonStackerCrane.StackerCraneStatusValue != StackerCraneStatus.Normal) |
| | | { |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | commonStackerCrane.CheckStackerCraneTaskCompleted(); |
| | | |
| | | if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) |
| | | { |
| | | Dt_Task? task = GetTask(commonStackerCrane); |
| | | if (task != null) |
| | | { |
| | | StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); |
| | | if (stackerCraneTaskCommand != null) |
| | | { |
| | | Dt_Task? task = GetTask(commonStackerCrane); |
| | | if (task != null) |
| | | bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand); |
| | | if (sendFlag) |
| | | { |
| | | int num = new Random().Next(1, 100); |
| | | if (num < 30) |
| | | { |
| | | throw new CommunicationException("错误测试", CommunicationErrorType.Unknown); |
| | | } |
| | | StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); |
| | | if (stackerCraneTaskCommand != null) |
| | | { |
| | | bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand); |
| | | if (sendFlag) |
| | | { |
| | | commonStackerCrane.LastTaskType = task.TaskType; |
| | | _taskService.UpdateTaskStatusToNext(task.TaskNum); |
| | | } |
| | | } |
| | | commonStackerCrane.LastTaskType = task.TaskType; |
| | | _taskService.UpdateTaskStatusToNext(task.TaskNum); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"CommonStackerCraneJob Error: {ex.Message}"); |
| | | } |
| | | return Task.CompletedTask; |
| | | } |
| | |
| | | /// <returns></returns> |
| | | private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane) |
| | | { |
| | | Dt_Task task; |
| | | Dt_Task? task = null; |
| | | if (commonStackerCrane.LastTaskType == null) |
| | | { |
| | | task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode); |
| | |
| | | if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) |
| | | { |
| | | task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); |
| | | if (task == null) |
| | | { |
| | | task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode); |
| | | } |
| | | task ??= _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) |
| | | { |
| | | if (OutTaskStationIsOccupied(task) != null) |
| | | if (IsOutTaskStationAvailable(task)) |
| | | { |
| | | return task; |
| | | } |
| | | else |
| | | |
| | | List<string> otherOutStationCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress, task.TaskType) |
| | | .Select(x => x.ChildPosi).ToList(); |
| | | List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStationCodes); |
| | | foreach (var alternativeTask in tasks) |
| | | { |
| | | List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList(); |
| | | List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); |
| | | foreach (var item in tasks) |
| | | if (IsOutTaskStationAvailable(alternativeTask)) |
| | | { |
| | | if (OutTaskStationIsOccupied(task) != null) |
| | | { |
| | | return task; |
| | | } |
| | | return alternativeTask; |
| | | } |
| | | task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); |
| | | } |
| | | task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); |
| | | } |
| | | |
| | | return task; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 出库任务判断出库站台是否被占用 |
| | | /// 出库任务判断出库站台是否可用 |
| | | /// </summary> |
| | | /// <param name="task">任务实体</param> |
| | | /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns> |
| | | private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task) |
| | | /// <returns>如果站台可用返回true,否则返回false</returns> |
| | | private bool IsOutTaskStationAvailable([NotNull] Dt_Task task) |
| | | { |
| | | |
| | | Dt_Router? router = _routerService.QueryNextRoutes(task.Roadway, task.NextAddress).FirstOrDefault(); |
| | | if (router != null) |
| | | { |
| | | IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode); |
| | | if (device != null) |
| | | { |
| | | CommonConveyorLine conveyorLine = (CommonConveyorLine)device; |
| | | if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用 |
| | | { |
| | | return task; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); |
| | | } |
| | | } |
| | | else |
| | | Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.NextAddress, task.TaskType); |
| | | if (router == null) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); |
| | | return false; |
| | | } |
| | | return null; |
| | | |
| | | IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode); |
| | | if (device == null) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); |
| | | return false; |
| | | } |
| | | |
| | | CommonConveyorLine conveyorLine = (CommonConveyorLine)device; |
| | | return conveyorLine.IsOccupied(router.ChildPosi); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="task">任务实体</param> |
| | | /// <returns></returns> |
| | | /// <exception cref="Exception"></exception> |
| | | public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task) |
| | | { |
| | | StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand(); |
| | | |
| | | stackerCraneTaskCommand.Barcode = task.PalletCode; |
| | | stackerCraneTaskCommand.TaskNum = task.TaskNum; |
| | | stackerCraneTaskCommand.WorkType = 1; |
| | | stackerCraneTaskCommand.TrayType = 0; |
| | | if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务 |
| | | StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand |
| | | { |
| | | List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway); |
| | | if (routers.Count > 0) |
| | | { |
| | | stackerCraneTaskCommand.StartRow = Convert.ToInt16(routers.FirstOrDefault().SrmRow); |
| | | stackerCraneTaskCommand.StartColumn = Convert.ToInt16(routers.FirstOrDefault().SrmColumn); |
| | | stackerCraneTaskCommand.StartLayer = Convert.ToInt16(routers.FirstOrDefault().SrmLayer); |
| | | Barcode = task.PalletCode, |
| | | TaskNum = task.TaskNum, |
| | | WorkType = 1, |
| | | TrayType = 0 |
| | | }; |
| | | |
| | | string[] targetCodes = task.NextAddress.Split("-"); |
| | | if (targetCodes.Length == 3) |
| | | { |
| | | stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]); |
| | | stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); |
| | | stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); |
| | | } |
| | | else |
| | | { |
| | | //数据配置错误 |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】"); |
| | | return null; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息"); |
| | | return null; |
| | | } |
| | | } |
| | | else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) |
| | | TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup(); |
| | | return taskTypeGroup switch |
| | | { |
| | | List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress); |
| | | if (routers.Count > 0) |
| | | { |
| | | stackerCraneTaskCommand.EndRow = Convert.ToInt16(routers.FirstOrDefault().SrmRow); |
| | | stackerCraneTaskCommand.EndColumn = Convert.ToInt16(routers.FirstOrDefault().SrmColumn); |
| | | stackerCraneTaskCommand.EndLayer = Convert.ToInt16(routers.FirstOrDefault().SrmLayer); |
| | | TaskTypeGroup.InboundGroup => BuildInboundCommand(task, stackerCraneTaskCommand), |
| | | TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, stackerCraneTaskCommand), |
| | | TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, stackerCraneTaskCommand), |
| | | _ => stackerCraneTaskCommand |
| | | }; |
| | | } |
| | | |
| | | string[] sourceCodes = task.CurrentAddress.Split("-"); |
| | | if (sourceCodes.Length == 3) |
| | | { |
| | | stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]); |
| | | stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]); |
| | | stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]); |
| | | } |
| | | else |
| | | { |
| | | //数据配置错误 |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); |
| | | return null; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息"); |
| | | return null; |
| | | } |
| | | } |
| | | else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) |
| | | /// <summary> |
| | | /// 构建入库命令 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildInboundCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | { |
| | | Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, task.TaskType); |
| | | if (router == null) |
| | | { |
| | | string[] targetCodes = task.NextAddress.Split("-"); |
| | | if (targetCodes.Length == 3) |
| | | { |
| | | stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]); |
| | | stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); |
| | | stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); |
| | | } |
| | | else |
| | | { |
| | | //数据配置错误 |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.NextAddress}】"); |
| | | return null; |
| | | } |
| | | string[] sourceCodes = task.CurrentAddress.Split("-"); |
| | | if (sourceCodes.Length == 3) |
| | | { |
| | | stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]); |
| | | stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]); |
| | | stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]); |
| | | } |
| | | else |
| | | { |
| | | //数据配置错误 |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】"); |
| | | return null; |
| | | } |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.CurrentAddress}】信息,无法获取对应的堆垛机取货站台信息"); |
| | | return null; |
| | | } |
| | | return stackerCraneTaskCommand; |
| | | |
| | | command.StartRow = Convert.ToInt16(router.SrmRow); |
| | | command.StartColumn = Convert.ToInt16(router.SrmColumn); |
| | | command.StartLayer = Convert.ToInt16(router.SrmLayer); |
| | | |
| | | if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,终点:【{task.NextAddress}】"); |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = endRow; |
| | | command.EndColumn = endColumn; |
| | | command.EndLayer = endLayer; |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 构建出库命令 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildOutboundCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | { |
| | | Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, task.TaskType); |
| | | if (router == null) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress}】信息,无法获取对应的堆垛机放货站台信息"); |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = Convert.ToInt16(router.SrmRow); |
| | | command.EndColumn = Convert.ToInt16(router.SrmColumn); |
| | | command.EndLayer = Convert.ToInt16(router.SrmLayer); |
| | | |
| | | if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); |
| | | return null; |
| | | } |
| | | |
| | | command.StartRow = startRow; |
| | | command.StartColumn = startColumn; |
| | | command.StartLayer = startLayer; |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 构建移库命令 |
| | | /// </summary> |
| | | private StackerCraneTaskCommand? BuildRelocationCommand(Dt_Task task, StackerCraneTaskCommand command) |
| | | { |
| | | if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,终点:【{task.NextAddress}】"); |
| | | return null; |
| | | } |
| | | |
| | | command.EndRow = endRow; |
| | | command.EndColumn = endColumn; |
| | | command.EndLayer = endLayer; |
| | | |
| | | if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) |
| | | { |
| | | _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】"); |
| | | return null; |
| | | } |
| | | |
| | | command.StartRow = startRow; |
| | | command.StartColumn = startColumn; |
| | | command.StartLayer = startLayer; |
| | | |
| | | return command; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 解析地址字符串(格式:行-列-层) |
| | | /// </summary> |
| | | private bool TryParseAddress(string address, out short row, out short column, out short layer) |
| | | { |
| | | row = column = layer = 0; |
| | | |
| | | string[] parts = address.Split("-"); |
| | | if (parts.Length != 3) |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | return short.TryParse(parts[0], out row) |
| | | && short.TryParse(parts[1], out column) |
| | | && short.TryParse(parts[2], out layer); |
| | | } |
| | | } |
| | | } |