qinchulong
2025-05-27 c020f31a67fc5aa5644511bddff075f7ecc85234
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,282 @@
using Quartz;
using SqlSugar.Extensions;
using System.Diagnostics.CodeAnalysis;
using WIDESEA_Core.Enums;
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;
using WIDESEAWCS_Tasks.StackerCraneJob;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonStackerCraneJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        public CommonStackerCraneJob(ITaskService taskService, ITaskRepository taskRepository, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService)
        {
            _taskService = taskService;
            _taskRepository = taskRepository;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerService = routerService;
        }
        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;//订阅任务完成事件
                    }
                    if (getStackerObject.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && getStackerObject.StackerCraneStatusValue == StackerCraneStatus.Ready)
                    {
                        getStackerObject.CheckStackerCraneTaskCompleted();//检测堆垛机任务完成事件
                        if(getStackerObject.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                        {
                            Dt_Task? task = GetTask(speStackerCrane);
                            if (task != null)
                            {
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                if (stackerCraneTaskCommand != null)
                                {
                                    bool sendFlag = getStackerObject.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        speStackerCrane.LastTaskType = task.TaskType;
                                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //WriteLog.GetLog($"堆垛机异常").Write($"{nameof(CommonStackerCraneJob)}:{ex.Message}", "堆垛机异常");
            }
            return Task.CompletedTask;
        }
        /// <summary>
        /// ä»»åŠ¡å®Œæˆäº‹ä»¶è®¢é˜…çš„æ–¹æ³•
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
        {
            SpeStackerCrane? speStackerCrane = sender as SpeStackerCrane;
            if (speStackerCrane != null)
            {
                if (speStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                {
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    speStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
        }
        /// <summary>
        /// èŽ·å–ä»»åŠ¡
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(SpeStackerCrane speStackerCrane)
        {
            Dt_Task? task;
            if (speStackerCrane.LastTaskType == null)
            {
                task = _taskService.QueryStackerCraneTask(speStackerCrane.DeviceCode);
            }
            else
            {
                bool flag = speStackerCrane.LastTaskType == TaskTypeEnum.Inbound.ObjToInt() || speStackerCrane.LastTaskType == TaskTypeEnum.PalletInbound.ObjToInt();
                if (flag == false)
                {
                    task = _taskService.QueryStackerCraneInTask(speStackerCrane.DeviceCode);
                    if (task == null)
                    {
                        task = _taskService.QueryStackerCraneOutTask(speStackerCrane.DeviceCode);
                    }
                }
                else
                {
                    task = _taskService.QueryStackerCraneOutTask(speStackerCrane.DeviceCode);
                }
            }
            return task;
        }
        /// <summary>
        /// å‡ºåº“任务判断出库站台是否被占用
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        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;
        }
        /// <summary>
        /// ä»»åŠ¡å®žä½“è½¬æ¢æˆå‘½ä»¤Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TrayType = 0;
            if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())//判断是否是入库任务
            {
                if (task.SourceAddress != null && task.TargetAddress != null)
                {
                    string[] sourceCodes = task.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]);
                        stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.SourceAddress}】");
                        return null;
                    }
                    string[] targetCodes = task.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                        stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.TargetAddress}】");
                        return null;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress},{task.SourceAddress}】信息,无法获取对应的堆垛机取货站台信息");
                    return null;
                }
            }
            else if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletOutbound.ObjToInt())
            {
                if (task.SourceAddress != null && task.TargetAddress != null)
                {
                    string[] targetCodes = task.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                        stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.SourceAddress}】");
                        return null;
                    }
                    string[] sourceCodes = task.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        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}】");
                        return null;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
                    return null;
                }
            }
            else if (task.TaskType == TaskTypeEnum.Relocation.ObjToInt())
            {
                if (task.SourceAddress != null && task.TargetAddress != null)
                {
                    string[] targetCodes = task.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                        stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.TargetAddress}】");
                        return null;
                    }
                    string[] sourceCodes = task.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]);
                        stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.SourceAddress}】");
                        return null;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点终点错误,起点:【{task.SourceAddress}】,终点:【{task.TargetAddress}】");
                }
            }
            return stackerCraneTaskCommand;
        }
    }
}