| | |
| | | using System.Collections.Concurrent; |
| | | using System.Net.Sockets; |
| | | using Microsoft.Extensions.Logging; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | using WIDESEAWCS_QuartzJob; |
| | | using WIDESEAWCS_Tasks.SocketServer; |
| | |
| | | /// 机械手状态管理器,用于读写设备状态 |
| | | /// </summary> |
| | | private readonly RobotStateManager _stateManager; |
| | | |
| | | /// <summary> |
| | | /// 日志记录器 |
| | | /// </summary> |
| | | private readonly ILogger _logger; |
| | | |
| | | /// <summary> |
| | | /// 跟踪已启动消息处理的客户端,避免重复启动 |
| | |
| | | /// </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> |
| | |
| | | // 清理该客户端的 HandleClientAsync 启动标志 |
| | | // 以便下次重连时可以重新启动处理 |
| | | _handleClientStarted.TryRemove(ipAddress, out _); |
| | | _logger.LogDebug("客户端未连接,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Debug($"客户端未连接,IP: {ipAddress}", robotCrane.DeviceName); |
| | | return false; |
| | | } |
| | | |
| | |
| | | { |
| | | // 绑定客户端断开连接的事件处理 |
| | | _tcpSocket.RobotReceived += OnRobotReceived; |
| | | // 记录日志(注意:日志内容为"客户端已断开连接",可能是遗留的占位文本) |
| | | QuartzLogger.Warn($"客户端已断开连接", robotCrane.DeviceName); |
| | | // 记录日志:事件订阅成功 |
| | | _logger.LogInformation("机械手TCP消息事件已订阅,设备: {DeviceName}", robotCrane.DeviceName); |
| | | QuartzLogger.Info($"机械手TCP消息事件已订阅", robotCrane.DeviceName); |
| | | } |
| | | |
| | | // 从 TCP 服务器的客户端字典中获取 TcpClient 对象 |
| | |
| | | { |
| | | // 移除启动标志,返回 false 表示客户端不可用 |
| | | _handleClientStarted.TryRemove(ipAddress, out _); |
| | | _logger.LogWarning("获取TcpClient失败,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Warn($"获取TcpClient失败,IP: {ipAddress}", robotCrane.DeviceName); |
| | | return false; |
| | | } |
| | | |
| | |
| | | // 如果尚未启动,则启动消息处理循环 |
| | | if (!alreadyStarted) |
| | | { |
| | | // 记录日志 |
| | | // 记录日志:启动消息处理 |
| | | _logger.LogInformation("启动客户端消息处理,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Info($"启动客户端消息处理", robotCrane.DeviceName); |
| | | |
| | | // 获取最新的状态对象 |
| | |
| | | if (t.IsFaulted) |
| | | { |
| | | // 记录错误日志 |
| | | QuartzLogger.Info($"监听客户端消息事件异常", 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 _); |
| | | } |
| | |
| | | // 移除该客户端的 HandleClientAsync 启动标志 |
| | | _handleClientStarted.TryRemove(clientId, out _); |
| | | |
| | | // 记录日志:客户端断开连接 |
| | | _logger.LogInformation("客户端断开连接,IP: {ClientId}", clientId); |
| | | QuartzLogger.Info($"客户端断开连接", clientId); |
| | | |
| | | // 重置该客户端的状态信息 |
| | | _stateManager.TryUpdateStateSafely(clientId, state => |
| | | { |