| using Microsoft.AspNetCore.Components.Routing; | 
| using Newtonsoft.Json; | 
| using Quartz; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Diagnostics.CodeAnalysis; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Common.APIEnum; | 
| using WIDESEAWCS_Common.TaskEnum; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.Caches; | 
| using WIDESEAWCS_Core.Enums; | 
| using WIDESEAWCS_Core.Helper; | 
| using WIDESEAWCS_IBasicInfoRepository; | 
| 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.Enum; | 
| using WIDESEAWCS_Tasks.HoisterJob; | 
| using WIDESEAWCS_Tasks.StackerCraneJob; | 
|   | 
| namespace WIDESEAWCS_Tasks | 
| { | 
|     /// <summary> | 
|     /// 油墨仓堆垛机 | 
|     /// </summary> | 
|     [DisallowConcurrentExecution] | 
|     public class StackerCraneJob_YM : JobBase, IJob | 
|     { | 
|         private readonly ICacheService _cacheService; | 
|         private readonly ITaskService _taskService; | 
|         private readonly ITaskExecuteDetailService _taskExecuteDetailService; | 
|         private readonly ITaskRepository _taskRepository; | 
|         private readonly IRouterService _routerService; | 
|         private readonly IStationMangerRepository _stationMangerRepository; | 
|   | 
|         public StackerCraneJob_YM(ITaskService taskService, ICacheService cacheService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository) | 
|         { | 
|             _cacheService = cacheService; | 
|             _taskService = taskService; | 
|             _taskExecuteDetailService = taskExecuteDetailService; | 
|             _taskRepository = taskRepository; | 
|             _routerService = routerService; | 
|             _stationMangerRepository = stationMangerRepository; | 
|         } | 
|   | 
|         public Task Execute(IJobExecutionContext context) | 
|         { | 
|             bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object value); | 
|             if (flag && value != null) | 
|             { | 
|                 CommonStackerCrane commonStackerCrane = (CommonStackerCrane)value; | 
|                 try | 
|                 { | 
|                     if (commonStackerCrane != null) | 
|                     { | 
|                         if (!commonStackerCrane.IsEventSubscribed) | 
|                         { | 
|                             commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件 | 
|                         } | 
|                         commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次 | 
|                         if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) | 
|                         { | 
|                             Thread.Sleep(1000); | 
|                             Dt_Task? task = GetTask(commonStackerCrane); | 
|                             if (task != null) | 
|                             { | 
|                                 StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); | 
|                                 if (stackerCraneTaskCommand != null) | 
|                                 { | 
|                                     bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand); | 
|                                     if (sendFlag) | 
|                                     { | 
|                                         commonStackerCrane.LastTaskType = task.TaskType; | 
|                                         task.Dispatchertime = DateTime.Now; | 
|                                         task.ExceptionMessage = ""; | 
|                                         _taskService.UpdateTask(task, TaskStatusEnum.SC_Executing); | 
|                                         //延时1s | 
|                                         Thread.Sleep(1000); | 
|                                     } | 
|                                 } | 
|                             } | 
|                         } | 
|                     } | 
|                 } | 
|                 catch (Exception ex) | 
|                 { | 
|                     WriteError(commonStackerCrane.DeviceName, ex.Message, ex); | 
|                 } | 
|             }  | 
|   | 
|              | 
|             return Task.CompletedTask; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 任务完成事件订阅的方法 | 
|         /// </summary> | 
|         /// <param name="sender"></param> | 
|         /// <param name="e"></param> | 
|         private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e) | 
|         { | 
|             CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane; | 
|             try | 
|             { | 
|                 if (commonStackerCrane != null) | 
|                 { | 
|                     if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5) | 
|                     { | 
|                         WriteDebug(commonStackerCrane.DeviceName, $"读取到任务完成信号,{e.TaskNum}"); | 
|                         Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == e.TaskNum); | 
|                         if (task != null) | 
|                         { | 
|                             _taskService.TaskCompleted(e.TaskNum); | 
|                         } | 
|                         else | 
|                         { | 
|                             WriteError(commonStackerCrane.DeviceName, $"读取到任务完成信号,未找到对应的任务信息,{e.TaskNum}"); | 
|                         } | 
|                         commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); | 
|                     } | 
|                 } | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 WriteError(commonStackerCrane?.DeviceCode ?? nameof(StackerCraneJob_YM), ex.Message, ex); | 
|             } | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取任务 | 
|         /// </summary> | 
|         /// <param name="commonStackerCrane">堆垛机对象</param> | 
|         /// <returns></returns> | 
|         private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane) | 
|         { | 
|             Dt_Task task; | 
|             if (commonStackerCrane.LastTaskType == null) | 
|             { | 
|                 task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode); | 
|             } | 
|             else | 
|             { | 
|                 if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|                 { | 
|                     task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); | 
|                     if (task == null) | 
|                     { | 
|                         task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode); | 
|                     } | 
|                 } | 
|                 else | 
|                 { | 
|                     task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode); | 
|                     if (task == null) | 
|                     { | 
|                         task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); | 
|                     } | 
|                 } | 
|             } | 
|   | 
|             if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|             { | 
|                 if (OutTaskStationIsOccupied(task) != null || true) | 
|                 { | 
|                     return task; | 
|                 } | 
|                 else | 
|                 { | 
|                     List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress,task.TaskType).Select(x => x.ChildPosi).ToList(); | 
|                     List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); | 
|                     foreach (var item in tasks) | 
|                     { | 
|                         if (OutTaskStationIsOccupied(task) != null) | 
|                         { | 
|                             return task; | 
|                         } | 
|                     } | 
|                     task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); | 
|                 } | 
|             } | 
|   | 
|             return task; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 出库任务判断出库站台是否被占用 | 
|         /// </summary> | 
|         /// <param name="task">任务实体</param> | 
|         /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns> | 
|         private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task) | 
|         { | 
|             Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.TargetAddress && x.StackerCraneCode == task.DeviceCode); | 
|             if (stationManger != null) | 
|             { | 
|                 IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); | 
|                 if (device != null) | 
|                 { | 
|                     OtherDevice client = (OtherDevice)device; | 
|                     if (client.GetValue<HoisterDBName, bool>(HoisterDBName.Tray, stationManger.StationCode))//出库站台未被占用 | 
|                     { | 
|                         task.NextAddress = stationManger.StackerCraneStationCode; | 
|                         _taskRepository.UpdateData(task); | 
|                         return task; | 
|                     } | 
|                 } | 
|                 else | 
|                 { | 
|                     _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{stationManger.StationDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); | 
|             } | 
|             return null; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 任务实体转换成命令Model | 
|         /// </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 = 1; | 
|             if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务 | 
|             { | 
|                 string[] startCodes = task.CurrentAddress.Split("-"); | 
|                 if (startCodes.Length == 3) | 
|                 { | 
|                     stackerCraneTaskCommand.StartRow = Convert.ToInt16(startCodes[0]); | 
|                     stackerCraneTaskCommand.StartColumn = Convert.ToInt16(startCodes[1]); | 
|                     stackerCraneTaskCommand.StartLayer = Convert.ToInt16(startCodes[2]); | 
|                 } | 
|                 else | 
|                 { | 
|                     _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务起点错误,起点:【{task.CurrentAddress}】"); | 
|                     WriteError(task.DeviceCode, $"入库任务起点错误,起点:【{task.CurrentAddress}】"); | 
|                     return null; | 
|                 } | 
|   | 
|                 string[] targetCodes = task.NextAddress.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.NextAddress}】"); | 
|                     WriteError(task.DeviceCode, $"入库任务终点错误,终点:【{task.NextAddress}】"); | 
|                     return null; | 
|                 } | 
|             } | 
|             else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
|             { | 
|                 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}】"); | 
|                     WriteError(task.DeviceCode, $"出库任务终点错误,终点:【{task.NextAddress}】"); | 
|                     return null; | 
|                 } | 
|   | 
|                 string[] sourceCodes = task.CurrentAddress.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}】"); | 
|                     WriteError(task.DeviceCode, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); | 
|                     return null; | 
|                 } | 
|             } | 
|             else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) | 
|             { | 
|                 string[] targetCodes = task.NextAddress.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.NextAddress}】"); | 
|                     WriteError(task.DeviceCode, $"移库任务终点错误,终点:【{task.NextAddress}】"); | 
|                     return null; | 
|                 } | 
|                 string[] sourceCodes = task.CurrentAddress.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}】"); | 
|                     WriteError(task.DeviceCode, $"移库任务起点错误,起点:【{task.CurrentAddress}】"); | 
|                     return null; | 
|                 } | 
|             } | 
|             return stackerCraneTaskCommand; | 
|         } | 
|     } | 
| } |