wanshenmean
2026-03-11 a8f45091019012eeafec563913dee71cda3d9790
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -1,39 +1,65 @@
using HslCommunication;
using Newtonsoft.Json;
using Quartz;
using System.Collections.Concurrent;
using System.Net.Sockets;
using System.Text.Json;
using WIDESEAWCS_Common.HttpEnum;
using WIDESEAWCS_Core;
using Quartz;
using WIDESEA_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Http;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.Workflow.Abstractions;
using WIDESEAWCS_Tasks.Workflow;
using WIDESEAWCS_Tasks.SocketServer;
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 机器人任务作业:负责调度与生命周期管理,具体状态机流程交给编排器。
    /// </summary>
    [DisallowConcurrentExecution]
    public class RobotJob : IJob
    {
        private readonly TcpSocketServer _TcpSocket;
        private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
        private static int _eventSubscribedFlag;
        private readonly IRobotTaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private const int MaxTaskTotalNum = 48;
        public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService taskService)
        private static int _messageSubscribedFlag;
        private readonly RobotClientManager _clientManager;
        private readonly RobotStateManager _stateManager;
        private readonly IRobotMessageRouter _messageRouter;
        private readonly RobotTaskProcessor _taskProcessor;
        private readonly IRobotWorkflowOrchestrator _workflowOrchestrator;
        public RobotJob(
            TcpSocketServer tcpSocket,
            IRobotTaskService robotTaskService,
            ITaskService taskService,
            ICacheService cache,
            HttpClientHelper httpClientHelper)
        {
            _TcpSocket = TcpSocket;
            _taskService = taskService;
            _stateManager = new RobotStateManager(cache);
            // 收口 Socket 访问,后续若替换通信实现只需替换网关层。
            ISocketClientGateway socketGateway = new SocketClientGateway(tcpSocket);
            _taskProcessor = new RobotTaskProcessor(socketGateway, _stateManager, robotTaskService, taskService, httpClientHelper);
            _clientManager = new RobotClientManager(tcpSocket, _stateManager);
            var simpleCommandHandler = new RobotSimpleCommandHandler(_taskProcessor);
            var prefixCommandHandler = new RobotPrefixCommandHandler(robotTaskService, _taskProcessor, _stateManager, socketGateway);
            _messageRouter = new RobotMessageHandler(socketGateway, _stateManager, cache, simpleCommandHandler, prefixCommandHandler);
            _workflowOrchestrator = new RobotWorkflowOrchestrator(_stateManager, _clientManager, _taskProcessor, robotTaskService);
            _clientManager.OnClientDisconnected += OnClientDisconnected;
            // 全局只订阅一次消息事件,保持原有行为。
            if (System.Threading.Interlocked.CompareExchange(ref _messageSubscribedFlag, 1, 0) == 0)
            {
                tcpSocket.MessageReceived += _messageRouter.HandleMessageReceivedAsync;
                Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 机器手TCP消息事件已订阅");
            }
        }
        private void OnClientDisconnected(object? sender, RobotSocketState state)
        {
            Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 客户端已断开连接: {state.IPAddress}");
        }
        public async Task Execute(IJobExecutionContext context)
@@ -47,352 +73,35 @@
            string ipAddress = robotCrane.IPAddress;
            // 获取或创建状态
            RobotSocketState state = _socketStates.GetOrAdd(ipAddress, _ => new RobotSocketState
            {
                IPAddress = ipAddress,
                RobotCrane = robotCrane
            });
            // 更新设备信息
            RobotSocketState state = _stateManager.GetOrCreateState(ipAddress, robotCrane);
            state.RobotCrane = robotCrane;
            // 检查是否有该客户端连接
            var clientIds = _TcpSocket.GetClientIds();
            if (!clientIds.Contains(ipAddress))
            try
            {
                return;
            }
            // 订阅一次 message 事件(全局一次)
            if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
            {
                _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
                _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
            }
            if (!state.IsEventSubscribed)
            {
                _TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client);
                Task clientTask = _TcpSocket.HandleClientAsync(client, robotCrane.IPAddress, _TcpSocket._cts.Token, state);
                state.IsEventSubscribed = true;
            }
            // 获取任务并缓存到状态中
            Dt_RobotTask? task = GetTask(robotCrane);
            if (task != null)
            {
                state.CurrentTask = task;
                if (task.RobotTaskTotalNum != 48)
                if (!_clientManager.EnsureClientSubscribed(ipAddress, robotCrane))
                {
                    // 处理正在执行的任务
                    if (state.RobotRunMode == 1 && state.RobotControlMode == 1)
                    {
                        await Task.Delay(1000);
                        if ((state.CurrentAction == "Homed" || state.CurrentAction == "PickFinished" || state.CurrentAction == "PutFinished") && state.OperStatus == "Running")
                        {
                            // TODO 读取线体电池条码,发送取电池指令
                            if (true)
                            {
                                // 模拟读取条码
                                state.CellBarcode = new string[] { "CellBarcode1", "CellBarcode2", "CellBarcode3", "CellBarcode4" };
                    return;
                }
                                string taskString = $"Pickbattery,{task.RobotSourceAddress}";
                                // 发送任务指令
                                bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
                            }
                        }
                var task = _taskProcessor.GetTask(robotCrane);
                if (task != null)
                {
                    var latestState = _stateManager.GetState(ipAddress);
                    if (latestState == null)
                    {
                        return;
                    }
                    if (latestState.RobotTaskTotalNum < MaxTaskTotalNum)
                    {
                        await _workflowOrchestrator.ExecuteAsync(latestState, task, ipAddress);
                    }
                }
            }
            return;
        }
        /// <summary>
        ///  事件:客户端断开连接时触发
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        private Task<string?> _TcpSocket_RobotReceived(string clientId)
        {
            _socketStates.TryRemove(clientId, out _);
            return Task.FromResult<string?>(null);
        }
        /// <summary>
        /// 事件:收到消息时触发
        /// </summary>
        /// <param name="message"></param>
        /// <param name="isJson"></param>
        /// <param name="client"></param>
        /// <param name="state"></param>
        /// <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))
            catch (Exception)
            {
                return null;
                // 异常处理已在组件内部进行,Job 层保持兜底吞吐语义。
            }
            if (IsPrefixCommand(messageLower))
            {
                try
                {
                    var parts = message.Split(',');
                    if (parts.Length >= 1)
                    {
                        var cmd = parts[0].ToLowerInvariant();
                        int[] positions = new int[4];
                        for (int i = 1; i <= 4 && i < parts.Length; i++)
                        {
                            int.TryParse(parts[i], out positions[i - 1]);
                        }
                        if (cmd.StartsWith("pickfinished"))
                        {
                            StockDTO stockDTO = new StockDTO
                            {
                                SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
                                SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
                                TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
                                TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
                                Details = positions
                                        .Where(x => x > 0)
                                        .OrderBy(x => x)
                                        .Select((x, idx) => new StockDetailDTO
                                        {
                                            Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
                                            Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
                                            CellBarcode = state.CellBarcode[idx]
                                        })
                                        .ToList()
                            };
                            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)
                            {
                                state.CurrentAction = "PickFinished";
                            }
                        }
                        else if (cmd.StartsWith("putfinished"))
                        {
                            state.LastPutPositions = positions;
                            if (state.CurrentTask?.RobotTaskType == 1)
                            {
                                // 发送数据给WMS组盘/换盘
                                StockDTO stockDTO = new StockDTO
                                {
                                    SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
                                    SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
                                    TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
                                    TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
                                    Details = positions
                                        .Where(x => x > 0)
                                        .OrderBy(x => x)
                                        .Select((x, idx) => new StockDetailDTO
                                        {
                                            Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
                                            Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
                                            CellBarcode = state.CellBarcode[idx]
                                        })
                                        .ToList()
                                };
                                var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), nameof(ConfigKey.GroupPalletAsync));
                                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                                if (content.Status)
                                {
                                    state.CurrentAction = "PutFinished";
                                }
                            }
                        }
                    }
                }
                catch { }
                return null;
            }
            return null;
        }
        /// <summary>
        /// 机械手简单命令处理
        /// </summary>
        /// <param name="message"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        private bool IsSimpleCommand(string message, RobotSocketState state)
        {
            switch (message)
            {
                case "homing":
                    state.CurrentAction = "Homing";
                    return true;
                case "homed":
                    state.CurrentAction = "Homed";
                    return true;
                case "picking":
                    state.CurrentAction = "Picking";
                    return true;
                case "puting":
                    state.CurrentAction = "Putting";
                    return true;
                case "allpickfinished":
                    state.CurrentAction = "AllPickFinished";
                    if (state.CurrentTask?.RobotTaskType == 2 || state.CurrentTask?.RobotTaskType == 3)
                    {
                        // TODO 机械手取货完成,判断是否换盘、拆盘任务,创建空托盘回库任务
                    }
                    return true;
                case "allputfinished":
                    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
                        };
                    }
                    return true;
                case "running":
                    state.OperStatus = "Running";
                    return true;
                case "pausing":
                    state.OperStatus = "Pausing";
                    return true;
                case "warming":
                    state.OperStatus = "Warming";
                    return true;
                case "emstoping":
                    state.OperStatus = "Emstoping";
                    return true;
                case "runmode,1":
                    state.RobotRunMode = 1;
                    return true;
                case "runmode,2":
                    state.RobotRunMode = 2;
                    return true;
                case "controlmode,1":
                    state.RobotControlMode = 1;
                    return true;
                case "controlmode,2":
                    state.RobotControlMode = 2;
                    return true;
                case "armobject,1":
                    state.RobotArmObject = 1;
                    return true;
                case "armobject,0":
                    state.RobotArmObject = 0;
                    return true;
                default:
                    return false;
            }
        }
        /// <summary>
        /// 机械手前缀命令处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private static bool IsPrefixCommand(string message)
        {
            return message.StartsWith("pickfinished") || message.StartsWith("putfinished");
        }
        private Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
        {
            return _taskService.QueryRobotCraneTask(robotCrane.DeviceCode);
        }
    }
    public class RobotSocketState
    {
        public string IPAddress { get; set; } = string.Empty;
        /// <summary>
        /// 是否已订阅消息事件
        /// </summary>
        public bool IsEventSubscribed { get; set; }
        /// <summary>
        /// 机械手运行模式
        /// </summary>
        public int? RobotRunMode { get; set; }
        /// <summary>
        /// 机械手控制模式
        /// </summary>
        public int? RobotControlMode { get; set; }
        /// <summary>
        /// 机械手抓取对象
        /// </summary>
        public int? RobotArmObject { get; set; }
        /// <summary>
        /// 机械手设备信息
        /// </summary>
        public RobotCraneDevice? RobotCrane { get; set; }
        /// <summary>
        /// 当前动作
        /// </summary>
        public string? CurrentAction { get; set; }
        /// <summary>
        /// 当前状态
        /// </summary>
        public string? OperStatus { get; set; }
        /// <summary>
        /// 取货完成位置
        /// </summary>
        public int[]? LastPickPositions { get; set; }
        /// <summary>
        /// 放货完成位置
        /// </summary>
        public int[]? LastPutPositions { get; set; }
        /// <summary>
        /// 抓取位置条码
        /// </summary>
        public string[] CellBarcode { get; set; }
        /// <summary>
        /// 当前抓取任务
        /// </summary>
        public Dt_RobotTask? CurrentTask { get; set; }
    }
}
}