using Quartz; using SqlSugar.Extensions; using System.Diagnostics.CodeAnalysis; using WIDESEA_Core.Enums; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DeviceBase; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_QuartzJob.StackerCrane; using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class CommonStackerCraneJob : IJob { private readonly ITaskService _taskService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly ITaskRepository _taskRepository; private readonly IRouterService _routerService; public CommonStackerCraneJob(ITaskService taskService, ITaskRepository taskRepository, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService) { _taskService = taskService; _taskRepository = taskRepository; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; } public Task Execute(IJobExecutionContext context) { try { SpeStackerCrane speStackerCrane = (SpeStackerCrane)context.JobDetail.JobDataMap.Get("JobParams"); if (speStackerCrane != null) { GetStackerObject getStackerObject = new GetStackerObject(speStackerCrane); if (!getStackerObject.IsEventSubscribed) { getStackerObject.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件 } getStackerObject.CheckStackerCraneTaskCompleted();//检测堆垛机任务完成事件 if (getStackerObject.StackerCraneStatusValue == StackerCraneStatus.Ready) { Dt_Task? task = GetTask(speStackerCrane); if (task != null) { bool sendFlag = true; //if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletOutbound.ObjToInt()) //{ // //向WMS申请出入库口是否有空托盘 // sendFlag = false; //} if(sendFlag) { StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); if (stackerCraneTaskCommand != null) { sendFlag = getStackerObject.SendCommand(stackerCraneTaskCommand); if (sendFlag) { speStackerCrane.SetValue(StackerCraneDBName.CommandSend, 1);//启动命令 speStackerCrane.LastTaskType = task.TaskType; _taskService.UpdateTaskStatusToNext(task.TaskNum); } } } } } } } catch (Exception ex) { //WriteLog.GetLog($"堆垛机异常").Write($"{nameof(CommonStackerCraneJob)}:{ex.Message}", "堆垛机异常"); } return Task.CompletedTask; } /// /// 任务完成事件订阅的方法 /// /// /// private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e) { SpeStackerCrane? speStackerCrane = sender as SpeStackerCrane; if (speStackerCrane != null) { if (speStackerCrane.GetValue(StackerCraneDBName.CommandSend) != 2) { speStackerCrane.SetValue(StackerCraneDBName.CommandSend, 2); _taskService.StackCraneTaskCompleted(e.TaskNum, "堆垛机"); } } } /// /// 获取任务 /// /// 堆垛机对象 /// private Dt_Task? GetTask(SpeStackerCrane speStackerCrane) { Dt_Task? task; if (speStackerCrane.LastTaskType == null) { task = _taskService.QueryStackerCraneTask(speStackerCrane.DeviceCode); } else { bool flag = speStackerCrane.LastTaskType == TaskTypeEnum.Inbound.ObjToInt() || speStackerCrane.LastTaskType == TaskTypeEnum.PalletInbound.ObjToInt(); if (flag == false) { task = _taskService.QueryStackerCraneInTask(speStackerCrane.DeviceCode); if (task == null) { task = _taskService.QueryStackerCraneOutTask(speStackerCrane.DeviceCode); } } else { task = _taskService.QueryStackerCraneOutTask(speStackerCrane.DeviceCode); if (task == null) { task = _taskService.QueryStackerCraneInTask(speStackerCrane.DeviceCode); } } } return task; } /// /// 出库任务判断出库站台是否被占用 /// /// 任务实体 /// 如果未被占用,返回传入的任务信息,否则,返回null private Dt_Task? OutTaskStationIsOccupied([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 { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); } return null; } /// /// 任务实体转换成命令Model /// /// 任务实体 /// /// public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task) { StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand(); stackerCraneTaskCommand.Barcode = task.PalletCode; stackerCraneTaskCommand.TaskNum = task.TaskNum; stackerCraneTaskCommand.TrayType = 0; if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())//判断是否是入库任务 { stackerCraneTaskCommand.WorkType = 1; if (task.SourceAddress != null && task.TargetAddress != null) { string[] sourceCodes = task.SourceAddress.Split("-"); if (sourceCodes.Length == 5) { stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]); stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]); stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.SourceAddress}】"); return null; } string[] targetCodes = task.TargetAddress.Split("-"); if (targetCodes.Length == 5) { stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]); stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]); stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.TargetAddress}】"); return null; } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress},{task.SourceAddress}】信息,无法获取对应的堆垛机取货站台信息"); return null; } } else if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletOutbound.ObjToInt()) { stackerCraneTaskCommand.WorkType = 2; if (task.SourceAddress != null && task.TargetAddress != null) { string[] targetCodes = task.TargetAddress.Split("-"); if (targetCodes.Length == 5) { stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]); stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]); stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.SourceAddress}】"); return null; } string[] sourceCodes = task.SourceAddress.Split("-"); if (sourceCodes.Length == 5) { stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]); stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]); stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); return null; } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息"); return null; } } else if (task.TaskType == TaskTypeEnum.Relocation.ObjToInt()) { stackerCraneTaskCommand.WorkType = 3; if (task.SourceAddress != null && task.TargetAddress != null) { string[] targetCodes = task.TargetAddress.Split("-"); if (targetCodes.Length == 5) { stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]); stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]); stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.TargetAddress}】"); return null; } string[] sourceCodes = task.SourceAddress.Split("-"); if (sourceCodes.Length == 5) { stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]); stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]); stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]); } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.SourceAddress}】"); return null; } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点终点错误,起点:【{task.SourceAddress}】,终点:【{task.TargetAddress}】"); } } return stackerCraneTaskCommand; } } }