首页的权限交接的弹窗(关闭了释放权限)+添加日志过滤功能,支持通过 shouldLog参数控制日志输出
This commit is contained in:
@@ -48,7 +48,7 @@ class TcpClient {
|
||||
if (forSwitch) {
|
||||
_isSwitching = true;
|
||||
//debugPrint('🚫 [TCP] 标记为切换断开,将禁止自动重连');
|
||||
_logger.logWithLevel('🚫 [TCP] 标记为切换断开,将禁止自动重连');
|
||||
_logger.logWithLevel('🚫 [TCP] 标记为切换断开,将禁止自动重连', shouldLog: true);
|
||||
}
|
||||
|
||||
// 🔥 关键:立即取消可能已经存在或即将触发的重连定时器
|
||||
@@ -57,7 +57,7 @@ class TcpClient {
|
||||
|
||||
if (_socket != null) {
|
||||
//debugPrint('🔌 [TCP] 物理断开 Socket...');
|
||||
_logger.logWithLevel('🔌 [TCP] 物理断开 Socket...');
|
||||
_logger.logWithLevel('🔌 [TCP] 物理断开 Socket...', shouldLog: true);
|
||||
_socket!.destroy(); // 或者 .close()
|
||||
_socket = null;
|
||||
}
|
||||
@@ -65,7 +65,7 @@ class TcpClient {
|
||||
// 通过参数配置,不硬编码
|
||||
Future<void> connect({required String host, required int port}) async {
|
||||
//debugPrint('🔌 [TCP] 开始连接:$host:$port'); // ✅ 必须看到这条
|
||||
_logger.logWithLevel('🔌 [TCP] 开始连接:$host:$port');
|
||||
_logger.logWithLevel('🔌 [TCP] 开始连接:$host:$port', shouldLog: true);
|
||||
_lastHost = host;
|
||||
_lastPort = port;
|
||||
try {
|
||||
@@ -76,8 +76,8 @@ class TcpClient {
|
||||
);
|
||||
// 🔥 关键修复:禁用Nagle算法,确保小包立即发送
|
||||
_socket!.setOption(SocketOption.tcpNoDelay, true);
|
||||
debugPrint('✅ [TCP] 连接成功!'); // ✅ 必须看到这条
|
||||
_logger.logWithLevel('✅ [TCP] 连接成功!');
|
||||
// debugPrint('✅ [TCP] 连接成功!'); // ✅ 必须看到这条
|
||||
_logger.logWithLevel('✅ [TCP] 连接成功!', shouldLog: true);
|
||||
// 开始认证tcp
|
||||
await _sendAuthPacket();
|
||||
_socket!.listen((data) {
|
||||
@@ -97,28 +97,32 @@ class TcpClient {
|
||||
try {
|
||||
var packets = _decoder.decode(data);
|
||||
//debugPrint('📦 [TCP] 解码成功,包数量:${packets.length}');
|
||||
_logger.logWithLevel('✅ [TCP] 解码成功,包数量:${packets.length}');
|
||||
_logger.logWithLevel('✅ [TCP] 解码成功,包数量:${packets.length}',shouldLog: true);
|
||||
for (var packet in packets) {
|
||||
// 🔥 最根部日志:收到任何推送都打印
|
||||
// debugPrint('📥 [TCP-ROOT] 收到推送 CMD: 0x${packet.command.toRadixString(16).toUpperCase()}, Payload长度: ${packet.payload.length}');
|
||||
_logger.logWithLevel('📥 [TCP-ROOT] 收到推送 CMD: 0x${packet.command.toRadixString(16).toUpperCase()}, Payload长度: ${packet.payload.length}', shouldLog: true);
|
||||
|
||||
if (!_controller.isClosed) {
|
||||
_controller.add(packet);
|
||||
//debugPrint('➡️ [TCP] 已分发 CMD: 0x${packet.command.toRadixString(16)}');
|
||||
_logger.logWithLevel('✅ [TCP] 已分发 CMD: 0x${packet.command.toRadixString(16)}');
|
||||
_logger.logWithLevel('✅ [TCP] 已分发 CMD: 0x${packet.command.toRadixString(16)}' ,shouldLog: true);
|
||||
}
|
||||
if (packet.command == 0xFF) {
|
||||
//debugPrint('收到服务端心跳,自动回复...');
|
||||
_logger.logWithLevel('✅ [TCP] 收到服务端心跳,自动回复...');
|
||||
_logger.logWithLevel('✅ [TCP] 收到服务端心跳,自动回复...',shouldLog: true);
|
||||
sendHeartbeat(); // 回复 AB AA FF AA AB
|
||||
}
|
||||
if (packet.command == 0x03) {
|
||||
// debugPrint('⚠️ 收到认证响应:${packet.payload}');
|
||||
_logger.logWithLevel('⚠️ 收到认证响应:${packet.payload}');
|
||||
_logger.logWithLevel('⚠️ 收到认证响应:${packet.payload}',shouldLog: true);
|
||||
// 解析 payload 看是否有错误信息
|
||||
}
|
||||
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('❌ [TCP] 解码数据时发生异常:$e\n$stackTrace'); // 🔥 捕获解码异常
|
||||
_logger.logWithLevel('❌ [TCP] 解码数据时发生异常:$e\n$stackTrace');
|
||||
// debugPrint('❌ [TCP] 解码数据时发生异常:$e\n$stackTrace'); // 🔥 捕获解码异常
|
||||
_logger.logWithLevel('❌ [TCP] 解码数据时发生异常:$e\n$stackTrace', shouldLog: true);
|
||||
}
|
||||
},
|
||||
onDone: (){
|
||||
@@ -126,7 +130,7 @@ class TcpClient {
|
||||
// debugPrint('来到断线重连!');
|
||||
if (!_isSwitching) {
|
||||
//debugPrint('onDone❌ [TCP] 连接已断开!');
|
||||
_logger.logWithLevel('❌ [TCP] 连接已断开!');
|
||||
_logger.logWithLevel('❌ [TCP] 连接已断开!', shouldLog: true);
|
||||
_handleDisconnect();
|
||||
}
|
||||
_isSwitching = false; // 重置标志,以免影响下次
|
||||
@@ -134,10 +138,10 @@ class TcpClient {
|
||||
},
|
||||
onError: (e) {
|
||||
//debugPrint('来到断线重连!error');
|
||||
_logger.logWithLevel('❌ [TCP] 发生错误:$e');
|
||||
_logger.logWithLevel('❌ [TCP] 发生错误:$e', shouldLog: true);
|
||||
if (!_isSwitching) {
|
||||
//debugPrint('❌ [TCP] 发生错误:$e');
|
||||
_logger.logWithLevel('❌ [TCP] 发生错误:$e');
|
||||
_logger.logWithLevel('❌ [TCP] 发生错误:$e', shouldLog: true);
|
||||
_handleDisconnect();
|
||||
} // 统一走重连逻辑,保护 Controller 不被关闭
|
||||
_isSwitching = false; // 重置标志,以免影响下次
|
||||
@@ -167,14 +171,14 @@ class TcpClient {
|
||||
_logger.logWithLevel('⏳ 调度重连:Host=${_lastHost}, Port=${_lastPort}');
|
||||
if (_lastHost == null || _lastPort == null) {
|
||||
//debugPrint('❌ 无法重连:Host 或 Port 为空!');
|
||||
_logger.logWithLevel('❌ 无法重连:Host 或 Port 为空!');
|
||||
_logger.logWithLevel('❌ 无法重连:Host 或 Port 为空!', shouldLog: true);
|
||||
return;
|
||||
}
|
||||
|
||||
_reconnectTimer = Timer(const Duration(seconds: 5), () {
|
||||
_reconnectTimer = null;
|
||||
//debugPrint('⏰ 定时器触发,开始执行重连...');
|
||||
_logger.logWithLevel('⏰ 定时器触发,开始执行重连...');
|
||||
_logger.logWithLevel('⏰ 定时器触发,开始执行重连...', shouldLog: true);
|
||||
connect(host: _lastHost!, port: _lastPort!);
|
||||
});
|
||||
await _sendAuthPacket();
|
||||
@@ -185,17 +189,17 @@ class TcpClient {
|
||||
Future<void> _scheduleReconnectBySwitch(devname) async {
|
||||
|
||||
|
||||
debugPrint('⏳ 被动调度重连:Host=${_lastHost}, Port=${_lastPort}'); // ✅ 检查 Host/Port 是否为空
|
||||
//debugPrint('⏳ 被动调度重连:Host=${_lastHost}, Port=${_lastPort}'); // ✅ 检查 Host/Port 是否为空
|
||||
if (_lastHost == null || _lastPort == null) {
|
||||
// debugPrint('❌ 无法重连:Host 或 Port 为空!');
|
||||
_logger.logWithLevel('❌ 无法重连:Host 或 Port 为空!');
|
||||
_logger.logWithLevel('❌ 无法重连:Host 或 Port 为空!', shouldLog: true);
|
||||
return;
|
||||
}
|
||||
|
||||
_reconnectTimer = Timer(const Duration(seconds: 5), () {
|
||||
_reconnectTimer = null;
|
||||
//debugPrint('⏰ 被动-定时器触发,开始执行重连...');
|
||||
_logger.logWithLevel('⏰ 被动-定时器触发,开始执行重连...');
|
||||
_logger.logWithLevel('⏰ 被动-定时器触发,开始执行重连...', shouldLog: true);
|
||||
connectBySwitch(host: _lastHost!, port: _lastPort!, deviceName: devname);
|
||||
});
|
||||
await _sendAuthPacketBySwitch(devname);
|
||||
@@ -246,7 +250,7 @@ class TcpClient {
|
||||
void startHeartbeat({Duration interval = const Duration(seconds: 4)}) {
|
||||
if (_heartbeatTimer != null) return; // 防止重复启动
|
||||
//debugPrint('收到服务端心跳,自动回复...');
|
||||
_logger.logWithLevel('✅ [TCP] 收到服务端心跳,自动回复...');
|
||||
_logger.logWithLevel('✅ [TCP] 收到服务端心跳,自动回复...', shouldLog: true);
|
||||
_heartbeatTimer = Timer.periodic(interval, (_) {
|
||||
// 发送心跳帧:AB AA FF 00 00 AA AB(与 sendRaw 一致)
|
||||
//sendRaw(0xFF, []);
|
||||
|
||||
Reference in New Issue
Block a user