wankeda
2 天以前 734f49c3f74e4a46cfb5892ce60dbf1bb86e74ab
WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -1,27 +1,10 @@
using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
using Quartz;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
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_QuartzJob.StackerCrane.Enum;
@@ -36,20 +19,17 @@
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IPlatformStationService _PlatformStationService;
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IPlatformStationService platformStation)
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _PlatformStationService= platformStation;
        }
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
@@ -75,7 +55,8 @@
                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                        commonStackerCrane.LastTaskType = task.TaskType;
                                        //_taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                }
                            }
@@ -85,7 +66,7 @@
            }
            catch (Exception ex)
            {
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
                Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
@@ -102,7 +83,8 @@
            {
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                {
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    //Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                   // _taskService.StackCraneTaskCompleted(e.TaskNum);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
@@ -116,49 +98,51 @@
        private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
        {
            Dt_Task task;
            task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
            if (task != null && task.TaskType== (int)TaskOutboundTypeEnum.Outbound)
            if (commonStackerCrane.LastTaskType == null)
            {
                string[] targetCodes = task.SourceAddress.Split("-");
                if (targetCodes[4] == "02")
                task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
            }
            else
            {
                if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                {
                    Dt_Task taskst = OutTaskMovelibrary(task);
                    if (taskst != null)
                    {
                        if(taskst.TaskType == (int)TaskRelocationTypeEnum.Relocation)
                        {
                            return taskst;
                        }
                        else
                        {
                            if(taskst.TargetAddress== "R02-001-021-001-02" || taskst.TargetAddress == "R02-001-022-001-02")
                            {
                                return taskst;
                            }
                            else
                            {
                                if (OutTaskStationIsOccupied(taskst) != null)
                                {
                                    return taskst;
                                }
                            }
                        }
                    }
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    if (task == null)
                    {
                        task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                    }
                }
                else
                {
                    if (OutTaskStationIsOccupied(task) != null)
                    task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
                    if (task == null)
                    {
                        return task;
                        task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    }
                }
            }
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                if (OutTaskStationIsOccupied(task) != null || true)
                {
                    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(task) != null)
                        {
                            return task;
                        }
                    }
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                }
            }
            return task;
        }
@@ -169,77 +153,31 @@
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
        {
            if(task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
            Dt_Router? router = _routerService.QueryNextRoutes(task.DeviceCode, task.NextAddress).FirstOrDefault();
            if (router != null)
            {
                string plcAddress = _PlatformStationService.GetOutSCName(task.Roadway,task.Station_storey);
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1002");
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode);
                if (device != null)
                {
                    if (device.DeviceCode == "YMCTSJ" || task.DeviceCode == "CSJCSC01") return task;
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = GetDeviceProDTO(conveyorLine, plcAddress, "Free");
                    if (deviceProDTO != null)
                    if (conveyorLine.IsOccupied(router.ChildPosi))//出库站台未被占用
                    {
                        bool B_Event_Test = GetLine(conveyorLine, deviceProDTO.DeviceProAddress);        //判断输送线是否允许放货信号
                        if (B_Event_Test)
                        {
                            return task;
                        }
                        return task;
                    }
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{task.SourceAddress}】对应的通讯对象,无法判断出库站台是否被占用");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{router.ChildPosiDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用");
                }
            }
            else
            {
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台");
            }
            return null;
        }
        /// <summary>
        /// 出库任务判断是否需要进行移库Move the library
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskMovelibrary([NotNull] Dt_Task task)
        {
            try
            {
                string[] targetCodes = task.SourceAddress.Split("-");
                if (targetCodes[1] == "001")
                {
                    targetCodes[1] = "002";
                }
                else if (targetCodes[1] == "004")
                {
                    targetCodes[1] = "003";
                }
                targetCodes[4] = "01";
                string SourceAddress = string.Join("-", targetCodes); //组装浅库位地址
                Dt_Task? tasks = _taskService.QueryStationIsOccupiedOutTasks(task.Roadway, SourceAddress).FirstOrDefault(); //找浅库位是否有任务
                if (tasks != null)
                {
                    return tasks;
                }
                else
                {
                    //向wms申请判断浅库位是否有货,是否需要进行移库
                    Dt_Task? taskst = _taskService.RequestWMSTaskMovelibrary(task);
                    if (taskst != null)
                    {
                        return taskst;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return null;
        }
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
@@ -249,31 +187,28 @@
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
            stackerCraneTaskCommand.Barcode = ""; /*task.PalletCode;*/
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TrayType = (short)task.PalletCodequantity;   //待改动
            stackerCraneTaskCommand.TrayType = 1;
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
            {
                string[] targetCodest = task.SourceAddress.Split("-");
                if (targetCodest.Length == 5)
                //List<Dt_Router> routers = _routerService.QueryNextRoutes(task.CurrentAddress, task.Roadway);
                //if (routers.Count > 0)
                //{
                string[] startCodes = task.CurrentAddress.Split("-");
                stackerCraneTaskCommand.StartRow = Convert.ToInt16(startCodes[0]);
                stackerCraneTaskCommand.StartColumn = Convert.ToInt16(startCodes[1]);
                stackerCraneTaskCommand.StartLayer = Convert.ToInt16(startCodes[2]);
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(targetCodest[1]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(targetCodest[2]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(targetCodest[3]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库起点错误,起点:【{task.SourceAddress}】");
                    return null;
                }
                string[] targetCodes = task.TargetAddress.Split("-");
                if (targetCodes.Length == 5)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                }
                else
                {
@@ -281,44 +216,52 @@
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                    return null;
                }
                //}
                //else
                //{
                //    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                //    return null;
                //}
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                string[] sourceCodes = task.SourceAddress.Split("-");
                if (sourceCodes.Length == 5)
                //List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress);
                //if (routers.Count > 0)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]);
                    string[] targetCodes = task.NextAddress.Split("-");
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                    string[] sourceCodes = task.CurrentAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                        stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                        stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                    }
                    else
                    {
                        //数据配置错误
                        _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                        return null;
                    }
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
                string[] sourceCodest = task.TargetAddress.Split("-");
                if (sourceCodest.Length == 5)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(sourceCodest[1]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(sourceCodest[2]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(sourceCodest[3]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.TargetAddress}】");
                    return null;
                }
                //else
                //{
                //    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
                //    return null;
                //}
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
            {
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 5)
                if (targetCodes.Length == 3)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                }
                else
                {
@@ -327,11 +270,11 @@
                    return null;
                }
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 5)
                if (sourceCodes.Length == 3)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]);
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                }
                else
                {
@@ -341,18 +284,6 @@
                }
            }
            return stackerCraneTaskCommand;
        }
        //根地址读取输送线信息
        public bool GetLine(CommonConveyorLine conveyorLine, string DeviceProDataBlock)
        {
            return conveyorLine.Communicator.Read<bool>(DeviceProDataBlock);
        }
        //获取输送线实例
        public DeviceProDTO? GetDeviceProDTO(CommonConveyorLine conveyorLine, string SCAddress, string Interactivet)
        {
            return conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SCAddress && x.DeviceProParamName == Interactivet);
        }
    }
}