#region << 版 本 注 释 >> /*---------------------------------------------------------------- * 命名空间:WIDESEAWCS_Tasks.ConveyorLineJob * 创建者:胡童庆 * 创建时间:2024/8/2 16:13:36 * 版本:V1.0.0 * 描述: * * ---------------------------------------------------------------- * 修改人: * 修改时间: * 版本:V1.0.1 * 修改说明: * *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using AutoMapper; using HslCommunication; using Newtonsoft.Json; using Quartz; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_BasicInfoRepository; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Core.Caches; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.HttpContextUser; using WIDESEAWCS_IBasicInfoRepository; using WIDESEAWCS_IBasicInfoService; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.BasicInfo; 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_SignalR; using WIDESEAWCS_Tasks.ConveyorLineJob; using WIDESEAWCS_Tasks.RGVJob; using WIDESEAWCS_Tasks.StackerCraneJob; using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; namespace WIDESEAWCS_Tasks { [DisallowConcurrentExecution] public class CommonRGVJob :JobBase, IJob { private readonly ITaskService _taskService; private readonly ITaskRepository _taskRepository; private readonly ITaskExecuteDetailService _taskExecuteDetailService; private readonly IRouterService _routerService; private readonly IDt_StationManagerService _stationManagerService; private readonly IDt_StationManagerRepository _stationManagerRepository; private readonly IMapper _mapper; private readonly ICacheService _cacheService; private readonly INoticeService _noticeService; private static List? userTokenIds; private static List? userIds; public CommonRGVJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IDt_StationManagerService stationManagerService, IMapper mapper, ICacheService cacheService, INoticeService noticeService, ITaskRepository taskRepository,IDt_StationManagerRepository stationManagerRepository) { _taskService = taskService; _taskExecuteDetailService = taskExecuteDetailService; _routerService = routerService; _stationManagerService = stationManagerService; _mapper = mapper; _cacheService = cacheService; _noticeService = noticeService; _taskRepository = taskRepository; _stationManagerRepository = stationManagerRepository; } public Task Execute(IJobExecutionContext context) { try { CommonRGV commonRGV = (CommonRGV)context.JobDetail.JobDataMap.Get("JobParams"); if (commonRGV != null) { //if (!commonRGV.IsEventSubscribed) //{ // commonRGV.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件 //} //commonRGV.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次 if (commonRGV.GetValue(StackerCraneDBName.WorkCompleted)) { var x = commonRGV.GetValue(RGVDBName.RGVTaskNum); RGVTaskFinish(commonRGV, commonRGV.GetValue(RGVDBName.RGVTaskNum)); } if (commonRGV.GetValue(RGVDBName.Automatic) && !commonRGV.GetValue(RGVDBName.Fault) && !commonRGV.GetValue(RGVDBName.Running)) { Dt_Task? task = GetTask(commonRGV, commonRGV.GetValue(RGVDBName.InStock)); if (task != null) { RGVTaskCommand? rgvTaskCommand = ConvertToRGVTaskCommand(task); if (rgvTaskCommand != null) { Thread.Sleep(1000); bool sendFlag = SendCommand(commonRGV,rgvTaskCommand); if (sendFlag) { _taskService.UpdateTaskStatusToNext(task.TaskNum); } } } } #region 调用事件总线通知前端 var tokenInfos = _cacheService.Get>("Cache_UserToken"); if (tokenInfos == null || !tokenInfos.Any()) { return Task.CompletedTask; } var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList(); var userIds = tokenInfos?.Select(x => x.UserId).ToList(); object obj = new { Automatic = commonRGV.GetValue(StackerCraneDBName.Automatic), Fault = commonRGV.GetValue(StackerCraneDBName.Fault), Running = commonRGV.GetValue(StackerCraneDBName.Running), //commonRGV.StackerCraneAutoStatusDes, //commonRGV.StackerCraneWorkStatusDes, //commonRGV.DeviceCode, //commonRGV.DeviceName, //commonRGV.CurrentTaskNum, //commonRGV.LastTaskNum, }; _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonRGV.DeviceName, data = obj }); #endregion 调用事件总线通知前端 } } catch (Exception ex) { WriteError("CommonStackerStationCraneJob", "test", ex); } return Task.CompletedTask; } /// /// 任务完成事件订阅的方法 /// /// /// private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e) { CommonRGV? commonRGV = sender as CommonRGV; } public void RGVTaskFinish(CommonRGV commonRGV, int TaskNum) { if (commonRGV != null) { #region 日志记录 ConsoleHelper.WriteColorLine($"【{commonRGV.DeviceName}】堆垛机作业状态:【】时间【{DateTime.Now}】", ConsoleColor.Magenta); string str = $"【{commonRGV.DeviceName}】任务完成,任务号:【{TaskNum}】时间【{DateTime.Now}】"; WriteInfo(commonRGV.DeviceName, str); ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue); #endregion var task = _taskRepository.QueryFirst(x => x.TaskNum == TaskNum); if (task != null) { if (task.TaskState == (int)TaskInStatusEnum.RGV_Indispatch) { Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.SourceAddress); if (stationManager != null) { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC); if (device != null) { CommonConveyorLine conveyorLine = (CommonConveyorLine)device; Thread.Sleep(100); var Barcode = conveyorLine.GetValue(ConveyorLineDBName.ConveyorLineBarcode, stationManager.stationChildCode); if (Barcode == null) { return; } var content = _taskService.RGVTaskCompleted(TaskNum, Barcode); } } } else { var content = _taskService.RGVTaskCompleted(TaskNum); } } } } /// /// 获取任务 /// /// 堆垛机对象 /// private Dt_Task? GetTask(CommonRGV commonRGV, bool InStock) { Dt_Task task; if (InStock) { task = _taskService.QueryRGVExecutingTask(commonRGV.DeviceCode); if (task != null) { return task; } } if (commonRGV.LastTaskType == null) { task = _taskService.QueryRGVTask(commonRGV.DeviceCode); } else { if (commonRGV.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { task = _taskService.QueryRGVInTask(commonRGV.DeviceCode); if (task == null) { task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode); } } else { task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode); } } if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { if (task == null) { task = _taskService.QueryRGVInTask(commonRGV.DeviceCode); } else { return task; } } else if (task == null) { task = _taskService.QueryRGVInTask(commonRGV.DeviceCode); } return task; } /// /// 任务实体转换成命令Model /// /// 任务实体 /// /// public RGVTaskCommand? ConvertToRGVTaskCommand([NotNull] Dt_Task task) { RGVTaskCommand rgvTaskCommand = new RGVTaskCommand(); rgvTaskCommand.TaskNum = Convert.ToInt16(task.TaskNum); if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务 { //rgvTaskCommand.TaskType = (byte)Convert.ToSByte(16); //rgvTaskCommand.TaskType = (byte)Convert.ToSByte(64); 退回 if (task.TaskState == (int)TaskInStatusEnum.InNew) { Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.SourceAddress); rgvTaskCommand.TaskType = (byte)Convert.ToSByte(3); rgvTaskCommand.PickupLocation = (byte)Convert.ToSByte(stationManager.stationRemark); } else if (task.TaskState == (int)TaskInStatusEnum.RGV_IndispatchFinish) { Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress); rgvTaskCommand.TaskType = (byte)Convert.ToSByte(4); rgvTaskCommand.PutcargoLocation = (byte)Convert.ToSByte(stationManager.stationRemark); } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息"); return null; } } else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) { if (task.TaskState == (int)TaskOutStatusEnum.Line_OutFinish) { Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.CurrentAddress); rgvTaskCommand.TaskType = (byte)Convert.ToSByte(3); rgvTaskCommand.PickupLocation = (byte)Convert.ToSByte(stationManager.stationRemark); } else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutdispatchFinish) { Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.NextAddress); rgvTaskCommand.TaskType = (byte)Convert.ToSByte(4); rgvTaskCommand.PutcargoLocation = (byte)Convert.ToSByte(stationManager.stationRemark); } else { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息"); return null; } } return rgvTaskCommand; } public bool SendCommand(CommonRGV commonRGV, RGVTaskCommand? rgvTaskCommand) { commonRGV.SetValue(StackerCraneDBName.TaskNum, rgvTaskCommand.TaskNum); Thread.Sleep(100); commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, rgvTaskCommand.PutcargoLocation); Thread.Sleep(100); commonRGV.SetValue(StackerCraneDBName.PickupLocation, rgvTaskCommand.PickupLocation); Thread.Sleep(100); commonRGV.SetValue(StackerCraneDBName.TaskType,Convert.ToSByte(rgvTaskCommand.TaskType)); for (int i = 0; i < 6; i++) { if (commonRGV.GetValue(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum && commonRGV.GetValue(StackerCraneDBName.TaskType) == rgvTaskCommand.TaskType && commonRGV.GetValue(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation && commonRGV.GetValue(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation) { WriteInfo(commonRGV.DeviceName, $"写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(rgvTaskCommand)}】"); return true; } if (commonRGV.GetValue(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum) { commonRGV.SetValue(StackerCraneDBName.TaskNum, rgvTaskCommand.TaskNum); Thread.Sleep(100); } if (commonRGV.GetValue(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation) { commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, rgvTaskCommand.PutcargoLocation); } if (commonRGV.GetValue(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation) { commonRGV.SetValue(StackerCraneDBName.PickupLocation, rgvTaskCommand.PickupLocation); } if (commonRGV.GetValue(StackerCraneDBName.TaskType) == rgvTaskCommand.TaskType) { commonRGV.SetValue(StackerCraneDBName.TaskType, rgvTaskCommand.TaskType); } } WriteInfo(commonRGV.DeviceName, $"RGV任务写入失败【{JsonConvert.SerializeObject(rgvTaskCommand)}】"); return false; } public async Task LogAndWarn(string deviceName, string log, string color = "red") { ConsoleHelper.WriteWarningLine(log); await _noticeService.Logs(userTokenIds, new { deviceName, log = log, time = DateTime.Now.ToString("G"), color = color }); WriteInfo(deviceName, log); } } }