USB 摄像头+远程遥控+中英的适配+topic的集成和状态的使用到相应页面

This commit is contained in:
2026-10-20 17:03:37 +08:00
parent 178cd2533c
commit 582242e9aa
144 changed files with 15759 additions and 406 deletions

View File

@@ -41,7 +41,10 @@ class TcpClient {
Timer? _heartbeatSendTimer; // 🔥 主动发送心跳定时器:每5秒给服务端发一次,防止服务端判定客户端失效
Timer? _reconnectTimer; // 重连定时器
int _reconnectAttempt = 0; // 🔥 重连尝试次数,用于退避计算
static const int _maxReconnectAttempts = 5; // 🔥 最大自动重连次数,防止网络不可达时无限重连
int _reconnectFailCount = 0; // 🔥 连续重连失败次数(含心跳超时),达到阈值后停止重连
int _reconnectFlapCount = 0; // 🔥 连续“连上又断”次数,识别成功-断开循环
static const int _maxReconnectFlaps = 5; // 🔥 最大允许连续成功-断开次数
/// 🔥 重连耗尽回调:连续4次重连失败(含心跳超时)后触发,通知上层弹窗提示用户
VoidCallback? onReconnectExhausted;
/// 🔥 重连开始回调:显示"TCP重连中"提示
@@ -156,6 +159,7 @@ class TcpClient {
_lastAuthPacketSentAt = null;
_reconnectAttempt = 0; // 🔥 重置重连退避
_reconnectFailCount = 0; // 🔥 重置重连失败计数
_reconnectFlapCount = 0; // 🔥 重置连上又断计数
// 5. 销毁 Socket
if (_socket != null) {
@@ -385,6 +389,15 @@ class TcpClient {
stopHeartbeat();
_socket = null;
// 🔥 每次断开都累计一次 flap(连上又断),识别成功-立即断开循环
_reconnectFlapCount++;
debugPrint('📊 [TCP] 连接断开次数: $_reconnectFlapCount/$_maxReconnectFlaps');
if (_reconnectFlapCount >= _maxReconnectFlaps) {
debugPrint('🛑 [TCP] 连续 $_maxReconnectFlaps 次连上后立刻断开,停止重连,通知用户退出登录');
_logger.logWithLevel('🛑 [TCP] 连续 $_maxReconnectFlaps 次连上后立刻断开,停止重连', shouldLog: true);
onReconnectExhausted?.call();
return;
}
// 🔥 连续失败达到4次,停止重连,通知上层弹窗
if (_reconnectFailCount >= 4) {
debugPrint('🛑 [TCP] 重连失败已达 4 次,停止重连,通知用户退出登录');
@@ -400,6 +413,14 @@ class TcpClient {
if (isUserSwitch) return;
if (_reconnectTimer != null) return;
// 🔥 限制总重连次数,防止网络不可达时无限重连
if (_reconnectAttempt >= _maxReconnectAttempts) {
debugPrint('🛑 [TCP] 自动重连已达最大次数 $_maxReconnectAttempts 次,停止重连,通知用户退出登录');
_logger.logWithLevel('🛑 [TCP] 自动重连已达最大次数 $_maxReconnectAttempts 次,停止重连', shouldLog: true);
onReconnectExhausted?.call();
return;
}
// 🔥 重连退避:5s → 10s → 20s → 30s(max)
final delay = [5, 10, 20, 30][_reconnectAttempt.clamp(0, 3)];
_reconnectAttempt++;
@@ -540,11 +561,31 @@ class TcpClient {
..addByte(0xAA)
..addByte(0xAB);
_socket!.add(builder.takeBytes());
// 🔥 关键修复:强制flush,确保数据立即发送
_socket!.flush();
final packet = builder.takeBytes();
try {
_socket!.add(packet);
// 🔥 关键修复:强制flush,确保数据立即发送
_socket!.flush();
// 联调日志:移到 add+flush 之后打印,证明这帧已真正交给系统 socket 发送缓冲
// ("发出去之后"才有参考价值;仍非后端 ACK,仅代表 App 侧写入成功)(联调结束可移除)
_logger.logWithLevel(
'🔗 [联调-TCP] sendRaw ✅已写入socket cmd=0x${cmd.toRadixString(16).padLeft(2, '0')} '
'len=${packet.length} frameHex=${_hex(packet)}',
shouldLog: true,
);
} catch (e) {
_logger.logWithLevel(
'🔗 [联调-TCP] sendRaw ❌写入socket失败 cmd=0x${cmd.toRadixString(16).padLeft(2, '0')} err=$e',
level: 'ERROR',
shouldLog: true,
);
}
}
/// 字节转十六进制字符串,供联调对包使用
String _hex(List<int> bytes) =>
bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ');
void sendHeartbeat() {
if (_socket == null) return;
final builder = BytesBuilder()
@@ -580,6 +621,7 @@ class TcpClient {
/// 重置心跳超时定时器
/// 每次收到服务端心跳(0xFF)时调用,重新开始计时
void _resetHeartbeatTimeout() {
final wasRunning = _heartbeatTimer != null;
_heartbeatTimer?.cancel();
_heartbeatTimer = Timer(_heartbeatTimeout, () {
debugPrint('⚠️ [TCP] 心跳超时 ${_heartbeatTimeout.inSeconds}s 未收到服务端心跳,判定连接断开');
@@ -595,6 +637,12 @@ class TcpClient {
// 🔥 统一走普通断开重连路径(_scheduleReconnect 已包含 HTTP switchDevice)
_handleDisconnect();
});
// 🔥 收到心跳说明连接真正稳定,重置“连上又断”计数
if (wasRunning && _reconnectFlapCount > 0) {
_reconnectFlapCount = 0;
debugPrint('📊 [TCP] 收到心跳,连接稳定,重置连接断开计数');
}
}
/// 🔥 封装完整的TCP初始化方法:连接 + 认证 + 心跳
@@ -784,6 +832,7 @@ class TcpClient {
_connectionCompleter = null;
_reconnectAttempt = 0; // 🔥 重置重连退避
_reconnectFailCount = 0; // 🔥 重置重连失败计数
_reconnectFlapCount = 0; // 🔥 重置连上又断计数
if (_socket != null) {
_socket!.destroy();
_socket = null;