1
hutongqing
2025-01-08 413f243b73ef073f4ac83889fda2f5bf6d1e727c
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/°å²Ä²Ö/StackerCraneJob_BC.cs
@@ -20,25 +20,47 @@
using WIDESEAWCS_Tasks.HoisterJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
using WIDESEAWCS_Tasks;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using Newtonsoft.Json;
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class StackerCraneJob_BC : JobBase, IJob
    {
        private readonly ICacheService _cacheService;
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IStationMangerRepository _stationMangerRepository;
        private List<Dt_ApiInfo> apiInfos;
        public StackerCraneJob_BC(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository)
        public StackerCraneJob_BC(ITaskService taskService, ICacheService cacheService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IStationMangerRepository stationMangerRepository)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _stationMangerRepository = stationMangerRepository;
            _cacheService = cacheService;
            string? apiInfoStr = _cacheService.Get("apiInfos");
            if (!string.IsNullOrEmpty(apiInfoStr))
            {
                List<Dt_ApiInfo>? infos = JsonConvert.DeserializeObject<List<Dt_ApiInfo>>(apiInfoStr);
                if (infos == null || infos.Count == 0)
                {
                    apiInfos = new List<Dt_ApiInfo>();
                }
                else
                {
                    apiInfos = infos;
                }
            }
        }
        public Task Execute(IJobExecutionContext context)
@@ -52,25 +74,25 @@
                    {
                        commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                    }
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
                    commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal && commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                    {
                        commonStackerCrane.CheckStackerCraneTaskCompleted();//防止任务完成事件监测超时,再手动触发一次
                        if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                        Dt_Task? task = GetTask(commonStackerCrane);
                        if (task != null)
                        {
                            Dt_Task? task = GetTask(commonStackerCrane);
                            if (task != null)
                            StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                            if (stackerCraneTaskCommand != null)
                            {
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                if (stackerCraneTaskCommand != null)
                                bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                if (sendFlag)
                                {
                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        commonStackerCrane.LastTaskType = task.TaskType;
                                       // _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                    commonStackerCrane.LastTaskType = task.TaskType;
                                    int oldState = task.TaskState;
                                    task.TaskState = TaskStatusEnum.SC_Executing.ObjToInt();
                                    task.Dispatchertime = DateTime.Now;
                                    task.ExceptionMessage = "";
                                    _taskRepository.UpdateData(task);
                                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"系统自动流程,任务状态从【{oldState}】转到【{task.TaskState}】");
                                }
                            }
                        }
@@ -96,10 +118,59 @@
            {
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
                {
                    Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                   // _taskService.StackCraneTaskCompleted(e.TaskNum);
                    StackerCraneTaskCompleted(e.TaskNum, commonStackerCrane.DeviceCode);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
        }
        public WebResponseContent StackerCraneTaskCompleted(int taskNum, string deviceCode)
        {
            try
            {
                Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == taskNum);
                if (task != null)
                {
                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                    {
                        Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.StackerCraneStationCode == task.NextAddress);
                        if (stationManger == null)
                        {
                            _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"AGV站点未配置,{task.NextAddress}");
                            _taskService.UpdateTaskExceptionMessage(taskNum, $"AGV站点未配置,{task.NextAddress}");
                            return WebResponseContent.Instance.Error($"AGV站点未配置,{task.NextAddress}");
                        }
                        int oldStatus = task.TaskState;
                        task.DeviceCode = "AGV_CSJ";
                        task.TaskState = TaskStatusEnum.AGV_Execute.ObjToInt();
                        task.CurrentAddress = stationManger.AGVStationCode;
                        task.NextAddress = task.TargetAddress;
                        _taskRepository.UpdateData(task);
                        _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"系统自动流程,,任务状态从【{oldStatus}】转到【{task.TaskState}】");
                    }
                    else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
                    {
                        _taskService.TaskCompleted(taskNum);
                    }
                    else
                    {
                        WriteInfo(deviceCode, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}");
                        _taskExecuteDetailService.AddTaskExecuteDetail(taskNum, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}");
                        _taskService.UpdateTaskExceptionMessage(taskNum, $"未找到该任务类型回调WMS任务完成接口,{task.TaskType}");
                    }
                }
                else
                {
                    WriteInfo(deviceCode, $"未找到任务信息,任务号:{taskNum}");
                    return WebResponseContent.Instance.Error($"未找到任务信息,任务号:{taskNum}");
                }
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                WriteError(deviceCode, $"任务完成错误", ex);
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
@@ -137,23 +208,37 @@
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                if (OutTaskStationIsOccupied(task) != null || true)
                if (OutTaskStationIsOccupied(task) == null)
                {
                    return task;
                }
                else
                {
                    bool flag = false;
                    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;
                            flag = true;
                            break;
                        }
                    }
                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    if (!flag)
                    {
                        task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
                    }
                }
            }
            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                string? url = apiInfos.FirstOrDefault(x => x.ApiCode == APIEnum.FeedBackWMSTaskCompleted.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(url))
                {
                    _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskNum, $"未找到WMS移库判断接口");
                    WriteInfo(commonStackerCrane.DeviceCode, $"未找到WMS移库判断接口");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到WMS移库判断接口");
                    return null;
                }
                HttpHelper.Post($"{url}?taskNum={task.TaskNum}&locationCode={task.NextAddress}", "");//todo è°ƒç”¨WMS任务完成方法
            }
            return task;
@@ -166,7 +251,7 @@
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
        {
            Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.TargetAddress && x.StackerCraneCode == task.DeviceCode);
            Dt_StationManger? stationManger = _stationMangerRepository.QueryFirst(x => x.StationCode == task.NextAddress && x.StackerCraneCode == task.DeviceCode);
            if (stationManger != null)
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode);
@@ -175,18 +260,21 @@
                    OtherDevice client = (OtherDevice)device;
                    if (client.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode))//出库站台未被占用
                    {
                        task.TargetAddress = stationManger.StackerCraneStationCode;
                        task.NextAddress = stationManger.StackerCraneStationCode;
                        _taskRepository.UpdateData(task);
                        client.SetValue(GroundStationDBName.R_IsCanPut, true, stationManger.StationCode);
                        return task;
                    }
                }
                else
                {
                    WriteInfo(task.DeviceCode, $"未找到出库站台【{stationManger.StationDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到出库站台【{stationManger.StationDeviceCode}】对应的通讯对象,无法判断出库站台是否被占用");
                }
            }
            else
            {
                WriteInfo(task.DeviceCode, $"未找到站台【{task.NextAddress}】信息,无法校验站台");
                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法校验站台");
            }
            return null;
@@ -205,19 +293,42 @@
            stackerCraneTaskCommand.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TrayType = 1;
            stackerCraneTaskCommand.TrayType = (Int16)task.PalletType;
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)//判断是否是入库任务
            {
                //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]);
                if (startCodes.Length == 3)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(startCodes[0]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(startCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(startCodes[2]);
                }
                else
                {
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务起点错误,起点:【{task.CurrentAddress}】");
                    WriteInfo(task.DeviceCode, $"入库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 5)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,终点:【{task.NextAddress}】");
                    WriteInfo(task.DeviceCode, $"入库任务终点错误,终点:【{task.NextAddress}】");
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
@@ -227,73 +338,54 @@
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,终点:【{task.NextAddress}】");
                    WriteInfo(task.DeviceCode, $"出库任务终点错误,终点:【{task.NextAddress}】");
                    return null;
                }
                //}
                //else
                //{
                //    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机取货站台信息");
                //    return null;
                //}
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
            {
                //List<Dt_Router> routers = _routerService.QueryNextRoutes(task.Roadway, task.TargetAddress);
                //if (routers.Count > 0)
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 5)
                {
                    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;
                    }
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[2]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[3]);
                }
                //else
                //{
                //    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.NextAddress}】信息,无法获取对应的堆垛机放货站台信息");
                //    return null;
                //}
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                    WriteInfo(task.DeviceCode, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
            {
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                if (targetCodes.Length == 5)
                {
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[2]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[3]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,起点:【{task.NextAddress}】");
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,终点:【{task.NextAddress}】");
                    WriteInfo(task.DeviceCode, $"移库任务终点错误,终点:【{task.NextAddress}】");
                    return null;
                }
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 3)
                if (sourceCodes.Length == 5)
                {
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                    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}】");
                    WriteInfo(task.DeviceCode, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
            }