dengjunjie
5 天以前 f40ba2a9fb9d3c2ece08e9a7cd987d4744dccab2
ÏîÄ¿´úÂë/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -15,8 +15,8 @@
using System.Threading.Tasks;
using WIDESEAWCS_Common.StackerCraneEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Common.WMSInfo;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IShuttleCar;
using WIDESEAWCS_ITaskInfoRepository;
@@ -30,27 +30,29 @@
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_TaskInfoService;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.ShuttleCarJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
using static WIDESEAWCS_Common.WMSInfo.RequestWMS;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public partial class CommonStackerCraneJob : IJob
    public partial class CommonStackerCraneJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IShuttleCarService _shuttleCarService;
        private readonly ICacheService _cacheService;
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IShuttleCarService shuttleCarService)
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IShuttleCarService shuttleCarService, ICacheService cacheService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _shuttleCarService = shuttleCarService;
            _cacheService = cacheService;
        }
        public Task Execute(IJobExecutionContext context)
@@ -60,53 +62,114 @@
                SpeStackerCrane commonStackerCrane = (SpeStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    commonStackerCrane.Communicator.IsReadAfterWrite = false;
                    #region èŽ·å–å †åž›æœºä¿¡æ¯
                    StackerCraneTaskCommandR common = ReadCustomer<StackerCraneTaskCommandR>(commonStackerCrane, commonStackerCrane.DeviceCode);
                    _cacheService.AddOrUpdate(commonStackerCrane.DeviceCode, common);
                    #endregion
                    #region ä¿¡å·äº¤äº’处理
                    Heartbeat(commonStackerCrane);
                    //Heartbeat(commonStackerCrane);
                    ShuttleCarSignal(commonStackerCrane);
                    #endregion
                    #region ä»»åŠ¡é€»è¾‘å¤„ç†
                    if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.Onlinestate) == (short)Onlinestate.online
                    && commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WriteStatus) == (short)WriteStatus.Permit)
                    #region ä»»åŠ¡å®Œæˆ
                    if (common.CompleteStatus == 2 || common.CompleteStatus == 3)
                    {
                        #region ä»»åŠ¡å®Œæˆ
                        DeviceProtocolDetailDTO? deviceProtocolDetail = commonStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.ProtocalDetailValue == commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.CompleteStatus).ToString() && x.DeviceProParamName == nameof(StackerCraneTaskCommandR.CompleteStatus));
                        if (deviceProtocolDetail != null)
                        {
                            MethodInfo? method = GetType().GetMethod(deviceProtocolDetail.ProtocolDetailType);
                            if (method != null) method.Invoke(this, new object[] { commonStackerCrane });
                        }
                        #endregion
                        NormalCompleted(commonStackerCrane);
                    }
                    //DeviceProtocolDetailDTO? deviceProtocolDetail = commonStackerCrane.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.ProtocalDetailValue == commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.CompleteStatus).ToString() && x.DeviceProParamName == nameof(StackerCraneTaskCommandR.CompleteStatus));
                    //if (deviceProtocolDetail != null)
                    //{
                    //    MethodInfo? method = GetType().GetMethod(deviceProtocolDetail.ProtocolDetailType);
                    //    if (method != null) method.Invoke(this, new object[] { commonStackerCrane });
                    //}
                    #endregion
                    #region ä»»åŠ¡é€»è¾‘å¤„ç†
                    if (common.Onlinestate == (short)Onlinestate.online)
                    {
                        #region æŸ¥è¯¢ä»»åŠ¡,逻辑处理
                        if (Enum.Parse<RunStatus>(commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.RunStatus).ToString()) == RunStatus.Standby)
                        if (common.RunStatus == (short)RunStatus.Standby)
                        {
                            var Task = _taskService.GetTaskState(TaskType: (int)TaskOtherTypeEnum.RelocationCar);//获取移车任务
                            var Task = _taskService.GetTaskState((int)TaskCarStatusEnum.ShuttleCar_Finish, (int)TaskOtherTypeEnum.RelocationCar);//获取移车任务
                            var Task1 = _taskService.GetTaskState((int)CarChargingTaskEnum.CarCharging_Finish, (int)TaskOtherTypeEnum.RelocationCarCharging);//获取移车充电任务
                            if (Task != null)
                            {
                                if (Task.TaskState == (int)TaskCarStatusEnum.ShuttleCar_Finish) SendTask(commonStackerCrane, Task);
                                SendTask(commonStackerCrane, Task);
                            }
                            else if (Task1 != null)
                            {
                                SendTask(commonStackerCrane, Task1);
                            }
                            else
                            {
                                Dt_Task? task = GetTask(commonStackerCrane);//获取堆垛机出入库任务
                                Dt_Task? task = GetTask(commonStackerCrane);//获取堆垛机任务
                                if (task != null)
                                {
                                    if (task.TaskType == (int)TaskInboundTypeEnum.Inbound)
                                    if (task.TaskType == (int)TaskRelocationTypeEnum.Relocation)//移库任务判断终点是否有穿梭车
                                    {
                                        var ShuttleCar = GetShuttleCarInfo(task.TargetAddress);//入库任务判断是否存在穿梭车
                                        if (task.IsPickPlace && ShuttleCar != null)//移走
                                        if (task.SourceIsPickPlace && task.TaskState == (int)TaskRelocationStatusEnum.RelocationNew)
                                        {
                                            var ShuttleCar = GetShuttleCarInfo(task.TargetAddress);//移库任务判断终点是否有穿梭车
                                            if (ShuttleCar == null)
                                            {
                                                _taskService.AddRelocationCarTask("", task.TargetAddress);
                                            }
                                            else
                                                SendTask(commonStackerCrane, task);//下发堆垛机任务
                                        }
                                        else if (!task.SourceIsPickPlace && task.TaskState == (int)TaskRelocationStatusEnum.RelocationNew && task.ShuttleCarCode == null)
                                        {
                                            var ShuttleCar = GetShuttleCarInfo(task.SourceAddress);//移库任务判断起点是否有穿梭车
                                            if (ShuttleCar != null)
                                            {
                                                task.ShuttleCarCode = ShuttleCar.ShuttleCarCode;
                                                _taskService.UpdateData(task);
                                            }
                                            else _taskService.AddRelocationCarTask("", task.SourceAddress);
                                        }
                                        else if (/*!task.TargetIsPickPlace &&*/ task.TaskState == (int)TaskRelocationStatusEnum.CarSource_Finish)
                                        {
                                            var ShuttleCar = GetShuttleCarInfo(task.TargetAddress);//移库任务判断终点是否有穿梭车
                                            if (task.TargetIsPickPlace && ShuttleCar != null)//移走
                                            {
                                                _taskService.AddRelocationCarTask(task.TargetAddress, "", ShuttleCar.ShuttleCarCode);
                                            }
                                            else if (!task.TargetIsPickPlace && ShuttleCar == null)
                                            {
                                                _taskService.AddRelocationCarTask("", task.TargetAddress);
                                                #region æŸ¥è¯¢
                                                task = _taskService.QueryCarOutFinish();
                                                if (task != null) SendTask(commonStackerCrane, task);//下发堆垛机任务
                                                #endregion
                                            }
                                            else
                                            {
                                                task.ShuttleCarCode = ShuttleCar?.ShuttleCarCode;
                                                _taskService.UpdateData(task);
                                                SendTask(commonStackerCrane, task);//下发堆垛机任务
                                            }
                                        }
                                    }
                                    else if (_taskService.TaskInboundTypes.Contains(task.TaskType))
                                    {
                                        var ShuttleCar = GetShuttleCarInfo(task.TargetAddress);//入库任务判断终点是否有穿梭车
                                        if (task.TargetIsPickPlace && ShuttleCar != null)//移走
                                        {
                                            _taskService.AddRelocationCarTask(task.TargetAddress, "", ShuttleCar.ShuttleCarCode);
                                        }
                                        else if (!task.IsPickPlace && ShuttleCar == null)
                                        else if (!task.TargetIsPickPlace && ShuttleCar == null)
                                        {
                                            _taskService.AddRelocationCarTask("", task.TargetAddress);
                                        }
                                        else
                                        {
                                            task.ShuttleCarCode = ShuttleCar?.ShuttleCarCode;
                                            _taskService.UpdateData(task);
                                            SendTask(commonStackerCrane, task);//下发堆垛机任务
                                        }
                                    }
                                    else
                                        SendTask(commonStackerCrane, task);//下发堆垛机任务
                                }
                            }
@@ -118,7 +181,7 @@
            }
            catch (Exception ex)
            {
                Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
@@ -130,12 +193,14 @@
            if (stackerCraneTaskCommand != null)
            {
                bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                bool worktype = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, task.TaskType == (int)TaskOtherTypeEnum.RelocationCar ? 2 : 1);
                bool worktype = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, task.TaskType >= (int)TaskOtherTypeEnum.RelocationCar ? (short)WorkType.Car : (short)WorkType.Cargo);
                if (sendFlag && worktype)
                {
                    if (commonStackerCrane.SetValue(StackerCraneDBName.CommandSend, sendFlag))
                    {
                        commonStackerCrane.LastTaskType = task.TaskType;
                        if (_taskService.TaskInboundTypes.Contains(task.TaskType) ||
                            _taskService.TaskOutboundTypes.Contains(task.TaskType))
                            commonStackerCrane.LastTaskType = task.TaskType;
                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                    }
                }
@@ -169,9 +234,8 @@
        {
            if (commonStackerCrane != null)
            {
                Console.Out.WriteLine($"堆垛机任务完成:[{commonStackerCrane.CurrentTaskNum}];{DateTime.Now}");
                var Status = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum).Status;///需修改!!!!!!!!!!!!!!
                commonStackerCrane.SetValue(StackerCraneDBName.ConfirmComplete, Status);
                var content = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum);
                commonStackerCrane.SetValue(StackerCraneDBName.ConfirmComplete, content.Status);
            }
        }
        /// <summary>
@@ -181,52 +245,69 @@
        /// <returns></returns>
        private Dt_Task? GetTask(SpeStackerCrane commonStackerCrane)
        {
            Dt_Task task;
            if (commonStackerCrane.LastTaskType == null)
            Dt_Task task = _taskService.QueryStackerCraneRelocationTask(commonStackerCrane.DeviceCode);//获取移库任务
            #region å­˜åœ¨ç§»åº“任务必须先执行移库任务
            if (task != null)
            {
                task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
                return task;
                //if (task.SourceIsPickPlace && task.ShuttleCarCode != null)
                //    return task;
                //else return null;
            }
            #endregion
            #region å‡ºå…¥åº“任务
            else
            {
                if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                if (task != null) return task;
                if (commonStackerCrane.LastTaskType == null)
                {
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    if (task == null)
                    {
                        task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                    }
                    task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
                }
                else
                {
                    task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                    if (task == null)
                    if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                    {
                        task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode) ?? _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                        if (task == null)
                        {
                            task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                        }
                    }
                    else
                    {
                        task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode) ?? _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                        if (task == null)
                        {
                            task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                        }
                    }
                }
                if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    if (OutTaskStationIsOccupied(task) != null)
                    {
                        return task;
                    }
                    else
                    {
                        List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList();
                        List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes);
                        foreach (var item in tasks)
                        {
                            if (OutTaskStationIsOccupied(item) != null)
                            {
                                return item;
                            }
                        }
                        task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    }
                }
                if (task != null && !string.IsNullOrEmpty(task.TargetAddress)) return task;//入库任务判断是否已分配货位,WMS判断货位状态是否允许放货
            }
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                if (OutTaskStationIsOccupied(task) != null)
                {
                    return task;
                }
                else
                {
                    List<string> otherOutStaionCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress).Select(x => x.ChildPosi).ToList();
                    List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStaionCodes);
                    foreach (var item in tasks)
                    {
                        if (OutTaskStationIsOccupied(item) != null)
                        {
                            return item;
                        }
                    }
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                }
            }
            if (task != null && task.TargetAddress != "SC01") return task;//入库任务判断是否已分配货位,WMS判断货位状态是否允许放货
            #endregion
            return task;
        }
@@ -244,10 +325,15 @@
                if (device != null)
                {
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用
                    ConveyorLineTaskCommandR command = conveyorLine.ReadCustomer<ConveyorLineTaskCommandR>(router.ChildPosi);
                    if (command.Status == 4)
                    {
                        return task;
                    }
                    //if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用
                    //{
                    //    return task;
                    //}
                }
                else
                {
@@ -271,10 +357,7 @@
        {
            StackerCraneTaskCommandW stackerCraneTaskCommand = new StackerCraneTaskCommandW();
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = (short)WorkType.Cargo;
            stackerCraneTaskCommand.TrayType = 0;
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
            {
                List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway);
@@ -287,7 +370,8 @@
                    string[] targetCodes = task.NextAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                        var EndRow = Convert.ToInt16(targetCodes[0]);
                        stackerCraneTaskCommand.EndRow = EndRow;// Convert.ToInt16(EndRow > 1 ? EndRow - 1 : EndRow);
                        stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                        stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                    }
@@ -316,7 +400,8 @@
                    string[] sourceCodes = task.CurrentAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                        var StartRow = Convert.ToInt16(sourceCodes[0]);
                        stackerCraneTaskCommand.StartRow = StartRow;// Convert.ToInt16(StartRow > 1 ? StartRow - 1 : StartRow);
                        stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                        stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                    }
@@ -333,12 +418,13 @@
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OtherGroup)
            {
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    var EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndRow = EndRow;// Convert.ToInt16(EndRow > 1 ? EndRow - 1 : EndRow);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                }
@@ -351,7 +437,8 @@
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 3)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                    var SourceRow = Convert.ToInt16(sourceCodes[0]);
                    stackerCraneTaskCommand.StartRow = SourceRow;// Convert.ToInt16(SourceRow > 1 ? SourceRow - 1 : SourceRow);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                }