wanshenmean
2026-02-11 88724eca51a5863f20ee0a552af741aeebf4e8f2
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -25,15 +25,19 @@
        private readonly TcpSocketServer _TcpSocket;
        private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
        private static int _eventSubscribedFlag;
        private readonly IRobotTaskService _taskService;
        private readonly ITaskService _taskService;
        private readonly IRobotTaskService _robottaskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService taskService)
        public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService RobottaskService, ITaskService TaskService)
        {
            _TcpSocket = TcpSocket;
            _taskService = taskService;
            _robottaskService = RobottaskService;
            this._taskService = TaskService;
        }
        public async Task Execute(IJobExecutionContext context)
@@ -130,10 +134,9 @@
        /// <returns></returns>
        private async Task<string?> _TcpSocket_MessageReceived(string message, bool isJson, TcpClient client, RobotSocketState state)
        {
            WebResponseContent content = new WebResponseContent();
            string messageLower = message.ToLowerInvariant();
            if (IsSimpleCommand(messageLower, state))
            if (await IsSimpleCommandAsync(messageLower, state))
            {
                return null;
            }
@@ -174,9 +177,8 @@
                            state.LastPickPositions = positions;
                            var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), state.CurrentTask?.RobotTaskType == 2 ? nameof(ConfigKey.ChangePalletAsync) : nameof(ConfigKey.SplitPalletAsync));
                            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                            if (content.Status)
                            if (result.Status)
                            {
                                state.CurrentAction = "PickFinished";
                            }
@@ -205,9 +207,8 @@
                                        .ToList()
                                };
                                var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), nameof(ConfigKey.GroupPalletAsync));
                                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                                if (content.Status)
                                if (result.Status)
                                {
                                    state.CurrentAction = "PutFinished";
                                }
@@ -229,7 +230,7 @@
        /// <param name="message"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        private bool IsSimpleCommand(string message, RobotSocketState state)
        private async Task<bool> IsSimpleCommandAsync(string message, RobotSocketState state)
        {
            switch (message)
            {
@@ -253,7 +254,7 @@
                    state.CurrentAction = "AllPickFinished";
                    if (state.CurrentTask?.RobotTaskType == 2 || state.CurrentTask?.RobotTaskType == 3)
                    {
                        // TODO 机械手取货完成,判断是否换盘、拆盘任务,创建空托盘回库任务
                        await HandleInboundTaskAsync(state, useSourceAddress: true);
                    }
                    return true;
@@ -261,17 +262,7 @@
                    state.CurrentAction = "AllPutFinished";
                    if (state.CurrentTask?.RobotTaskType == 1)
                    {
                        // TODO 机械手取货完成,判断是否组盘任务,创建组盘入库任务
                        CreateTaskDto taskDto = new CreateTaskDto()
                        {
                            PalletCode = state.CurrentTask?.RobotTargetAddressPalletCode ?? string.Empty,
                            SourceAddress = state.CurrentTask?.RobotTargetAddress ?? string.Empty,
                            TargetAddress = state.CurrentTask?.RobotTargetAddress ?? string.Empty,
                            Roadway = state.CurrentTask?.RobotRoadway == "1" ? "GWSC001" : state.CurrentTask?.RobotRoadway == "2" ? "HCSC001" : "SC001" ?? string.Empty,
                            WarehouseId = state.CurrentTask?.RobotRoadway == "1" ? 1 : state.CurrentTask?.RobotRoadway == "2" ? 2 : 3,
                            PalletType = 1,
                            TaskType = 4
                        };
                        await HandleInboundTaskAsync(state, useSourceAddress: false);
                    }
                    return true;
@@ -320,6 +311,49 @@
            }
        }
        private async Task HandleInboundTaskAsync(RobotSocketState state, bool useSourceAddress)
        {
            var currentTask = state.CurrentTask;
            if (currentTask == null)
            {
                return;
            }
            string roadway = currentTask.RobotRoadway == "1" ? "GWSC001" : currentTask.RobotRoadway == "2" ? "HCSC001" : "SC001";
            int warehouseId = currentTask.RobotRoadway == "1" ? 1 : currentTask.RobotRoadway == "2" ? 2 : 3;
            CreateTaskDto taskDto = new CreateTaskDto
            {
                PalletCode = currentTask.RobotTargetAddressPalletCode ?? string.Empty,
                SourceAddress = currentTask.RobotTargetAddress ?? string.Empty,
                TargetAddress = currentTask.RobotTargetAddress ?? string.Empty,
                Roadway = roadway,
                WarehouseId = warehouseId,
                PalletType = 1,
                TaskType = 4
            };
            var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), taskDto.ToJsonString(), nameof(ConfigKey.CreateTaskInboundAsync));
            if (!result.Status)
            {
                return;
            }
            WMSTaskDTO taskDTO = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.ToString() ?? string.Empty) ?? new WMSTaskDTO();
            var content = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
            if (!content.Status) return;
            var taskInfo = _taskService.QueryByTaskNum(taskDTO.TaskNum);
            string targetAddress = useSourceAddress ? taskDTO.SourceAddress : taskDTO.TargetAddress;
            IDevice? device = Storage.Devices.Where(x => x.DeviceProDTOs.Select(x => x.DeviceChildCode == taskDTO.SourceAddress).FirstOrDefault()).FirstOrDefault() ?? null;
            device?.Communicator.Write(nameof(ConveyorLineDBNameNew.Target), taskInfo.NextAddress);
            device?.Communicator.Write(nameof(ConveyorLineDBNameNew.TaskNo), taskDTO.TaskNum);
            device?.Communicator.Write(nameof(ConveyorLineDBNameNew.WCS_STB), 1);
        }
        /// <summary>
        /// 机械手前缀命令处理
        /// </summary>
@@ -332,7 +366,7 @@
        private Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
        {
            return _taskService.QueryRobotCraneTask(robotCrane.DeviceCode);
            return _robottaskService.QueryRobotCraneTask(robotCrane.DeviceCode);
        }
    }