优化+摄像头+扫码绑定

This commit is contained in:
2026-08-10 10:00:52 +08:00
parent db70665ced
commit 9dc445eeb5
41 changed files with 2618 additions and 438 deletions

View File

@@ -44,6 +44,10 @@ class TcpClient {
int _reconnectFailCount = 0; // 🔥 连续重连失败次数(含心跳超时),达到阈值后停止重连
/// 🔥 重连耗尽回调:连续4次重连失败(含心跳超时)后触发,通知上层弹窗提示用户
VoidCallback? onReconnectExhausted;
/// 🔥 重连开始回调:显示"TCP重连中"提示
VoidCallback? onReconnectStarted;
/// 🔥 重连成功回调:显示"连接成功"提示
VoidCallback? onReconnectSuccess;
static const Duration _heartbeatTimeout = Duration(seconds: 15); // 心跳超时时间
String? _lastHost;
@@ -418,11 +422,25 @@ class TcpClient {
_logger.logWithLevel('✅ [TCP-重连定时器] 用户切换中,跳过', shouldLog: true);
return;
}
_logger.logWithLevel('⏰ 定时器触发,开始执行重连...', shouldLog: true);
// 🔥 显示"TCP重连中"提示
onReconnectStarted?.call();
final reconnectStart = DateTime.now();
final timeStr = '${reconnectStart.hour.toString().padLeft(2,'0')}:${reconnectStart.minute.toString().padLeft(2,'0')}:${reconnectStart.second.toString().padLeft(2,'0')}.${reconnectStart.millisecond.toString().padLeft(3,'0')}';
debugPrint('⏰ [TCP-重连] $timeStr 开始重连(第$_reconnectAttempt次,退避${delay}s)');
_logger.logWithLevel('⏰ [TCP-重连] $timeStr 开始重连(第$_reconnectAttempt次,退避${delay}s)', shouldLog: true);
try {
await connect(host: _lastHost!, port: _lastPort!);
final connectDone = DateTime.now();
final connectMs = connectDone.difference(reconnectStart).inMilliseconds;
debugPrint('✅ [TCP-重连] ${connectDone.hour.toString().padLeft(2,'0')}:${connectDone.minute.toString().padLeft(2,'0')}:${connectDone.second.toString().padLeft(2,'0')}.${connectDone.millisecond.toString().padLeft(3,'0')} connect() 完成,耗时 ${connectMs}ms');
_reconnectAttempt = 0; // 🔥 重连成功,重置退避计数
// 🔥 重连成功后,补调 HTTP switchDevice 告知服务端推送目标设备
// 🔥 关键修复:等待5秒再调 HTTP switchDevice,给服务端时间完成相关操作
final delayStart = DateTime.now();
debugPrint('⏳ [TCP-重连] ${delayStart.hour.toString().padLeft(2,'0')}:${delayStart.minute.toString().padLeft(2,'0')}:${delayStart.second.toString().padLeft(2,'0')}.${delayStart.millisecond.toString().padLeft(3,'0')} 开始等待5秒...');
await Future.delayed(const Duration(milliseconds: 5000));
final afterDelay = DateTime.now();
final actualDelayMs = afterDelay.difference(delayStart).inMilliseconds;
debugPrint('⏳ [TCP-重连] ${afterDelay.hour.toString().padLeft(2,'0')}:${afterDelay.minute.toString().padLeft(2,'0')}:${afterDelay.second.toString().padLeft(2,'0')}.${afterDelay.millisecond.toString().padLeft(3,'0')} 等待结束,实际耗时 ${actualDelayMs}ms,开始调 HTTP switchDevice');
try {
final remoteControlCubit = GetIt.I<RemoteControlCubit>();
final targetDevice = remoteControlCubit.state.targetDevice;
@@ -430,10 +448,14 @@ class TcpClient {
debugPrint('🔄 [TCP-重连] 补调 HTTP switchDevice: ${targetDevice.deviceName}');
_logger.logWithLevel('🔄 [TCP-重连] 补调 HTTP switchDevice: ${targetDevice.deviceName}', shouldLog: true);
await switchDeviceUseCase.deviceRepository.switchDevice('app', targetDevice.deviceName);
debugPrint('✅ [TCP-重连] HTTP switchDevice 成功');
final switchDone = DateTime.now();
final totalMs = switchDone.difference(reconnectStart).inMilliseconds;
debugPrint('✅ [TCP-重连] ${switchDone.hour.toString().padLeft(2,'0')}:${switchDone.minute.toString().padLeft(2,'0')}:${switchDone.second.toString().padLeft(2,'0')}.${switchDone.millisecond.toString().padLeft(3,'0')} HTTP switchDevice 成功,重连总耗时 ${totalMs}ms');
} else {
debugPrint('⚠️ [TCP-重连] targetDevice 为 null,跳过 switchDevice');
}
// 🔥 显示"连接成功"提示
onReconnectSuccess?.call();
} catch (e) {
debugPrint('❌ [TCP-重连] HTTP switchDevice 失败: $e');
_logger.logWithLevel('❌ [TCP-重连] HTTP switchDevice 失败: $e', shouldLog: true);