| | |
| | | using Serilog; |
| | | using System.Collections.Concurrent; |
| | | using System.Net.Sockets; |
| | | using Microsoft.Extensions.Logging; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | using WIDESEAWCS_Model.Models; |
| | | using WIDESEAWCS_QuartzJob; |
| | | using WIDESEAWCS_Tasks.SocketServer; |
| | | |
| | |
| | | // 清理该客户端的 HandleClientAsync 启动标志 |
| | | // 以便下次重连时可以重新启动处理 |
| | | _handleClientStarted.TryRemove(ipAddress, out _); |
| | | _logger.LogDebug("客户端未连接,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Debug($"客户端未连接,IP: {ipAddress}", robotCrane.DeviceName); |
| | | QuartzLogHelper.LogDebug(_logger, $"客户端未连接,IP: {ipAddress}", robotCrane.DeviceName); |
| | | return false; |
| | | } |
| | | |
| | |
| | | // 绑定客户端断开连接的事件处理 |
| | | _tcpSocket.RobotReceived += OnRobotReceived; |
| | | // 记录日志:事件订阅成功 |
| | | _logger.LogInformation("机械手TCP消息事件已订阅,设备: {DeviceName}", robotCrane.DeviceName); |
| | | QuartzLogger.Info($"机械手TCP消息事件已订阅", robotCrane.DeviceName); |
| | | QuartzLogHelper.LogInfo(_logger, $"机械手TCP消息事件已订阅", robotCrane.DeviceName); |
| | | } |
| | | |
| | | // 从 TCP 服务器的客户端字典中获取 TcpClient 对象 |
| | |
| | | { |
| | | // 移除启动标志,返回 false 表示客户端不可用 |
| | | _handleClientStarted.TryRemove(ipAddress, out _); |
| | | _logger.LogWarning("获取TcpClient失败,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Warn($"获取TcpClient失败,IP: {ipAddress}", robotCrane.DeviceName); |
| | | QuartzLogHelper.LogWarn(_logger, $"获取TcpClient失败,IP: {ipAddress}", robotCrane.DeviceName); |
| | | return false; |
| | | } |
| | | |
| | |
| | | if (!alreadyStarted) |
| | | { |
| | | // 记录日志:启动消息处理 |
| | | _logger.LogInformation("启动客户端消息处理,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Info($"启动客户端消息处理", robotCrane.DeviceName); |
| | | QuartzLogHelper.LogInfo(_logger, $"启动客户端消息处理,IP: {ipAddress}", robotCrane.DeviceName); |
| | | |
| | | // 获取最新的状态对象 |
| | | var latestStateForSubscribe = _stateManager.GetState(ipAddress); |
| | |
| | | if (t.IsFaulted) |
| | | { |
| | | // 记录错误日志 |
| | | _logger.LogError(t.Exception, "监听客户端消息事件异常,IP: {IpAddress}", ipAddress); |
| | | QuartzLogger.Error($"监听客户端消息事件异常", robotCrane.DeviceName, t.Exception); |
| | | QuartzLogHelper.LogError(_logger, t.Exception, $"监听客户端消息事件异常,IP: {ipAddress}", $"监听客户端消息事件异常,IP: {ipAddress}", robotCrane.DeviceName); |
| | | // 发生错误时,移除启动标志,允许下次重试 |
| | | _handleClientStarted.TryRemove(ipAddress, out _); |
| | | } |
| | |
| | | _handleClientStarted.TryRemove(clientId, out _); |
| | | |
| | | // 记录日志:客户端断开连接 |
| | | _logger.LogInformation("客户端断开连接,IP: {ClientId}", clientId); |
| | | QuartzLogger.Info($"客户端断开连接", clientId); |
| | | QuartzLogHelper.LogInfo(_logger, $"客户端断开连接,IP: {clientId}", clientId); |
| | | |
| | | // 重置该客户端的状态信息 |
| | | _stateManager.TryUpdateStateSafely(clientId, state => |