using Microsoft.AspNetCore.Components.Routing; 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.TaskEnum; using WIDESEAWCS_Core.Enums; 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; using WIDESEAWCS_Tasks; using WIDESEAWCS_Common.APIEnum; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Caches; using Newtonsoft.Json; using WIDESEAWCS_TaskInfoService; using WIDESEAWCS_Tasks.ConveyorLineJob; using WIDESEAWCS_QuartzJob.Repository; using Autofac.Core; using WIDESEAWCS_DTO.TaskInfo; using AutoMapper; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class StackerCraneJob_BC : JobBase, IJob { private readonly IMapper _mapper; private readonly ICacheService _cacheService; private readonly ITaskService _taskService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly ITaskRepository _taskRepository; private readonly IRouterService _routerService; private readonly IStationMangerRepository _stationMangerRepository; private readonly IRouterRepository _routerRepository; private List apiInfos; private readonly List warehouseDevices; public StackerCraneJob_BC(IMapper mapper, ITaskService taskService, ICacheService cacheService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository, IRouterRepository routerRepository) { _mapper = mapper; _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _taskRepository = taskRepository; _routerService = routerService; _stationMangerRepository = stationMangerRepository; _cacheService = cacheService; _routerRepository = routerRepository; string? apiInfoStr = _cacheService.Get("apiInfos"); if (!string.IsNullOrEmpty(apiInfoStr)) { List? infos = JsonConvert.DeserializeObject>(apiInfoStr); if (infos == null || infos.Count == 0) { apiInfos = new List(); } else { apiInfos = infos; } } string? warehouseDevicesStr = _cacheService.Get(nameof(Dt_WarehouseDevice)); if (!string.IsNullOrEmpty(warehouseDevicesStr)) { warehouseDevices = JsonConvert.DeserializeObject>(warehouseDevicesStr) ?? new List(); } else { warehouseDevices = new List(); } } public Task Execute(IJobExecutionContext context) { try { //Console.WriteLine(DateTime.Now); CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams"); if (commonStackerCrane != null) { Dt_WarehouseDevice? warehouseDevice = warehouseDevices.FirstOrDefault(x => x.DeviceCode == commonStackerCrane.DeviceCode); if (warehouseDevice == null) { WriteError(commonStackerCrane.DeviceName, $"请配置仓库设备信息"); return Task.CompletedTask; } if (!commonStackerCrane.IsEventSubscribed) { commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件 } commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次 if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) { 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(nameof(CommonStackerCraneJob), ex.Message, ex); } return Task.CompletedTask; } /// /// 任务完成事件订阅的方法 /// /// /// private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e) { try { CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane; if (commonStackerCrane != null) { //if (commonStackerCrane.GetValue(StackerCraneDBName.WorkType) != 5) { if (e.TaskNum != 0) { WriteDebug(commonStackerCrane.DeviceCode, $"读取到任务完成,任务号:{e.TaskNum}"); StackerCraneTaskCompleted(e.TaskNum, commonStackerCrane.DeviceCode); commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5); } } } } catch (Exception ex) { WriteError(nameof(StackerCraneJob_BC), "任务完成失败", ex); } } 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); 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("SCUTL-001")) //自动一线 { _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: router.NextPosi, targetAddress: router.NextPosi); } else if (task.TargetAddress.Contains("SCUTL-002")) //自动二线 { _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: "1103", targetAddress: "1103"); } else //手动线 { _taskService.UpdateTask(task, TaskStatusEnum.Line_Execute, deviceCode: stationManger.StationDeviceCode, currentAddress: stationManger.StationCode, nextAddress: "1102", targetAddress: "1102"); } } else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup) { _taskService.TaskCompleted(taskNum); } else { WriteError(deviceCode, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}"); _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"未找到该任务类型回调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? GetTask(CommonStackerCrane commonStackerCrane) { try { //Dt_Task oldTask = _taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.TaskState == TaskStatusEnum.SC_Executing.ObjToInt()); //if (oldTask != null) //{ // StackerCraneTaskCompleted(oldTask.TaskNum, commonStackerCrane.DeviceCode); //} } catch { } if (_taskRepository.QueryFirst(x => x.DeviceCode == commonStackerCrane.DeviceCode && x.TaskState==TaskStatusEnum.SC_Executing.ObjToInt())!=null) { return null; } WriteDebug(commonStackerCrane.DeviceCode, $"读取到当前任务号:{commonStackerCrane.CurrentTaskNum}"); 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) { bool flag = false; List otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress,task.TaskType).Select(x => x.ChildPosi).ToList(); List tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes); foreach (var item in tasks) { if (OutTaskStationIsOccupied(task) != null) { flag = true; break; } } if (!flag) { task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode); } } } if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { string? url = apiInfos.FirstOrDefault(x => x.ApiCode == APIEnum.WMSIsReloaction.ToString())?.ApiAddress; if (string.IsNullOrEmpty(url)) { _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"未找到WMS移库判断接口"); WriteError(commonStackerCrane.DeviceCode, $"未找到WMS移库判断接口"); _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到WMS移库判断接口"); return null; } string response = HttpHelper.Post($"{url}?taskNum={task.TaskNum}&locationCode={task.CurrentAddress}", "");//todo 调用WMS任务完成方法 if (string.IsNullOrEmpty(response)) { _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"移库接口调用错误"); WriteError(commonStackerCrane.DeviceCode, $"移库接口调用错误"); _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库接口调用错误"); return null; } WebResponseContent? responseContent = JsonConvert.DeserializeObject(response); if (responseContent == null || !responseContent.Status) { _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"移库接口调用错误"); WriteError(commonStackerCrane.DeviceCode, $"移库接口调用错误"); _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库接口调用错误"); return null; } WMSTaskDTO? taskDTO = JsonConvert.DeserializeObject(responseContent.Data.Serialize()); if (taskDTO == null) { _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"移库接口调用错误"); WriteError(commonStackerCrane.DeviceCode, $"移库接口调用错误"); _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库接口调用错误"); return null; } if (task.TaskNum == taskDTO.TaskNum) { return task; } else { Dt_Task reloTask = _mapper.Map(taskDTO); //判断移库货位任务是否已存在,如存在先执行 Dt_Task existTask = _taskService.QueryStackerExistTask(reloTask.PalletCode, reloTask.SourceAddress); if (existTask != null) { return existTask; } else { reloTask.TaskState = TaskStatusEnum.SC_Execute.ObjToInt(); reloTask.CurrentAddress = taskDTO.SourceAddress; reloTask.NextAddress = taskDTO.TargetAddress; reloTask.DeviceCode = task.DeviceCode; reloTask.TaskType = TaskTypeEnum.Relocation.ObjToInt(); int taskId = _taskRepository.AddData(reloTask); reloTask.TaskId = taskId; } return reloTask; } } return task; } /// /// 出库任务判断出库站台是否被占用 /// /// 任务实体 /// 如果未被占用,返回传入的任务信息,否则,返回null private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task) { Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => (x.StationCode == task.NextAddress || x.StackerCraneStationCode == task.NextAddress) && 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(R_ConveyorLineDB.Goods, stationManger.StationCode))//出库站台未被占用 { task.NextAddress = stationManger.StackerCraneStationCode; _taskRepository.UpdateData(task); return task; } } else { WriteError(task.DeviceCode, $"未找到出库站台【{stationManger.StationDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{stationManger.StationDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); } } else { WriteError(task.DeviceCode, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); _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 = (short)task.PalletType; 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; } } }