wanshenmean
3 天以前 5e851678cc02257bbbd179446de36082430ca5bc
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotClientManager.cs
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.SocketServer;
@@ -27,6 +28,11 @@
        /// 机械手状态管理器,用于读写设备状态
        /// </summary>
        private readonly RobotStateManager _stateManager;
        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger _logger;
        /// <summary>
        /// 跟踪已启动消息处理的客户端,避免重复启动
@@ -59,10 +65,12 @@
        /// </summary>
        /// <param name="tcpSocket">TCP Socket 服务器实例</param>
        /// <param name="stateManager">状态管理器实例</param>
        public RobotClientManager(TcpSocketServer tcpSocket, RobotStateManager stateManager)
        /// <param name="logger">日志记录器</param>
        public RobotClientManager(TcpSocketServer tcpSocket, RobotStateManager stateManager, ILogger logger)
        {
            _tcpSocket = tcpSocket;
            _stateManager = stateManager;
            _logger = logger;
        }
        /// <summary>
@@ -92,6 +100,8 @@
                // 清理该客户端的 HandleClientAsync 启动标志
                // 以便下次重连时可以重新启动处理
                _handleClientStarted.TryRemove(ipAddress, out _);
                _logger.LogDebug("客户端未连接,IP: {IpAddress}", ipAddress);
                QuartzLogger.Debug($"客户端未连接,IP: {ipAddress}", robotCrane.DeviceName);
                return false;
            }
@@ -101,8 +111,9 @@
            {
                // 绑定客户端断开连接的事件处理
                _tcpSocket.RobotReceived += OnRobotReceived;
                // 记录日志(注意:日志内容为"客户端已断开连接",可能是遗留的占位文本)
                QuartzLogger.Error($"客户端已断开连接", robotCrane.DeviceName);
                // 记录日志:事件订阅成功
                _logger.LogInformation("机械手TCP消息事件已订阅,设备: {DeviceName}", robotCrane.DeviceName);
                QuartzLogger.Info($"机械手TCP消息事件已订阅", robotCrane.DeviceName);
            }
            // 从 TCP 服务器的客户端字典中获取 TcpClient 对象
@@ -114,6 +125,8 @@
            {
                // 移除启动标志,返回 false 表示客户端不可用
                _handleClientStarted.TryRemove(ipAddress, out _);
                _logger.LogWarning("获取TcpClient失败,IP: {IpAddress}", ipAddress);
                QuartzLogger.Warn($"获取TcpClient失败,IP: {ipAddress}", robotCrane.DeviceName);
                return false;
            }
@@ -123,8 +136,9 @@
            // 如果尚未启动,则启动消息处理循环
            if (!alreadyStarted)
            {
                // 记录日志
                QuartzLogger.Error($"启动客户端消息处理", robotCrane.DeviceName);
                // 记录日志:启动消息处理
                _logger.LogInformation("启动客户端消息处理,IP: {IpAddress}", ipAddress);
                QuartzLogger.Info($"启动客户端消息处理", robotCrane.DeviceName);
                // 获取最新的状态对象
                var latestStateForSubscribe = _stateManager.GetState(ipAddress);
@@ -142,8 +156,8 @@
                            if (t.IsFaulted)
                            {
                                // 记录错误日志
                                QuartzLogger.Error($"监听客户端消息事件异常", robotCrane.DeviceName);
                                Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] HandleClientAsync error: {t.Exception?.GetBaseException().Message}");
                                _logger.LogError(t.Exception, "监听客户端消息事件异常,IP: {IpAddress}", ipAddress);
                                QuartzLogger.Error($"监听客户端消息事件异常", robotCrane.DeviceName, t.Exception);
                                // 发生错误时,移除启动标志,允许下次重试
                                _handleClientStarted.TryRemove(ipAddress, out _);
                            }
@@ -179,6 +193,10 @@
            // 移除该客户端的 HandleClientAsync 启动标志
            _handleClientStarted.TryRemove(clientId, out _);
            // 记录日志:客户端断开连接
            _logger.LogInformation("客户端断开连接,IP: {ClientId}", clientId);
            QuartzLogger.Info($"客户端断开连接", clientId);
            // 重置该客户端的状态信息
            _stateManager.TryUpdateStateSafely(clientId, state =>
            {