using Mapster; using Microsoft.AspNetCore.Components.Routing; using Newtonsoft.Json; using Quartz; using System.Diagnostics.CodeAnalysis; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_BasicInfoRepository; using WIDESEAWCS_Common; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.Caches; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.HttpContextUser; using WIDESEAWCS_IProcessRepository; using WIDESEAWCS_ISystemServices; using WIDESEAWCS_ITaskInfo_HtyRepository; 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_SignalR; using WIDESEAWCS_Tasks.ConveyorLineJob; using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class SCJob : JobBase, IJob { private readonly ITaskService _taskService; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly ITaskRepository _taskRepository; private readonly IRouterService _routerService; private readonly IProcessRepository _processRepository; private readonly ICacheService _cacheService; private readonly INoticeService _noticeService; private readonly IDt_StationManagerRepository _stationManagerRepository; private readonly ITask_HtyRepository _htyRepository; private readonly ISys_ConfigService _sys_ConfigService; private static List? userTokenIds; private static List? userIds; public SCJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IProcessRepository processRepository, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerRepository stationManagerRepository, ITask_HtyRepository htyRepository, ISys_ConfigService sys_ConfigService) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _taskRepository = taskRepository; _routerService = routerService; _processRepository = processRepository; _cacheService = cacheService; _noticeService = noticeService; _stationManagerRepository = stationManagerRepository; _htyRepository = htyRepository; _sys_ConfigService = sys_ConfigService; } public Task Execute(IJobExecutionContext context) { try { bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value); if (flag && value != null && value is STK commonStackerCrane) { List childCodes = commonStackerCrane.DeviceProDTOs.GroupBy(d => d.DeviceChildCode).Select(g => g.Key).ToList(); //childCodes = new List { "SC06" }; //测试使用 Parallel.For(0, childCodes.Count, (i, state) => { //ConsoleHelper.WriteColorLine($"【{childCodes[i]}】时间【{DateTime.Now}】】【{Thread.CurrentThread.ManagedThreadId}】", ConsoleColor.Magenta); if (commonStackerCrane.GetValue(StackerCraneDBName.TaskState, childCodes[i]) == 2) { int taskNum = commonStackerCrane.GetValue(StackerCraneDBName.TaskNum, childCodes[i]); int workStatus = commonStackerCrane.GetValue(StackerCraneDBName.TaskState, childCodes[i]); bool RfidError = commonStackerCrane.GetValue(StackerCraneDBName.ForkTemp1, childCodes[i]); ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机作业状态:【{workStatus}】时间【{DateTime.Now}", ConsoleColor.Magenta); if (RfidError) { ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机作业异常:RFID异常;时间【{DateTime.Now}", ConsoleColor.Magenta); } string str = $"【{childCodes[i]}】任务完成,任务号:【{taskNum}】时间【{DateTime.Now}】"; WriteInfo(childCodes[i], str); ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue); var task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum); if (task == null) { commonStackerCrane.SetValue(StackerCraneDBName.CMD, 2, childCodes[i]); if (commonStackerCrane.GetValue(StackerCraneDBName.TaskState, childCodes[i]) == 0) { commonStackerCrane.SetValue(StackerCraneDBName.CMD, 0, childCodes[i]); } } else { var content = _taskService.StackCraneTaskCompleted(taskNum); var isWorkType = commonStackerCrane.SetValue(StackerCraneDBName.CMD, 2, childCodes[i]); if (commonStackerCrane.GetValue(StackerCraneDBName.TaskState, childCodes[i]) == 0) { Thread.Sleep(500); commonStackerCrane.SetValue(StackerCraneDBName.CMD, 0, childCodes[i]); } str = $"【{childCodes[i]}】WMS|WCS任务完成:【{content.Status}】,堆垛机完成信号写入:【{isWorkType}】,任务号:【{taskNum}】时间【{DateTime.Now}】"; WriteInfo(childCodes[i], str); ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue); } } if (commonStackerCrane.GetValue(StackerCraneDBName.Online, childCodes[i]) //在线 && commonStackerCrane.GetValue(StackerCraneDBName.Free, childCodes[i]) //空闲 && !commonStackerCrane.GetValue(StackerCraneDBName.Alarm, childCodes[i]) //无报警 && !commonStackerCrane.GetValue(StackerCraneDBName.ForkGoods, childCodes[i]) //载货台无货 //&& commonStackerCrane.GetValue(StackerCraneDBName.ForkFlag, childCodes[i]) //货叉启用 ) { Console.WriteLine($"当前堆垛机{childCodes[i]}"); Dt_Task? task = GetTask(commonStackerCrane, childCodes[i]); if (task != null) { //StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task); //if (stackerCraneTaskCommand != null) //{ var taskNum = commonStackerCrane.GetValue(StackerCraneDBName.TaskNo, childCodes[i]); //var taskBarCode = commonStackerCrane.GetValue(StackerCraneDBName.Barcode); ConsoleHelper.WriteColorLine($"【{childCodes[i]}】堆垛机任务号:【{taskNum}】任务任务号:【{task.TaskNum}】", ConsoleColor.DarkBlue); if (taskNum != task.TaskNum) { ConsoleHelper.WriteColorLine($"【{childCodes[i]}】任务号为【{0}】,任务号不一致可以下发任务", ConsoleColor.DarkBlue); Thread.Sleep(500); WebResponseContent responseContent = SendStackerTask(commonStackerCrane, task, childCodes[i]); if (responseContent.Status) { _taskService.UpdateTaskStatusToNext(task.TaskNum); } //StackerExecutor.SendStackerTask(commonStackerCrane, task, childCodes[i]); //bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand, childCodes[i]); //commonStackerCrane.LastTaskType = task.TaskType; } //} } } }); } } catch (Exception ex) { throw; } return Task.CompletedTask; } /// /// 获取任务 /// /// 堆垛机对象 /// private Dt_Task? GetTask(STK commonStackerCrane, string childCode) { Dt_Task task; task = _taskService.QueryExcutingTask(childCode); if (task != null) { ConsoleHelper.WriteErrorLine($"{childCode}堆垛机存在执行中的任务,请检查"); return null; } //task = _taskService.QueryRelocationTask(childCode); //if (task != null) //{ // return task; //} if (commonStackerCrane.LastTaskType == null) { task = _taskService.QueryStackerCraneTask(childCode); } else { var lastTaskTypeGroup = commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup(); if (lastTaskTypeGroup == TaskTypeGroup.OutbondGroup) { task = _taskService.QueryStackerCraneInTask(childCode); if (task == null) { task = _taskService.QueryStackerCraneOutTask(childCode); } } else { task = _taskService.QueryStackerCraneOutTask(childCode); } } if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { //var relocation = _taskService.OutBeforeCheck(task.TaskNum); //if (relocation != null) //{ // return relocation; //} // 检查当前出库任务站台是否允许放货 var occupiedStation = OutTaskStationIsOccupied(task); if (occupiedStation == null) { // 如果当前出库任务站台不允许放货,排除当前任务,查找其他出库任务 var log = $"任务号:【{task.TaskNum}】出库地址:【{task.NextAddress}】不允许放货"; ConsoleHelper.WriteErrorLine(log); _noticeService.Logs(userTokenIds, new { commonStackerCrane.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" }); WriteInfo(commonStackerCrane.DeviceName, log); task = _taskService.QueryStackerCraneInTask(childCode); } else { return task; } } else if (task == null) { task = _taskService.QueryStackerCraneInTask(childCode); } //if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) //{ // var relocation = _taskService.InBeforeCheck(task.TaskNum); // if (relocation != null) // { // return relocation; // } //} return task; } /// /// 出库任务判断出库站台是否被占用 /// /// 任务实体 /// 如果未被占用,返回传入的任务信息,否则,返回null private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task) { //return task; //var stationList = _stationManagerRepository.QueryData(x => x.stationType == 2 && x.Roadway == task.Roadway); //foreach (var item in stationList) //{ // IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == item.stationPLC); // if (device != null) // { // CommonConveyorLineTZ conveyorLine = (CommonConveyorLineTZ)device; // if (conveyorLine.IsOccupied(item.stationChildCode))//出库站台未被占用 // { // return task; // } // } // else // { // _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{item.stationChildCode}】对应的通讯对象,无法判断出库站台是否被占用"); // } //} 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) { CommonConveyorLineTZ conveyorLine = (CommonConveyorLineTZ)device; if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用 { return task; } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用"); } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台"); } return null; } public WebResponseContent SendStackerTask(STK commonStackerCrane, Dt_Task task, string scName) { WebResponseContent responseContent = new WebResponseContent(); try { if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务 { List routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway); if (routers.Count > 0) { commonStackerCrane.SetValue(StackerCraneDBName.S_Row, Convert.ToInt16(routers.FirstOrDefault().SrmRow), scName);//来源货位行 commonStackerCrane.SetValue(StackerCraneDBName.S_Bay, Convert.ToInt16(routers.FirstOrDefault().SrmColumn), scName);//来源货位列 commonStackerCrane.SetValue(StackerCraneDBName.S_level, Convert.ToInt16(routers.FirstOrDefault().SrmLayer), scName);//来源货位层 string[] targetCodes = task.NextAddress.Split("-"); if (targetCodes.Length == 3) { commonStackerCrane.SetValue(StackerCraneDBName.E_Row, Convert.ToInt16(targetCodes[0]), scName);//目的货位行 commonStackerCrane.SetValue(StackerCraneDBName.E_Bay, Convert.ToInt16(targetCodes[1]), scName);//目的货位列 commonStackerCrane.SetValue(StackerCraneDBName.E_level, Convert.ToInt16(targetCodes[2]), scName);//目的货位层 } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】"); return responseContent.Error(); } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息"); return responseContent.Error(); } } else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { var OutStation = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress); //List routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress); if (OutStation != null) { string[] targetCodes = OutStation.stationLocation.Split("-"); commonStackerCrane.SetValue(StackerCraneDBName.E_Row, Convert.ToInt16(targetCodes[0]), scName);//目的货位行 commonStackerCrane.SetValue(StackerCraneDBName.E_Bay, Convert.ToInt16(targetCodes[1]), scName);//目的货位列 commonStackerCrane.SetValue(StackerCraneDBName.E_level, Convert.ToInt16(targetCodes[2]), scName);//目的货位层 string[] sourceCodes = task.CurrentAddress.Split("-"); if (sourceCodes.Length == 3) { commonStackerCrane.SetValue(StackerCraneDBName.S_Row, Convert.ToInt16(sourceCodes[0]), scName);//来源货位行 commonStackerCrane.SetValue(StackerCraneDBName.S_Bay, Convert.ToInt16(sourceCodes[1]), scName);//来源货位列 commonStackerCrane.SetValue(StackerCraneDBName.S_level, Convert.ToInt16(sourceCodes[2]), scName);//来源货位层 } else { //数据配置错误 _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); return null; } } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息"); return null; } } commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 1, scName);//工作命令代码 commonStackerCrane.SetValue(StackerCraneDBName.TaskNum, task.TaskNum, scName);//工作序号 commonStackerCrane.SetValue(StackerCraneDBName.RFID, task.PalletCode.PadLeft(4, '0') + task.PVI, scName);//pvi //写入后 读取验证 commonStackerCrane.SetValue(StackerCraneDBName.CMD, 1, scName); return responseContent.OK(); } catch (Exception ex) { return responseContent.Error(ex.Message); } } } }