huangxiaoqiang
昨天 2d9272bdcdbdbca81396a61493e4ef6a822dcf4a
ÏîÄ¿´úÂë/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RGVJob/CommonRGVJob.cs
@@ -19,12 +19,16 @@
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;
@@ -33,9 +37,10 @@
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.RGV;
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;
@@ -50,13 +55,14 @@
        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<string>? userTokenIds;
        private static List<int>? userIds;
        public CommonRGVJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IDt_StationManagerService stationManagerService, IMapper mapper, ICacheService cacheService, INoticeService noticeService, ITaskRepository taskRepository)
        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;
@@ -66,6 +72,7 @@
            _cacheService = cacheService;
            _noticeService = noticeService;
            _taskRepository = taskRepository;
            _stationManagerRepository = stationManagerRepository;
        }
        public Task Execute(IJobExecutionContext context)
@@ -75,25 +82,23 @@
                CommonRGV commonRGV = (CommonRGV)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonRGV != null)
                {
                    if (!commonRGV.IsEventSubscribed)
                    if (commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.WorkCompleted))
                    {
                        commonRGV.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                        var x = commonRGV.GetValue<RGVDBName, short>(RGVDBName.RGVTaskNum);
                        RGVTaskFinish(commonRGV, commonRGV.GetValue<RGVDBName, short>(RGVDBName.RGVTaskNum));
                    }
                    commonRGV.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
                    if (commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Automatic) && !commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Fault) && !commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Running))
                    if (commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Automatic) && !commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Fault) && !commonRGV.GetValue<RGVDBName, bool>(RGVDBName.Running))
                    {
                        Dt_Task? task = GetTask(commonRGV);
                        Dt_Task? task = GetTask(commonRGV, commonRGV.GetValue<RGVDBName, bool>(RGVDBName.InStock));
                        if (task != null)
                        {
                            var taskCommand =  _mapper.Map<RGVTaskCommandWrite>(task);
                            if (taskCommand != null)
                            RGVTaskCommand? rgvTaskCommand = ConvertToRGVTaskCommand(task);
                            if (rgvTaskCommand != null)
                            {
                                Thread.Sleep(1000);
                                bool sendFlag = commonRGV.SendCommand(taskCommand);
                                bool sendFlag = SendCommand(commonRGV,rgvTaskCommand);
                                if (sendFlag)
                                {
                                    _taskService.UpdateTaskStatusToNext(task.TaskNum);
@@ -117,12 +122,12 @@
                        Automatic = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Automatic),
                        Fault = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Fault),
                        Running = commonRGV.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Running),
                        commonRGV.StackerCraneAutoStatusDes,
                        commonRGV.StackerCraneWorkStatusDes,
                        commonRGV.DeviceCode,
                        commonRGV.DeviceName,
                        commonRGV.CurrentTaskNum,
                        commonRGV.LastTaskNum,
                        //commonRGV.StackerCraneAutoStatusDes,
                        //commonRGV.StackerCraneWorkStatusDes,
                        //commonRGV.DeviceCode,
                        //commonRGV.DeviceName,
                        //commonRGV.CurrentTaskNum,
                        //commonRGV.LastTaskNum,
                    };
                    _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonRGV.DeviceName, data = obj });
@@ -135,31 +140,69 @@
            }
            return Task.CompletedTask;
        }
        /// <summary>
        /// ä»»åŠ¡å®Œæˆäº‹ä»¶è®¢é˜…çš„æ–¹æ³•
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
        public void RGVTaskFinish(CommonRGV commonRGV, int TaskNum)
        {
            CommonRGV? commonRGV = sender as CommonRGV;
            if (commonRGV != null)
            {
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                #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)
                {
                    ConsoleHelper.WriteColorLine($"【{commonRGV.DeviceName}】堆垛机作业状态:【{(int)commonRGV.StackerCraneWorkStatusValue}】时间【{DateTime.Now}】", ConsoleColor.Magenta);
                    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;
                                //var x = conveyorLine.Communicator.Read<string>("DB1103.16");
                                Thread.Sleep(100);
                                var Barcode = conveyorLine.GetValue<ConveyorLineDBName, string>(ConveyorLineDBName.ConveyorLineBarcode, stationManager.stationChildCode);
                    string str = $"【{commonRGV.DeviceName}】任务完成,任务号:【{e.TaskNum}】时间【{DateTime.Now}】";
                    WriteInfo(commonRGV.DeviceName, str);
                    ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
                    var content = _taskService.StackCraneTaskCompleted(e.TaskNum);
                    commonRGV.SetValue(StackerCraneDBName.WorkType, 5);
                    var isWorkType = commonRGV.SetValue(StackerCraneDBName.WorkType, 5);
                    str = $"{commonRGV.DeviceName}】WMS|WCS任务完成:【{content.Status}】,堆垛机完成信号写入:【{isWorkType}】,任务号:【{e.TaskNum}】时间【{DateTime.Now}】";
                    WriteInfo(commonRGV.DeviceName, str);
                    ConsoleHelper.WriteColorLine(str, ConsoleColor.Blue);
                                if (Barcode == null)
                                {
                                    return;
                                }
                                var content = _taskService.RGVTaskCompleted(TaskNum, Barcode);
                            }
                        }
                    }
                    else
                    {
                        if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutExecuting)
                        {
                            Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == task.TargetAddress&&x.stationType==7);
                            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, string>(ConveyorLineDBName.ConveyorLineBarcode, stationManager.stationChildCode);
                                    conveyorLine.SetValue(ConveyorLineDBName.ConveyorLineTargetAddress, Convert.ToInt16(stationManager.stationNextChildCode), stationManager.stationChildCode);
                                    Thread.Sleep(100);
                                    conveyorLine.SetValue(ConveyorLineDBName.ConveyorLineTaskNum, Convert.ToInt16(1000), stationManager.stationChildCode);
                                    Thread.Sleep(100);
                                    conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(2), stationManager.stationChildCode);
                                }
                            }
                        }
                        var content = _taskService.RGVTaskCompleted(TaskNum);
                    }
                }
            }
        }
@@ -169,33 +212,34 @@
        /// </summary>
        /// <param name="commonRGV">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(CommonRGV commonRGV)
        private Dt_Task? GetTask(CommonRGV commonRGV, bool InStock)
        {
            Dt_Task task;
            task = _taskService.QueryRelocationTask(commonRGV.DeviceCode);
            if (task != null)
            if (InStock)
            {
                return task;
                task = _taskService.QueryRGVExecutingTask(commonRGV.DeviceCode);
                if (task != null)
                {
                    return task;
                }
            }
            if (commonRGV.LastTaskType == null)
            {
                task = _taskService.QueryStackerCraneTask(commonRGV.DeviceCode);
                task = _taskService.QueryRGVTask(commonRGV.DeviceCode);
            }
            else
            {
                if (commonRGV.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    task = _taskService.QueryStackerCraneInTask(commonRGV.DeviceCode);
                    task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
                    if (task == null)
                    {
                        task = _taskService.QueryStackerCraneOutTask(commonRGV.DeviceCode);
                        task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode);
                    }
                }
                else
                {
                    task = _taskService.QueryStackerCraneOutTask(commonRGV.DeviceCode);
                    task = _taskService.QueryRGVOutTask(commonRGV.DeviceCode);
                }
            }
@@ -203,7 +247,7 @@
            {
                if (task == null)
                {
                    task = _taskService.QueryStackerCraneInTask(commonRGV.DeviceCode);
                    task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
                }
                else
                {
@@ -212,12 +256,109 @@
            }
            else if (task == null)
            {
                task = _taskService.QueryStackerCraneInTask(commonRGV.DeviceCode);
                task = _taskService.QueryRGVInTask(commonRGV.DeviceCode);
            }
            return task;
        }
        /// <summary>
        /// ä»»åŠ¡å®žä½“è½¬æ¢æˆå‘½ä»¤Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        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, short>(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.TaskType) == rgvTaskCommand.TaskType && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation && commonRGV.GetValue<StackerCraneDBName, byte>(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation)
                {
                    WriteInfo(commonRGV.DeviceName, $"写入任务成功写入次数{i}写入任务【{JsonConvert.SerializeObject(rgvTaskCommand)}】");
                    return true;
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.TaskNum) == rgvTaskCommand.TaskNum)
                {
                    commonRGV.SetValue(StackerCraneDBName.TaskNum, rgvTaskCommand.TaskNum);
                    Thread.Sleep(100);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.PutcargoLocation) == rgvTaskCommand.PutcargoLocation)
                {
                    commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, rgvTaskCommand.PutcargoLocation);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(StackerCraneDBName.PickupLocation) == rgvTaskCommand.PickupLocation)
                {
                    commonRGV.SetValue(StackerCraneDBName.PickupLocation, rgvTaskCommand.PickupLocation);
                }
                if (commonRGV.GetValue<StackerCraneDBName, short>(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);