优化和路径规划新适配

This commit is contained in:
2026-07-11 16:46:53 +08:00
parent 2124f3bc02
commit 1a4d0aedfd
43 changed files with 2454 additions and 685 deletions

View File

@@ -833,21 +833,24 @@ class TcpClient {
return;
}
isUserSwitch = true;
_isSwitching = true; // 🔥 关键修复:防止旧 socket 的 onDone/onError 触发重连
_connecting = true; // 🔥 防止 connect() 并发创建新连接
debugPrint('🔌 [TCP-connectBySwitch] 设置 _connecting=true');
// 🔥 关键修复:如果 TCP 已连接,先断开旧连接再创建新连接
// 原因:旧连接可能是 _handleResume() 创建的“空连接”(没有关联设备),
// 服务端残留的 session 与当前连接不匹配,导致推送 have_logged_in
// 🔥 核心优化:如果 TCP 已连接,复用现有连接,只调 HTTP switchDevice
// 不再销毁重建,避免推送空窗期导致延迟
if (_socket != null) {
debugPrint('🔌 [TCP-connectBySwitch] 检测到已有连接,先断开再创建新连接(确保服务端 session 干净)');
stopHeartbeat();
_socket!.destroy();
_socket = null;
_isSwitching = false; // 防止 onDone 触发重连
debugPrint('✅ [TCP-connectBySwitch] 已有连接,复用现有连接,只调 HTTP switchDevice: $deviceName');
_logger.logWithLevel('✅ [TCP-connectBySwitch] 复用连接,HTTP切换设备: $deviceName', shouldLog: true);
try {
await switchDeviceUseCase.deviceRepository.switchDevice("app", deviceName);
debugPrint('✅ [TCP-connectBySwitch] HTTP切换成功: $deviceName');
} catch (e) {
debugPrint('❌ [TCP-connectBySwitch] HTTP切换失败: $e');
_logger.logWithLevel('❌ [TCP-connectBySwitch] HTTP切换失败: $e', shouldLog: true);
}
return;
}
isUserSwitch = true;
_connecting = true; // 🔥 防止 connect() 并发创建新连接
debugPrint('🔌 [TCP-connectBySwitch] 无现有连接,开始新建');
// debugPrint('🔌被动 [TCP] 开始连接:$host:$port'); // ✅ 必须看到这条
_logger.logWithLevel('🔌被动 [TCP] 开始连接:$host:$port');
@@ -995,6 +998,7 @@ class TcpClient {
_lastAuthPacketSentAt = DateTime.now(); // 🔥 记录发送 0x03 的时间
_socket!.add(builder.takeBytes());
await _socket!.flush(); // 🔥 关键修复:强制flush,确保认证包立即到达服务端
//debugPrint('🔑 被动[TCP] 已发送认证包 (0x03): $authString');
_logger.logWithLevel('🔑 被动[TCP] 已发送认证包 (0x03): $authString');