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.Net.Http.Json; using System.Net.NetworkInformation; using System.Reflection; using System.Reflection.Metadata; using System.Reflection.PortableExecutable; using System.Text; using System.Text.Json.Nodes; using System.Threading.Tasks; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Caches; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; using WIDESEAWCS_QuartzJob.DeviceBase; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_TaskInfoService; using WIDESEAWCS_Tasks.StackerCraneJob; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WIDESEAWCS_QuartzJob.StackerCrane; using WIDESEAWCS_Core.BaseRepository; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public partial class CommonStackerCraneJob : JobBase, IJob { private readonly ITaskService _taskService; private readonly IRouterService _routerService; private readonly ICacheService _cacheService; private readonly IRepository _routerRepository; private readonly IRepository _stationMangerRepository; private readonly IRepository _taskRepository; public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, ICacheService cacheService, IRepository routerRepository, IRepository stationMangerRepository, IRepository taskRepository) { _taskService = taskService; _routerService = routerService; _cacheService = cacheService; _routerService = routerService; _stationMangerRepository = stationMangerRepository; _taskRepository = taskRepository; } 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.Normal) { Dt_Task? task = GetTasks(speStackerCrane); if (task != null) { bool sendFlag = true; if (sendFlag) { StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); if (stackerCraneTaskCommand != null) { sendFlag = getStackerObject.SendCommand(stackerCraneTaskCommand); if (sendFlag) { speStackerCrane.SetValue(StackerCraneDBName.CommandSend, 1);//启动命令 speStackerCrane.LastTaskType = task.TaskType; task.Dispatchertime = DateTime.Now; task.ExceptionMessage = ""; _taskService.UpdateTask(task, TaskStatusEnum.SC_Executing); //延时1s Thread.Sleep(1000); } } } } } } } catch (Exception) { throw; } return Task.CompletedTask; } private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e) { SpeStackerCrane? speStackerCrane = sender as SpeStackerCrane; if (speStackerCrane != null) { if (speStackerCrane.GetValue(StackerCraneDBName.WorkType) != 5) { StackerCraneTaskCompleted(e.TaskNum, speStackerCrane.DeviceCode); speStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); } } } public WebResponseContent StackerCraneTaskCompleted(int taskNum, string deviceCode) { try { Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum); if (task != null) { if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StackerCraneStationCode == task.NextAddress && x.StackerCraneCode == deviceCode); if (stationManger == null) { //_taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"输送线出库站点未配置,{task.NextAddress}"); _taskService.UpdateTaskExceptionMessage(taskNum, $"输送线出库站点未配置,{task.NextAddress}"); WriteError(deviceCode, $"输送线出库站点未配置,{task.NextAddress}"); return WebResponseContent.Instance.Error($"输送线出库站点未配置,{task.NextAddress}"); } Dt_Router router = _routerRepository.QueryFirst(x => x.InOutType == task.TaskType && x.StartPosi == stationManger.StationCode); if (router == null) { router = _routerRepository.QueryFirst(x => x.ChildPosi == deviceCode && x.ChildPosiDeviceCode == stationManger.StationDeviceCode && x.NextPosi == stationManger.StationCode && x.InOutType == task.TaskType && x.IsEnd); if (router != null && router.IsEnd) { _taskService.TaskCompleted(taskNum); } else { _taskService.UpdateTaskExceptionMessage(taskNum, $"未找到路由信息,{task.NextAddress}"); WriteError(deviceCode, $"未找到路由信息,{task.NextAddress}"); return WebResponseContent.Instance.Error($"未找到路由信息,{task.NextAddress}"); } } if (task.TargetAddress.Contains("1030") || task.TargetAddress.Contains("1026")) //自动一线 { _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: router.NextPosi); } } else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) { _taskService.TaskCompleted(taskNum); } else { WriteError(deviceCode, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}"); _taskService.UpdateTaskExceptionMessage(taskNum, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}"); } } else { WriteError(deviceCode, $"未找到任务信息,任务号:{taskNum}"); return WebResponseContent.Instance.Error($"未找到任务信息,任务号:{taskNum}"); } return WebResponseContent.Instance.OK(); } catch (Exception ex) { WriteError(deviceCode, $"任务完成错误", ex); return WebResponseContent.Instance.Error(ex.Message); } } /// /// 获取任务 /// /// /// private Dt_Task? GetTasks(SpeStackerCrane commonStackerCrane) { Dt_Task? task; 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 otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList(); List tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); foreach (var item in tasks) { if (OutTaskStationIsOccupied(task) != null) { return task; } } task = _taskService.QueryStackerCraneInTask(commonStackerCrane.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.WorkType = 1; stackerCraneTaskCommand.TrayType = 1; if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务 { string[] startCodes = task.SourceAddress.Split("-"); if (startCodes.Length == 3) { stackerCraneTaskCommand.StartRow = Convert.ToInt16(startCodes[0]); stackerCraneTaskCommand.StartColumn = Convert.ToInt16(startCodes[1]); stackerCraneTaskCommand.StartLayer = Convert.ToInt16(startCodes[2]); } 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.NextAddress}】"); return null; } } else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { string[] targetCodes = task.TargetAddress.Split("-"); stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]); stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]); stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]); string[] sourceCodes = task.SourceAddress.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 if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) { 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; } } return stackerCraneTaskCommand; } } }