完成解决日志sentry日志代码被覆盖的问题
完成切换后台App获取列表和TCP连接后数据响应慢的问题。
This commit is contained in:
@@ -28,7 +28,7 @@ class AuthTcpDatasourceImpl implements AuthTcpDatasource {
|
||||
@override
|
||||
Future<void> sendAuthPacket() async {
|
||||
if (!_tcpClient.isConnected) {
|
||||
throw Exception('TCP 未连接,无法发送认证包');
|
||||
throw Exception('TCP 未连接,无法发送认证包sendAuthPacket');
|
||||
}
|
||||
|
||||
final user = await _userStorage.getUser();
|
||||
@@ -40,7 +40,7 @@ class AuthTcpDatasourceImpl implements AuthTcpDatasource {
|
||||
final token = user.token!;
|
||||
final authString = '$username:app:$token';
|
||||
|
||||
debugPrint('🔑 [AuthTcp] 准备发送认证包:$authString');
|
||||
debugPrint('🔑 sendAuthPacket[AuthTcp] 准备发送认证包:$authString');
|
||||
|
||||
final authBytes = utf8.encode(authString);
|
||||
|
||||
@@ -57,7 +57,7 @@ class AuthTcpDatasourceImpl implements AuthTcpDatasource {
|
||||
..addByte(0xAB);
|
||||
|
||||
_tcpClient.sendRawBytes(builder.takeBytes()); // 假设 TcpClient 增加了此方法,或用 socket.add
|
||||
debugPrint('✅ [AuthTcp] 认证包已发送');
|
||||
debugPrint('✅ sendAuthPacket[AuthTcp] 认证包已发送');
|
||||
|
||||
// 获取用户设备列表
|
||||
try {
|
||||
@@ -70,36 +70,36 @@ class AuthTcpDatasourceImpl implements AuthTcpDatasource {
|
||||
await eitherResult.fold(
|
||||
(failure) {
|
||||
// 处理失败:打印日志或抛出异常
|
||||
debugPrint('❌ [AuthTcp] 获取设备列表失败:$failure');
|
||||
debugPrint('❌ sendAuthPacket [AuthTcp] 获取设备列表失败:$failure');
|
||||
throw Exception('获取设备列表失败:$failure');
|
||||
},
|
||||
(devices) async {
|
||||
// 处理成功:devices 现在是真正的 List<DeviceEntity>
|
||||
if (devices.isEmpty) {
|
||||
debugPrint('⚠️ [AuthTcp] 当前用户无可用设备,跳过切换步骤');
|
||||
debugPrint('⚠️ sendAuthPacket[AuthTcp] 当前用户无可用设备,跳过切换步骤');
|
||||
return;
|
||||
}
|
||||
|
||||
// 取第一个设备
|
||||
final DeviceEntity targetDevice = devices.first;
|
||||
debugPrint('📱 [AuthTcp] 准备切换至默认设备:${targetDevice.deviceName}');
|
||||
debugPrint('📱 sendAuthPacket[AuthTcp] 准备切换至默认设备:${targetDevice.deviceName}');
|
||||
|
||||
// 切换设备 (同样,如果 switchDeviceUseCase 也返回 Either,也需要 fold 处理)
|
||||
final switchResult = await switchDeviceUseCase.deviceRepository.switchDevice("app",targetDevice.deviceName);
|
||||
|
||||
await switchResult.fold(
|
||||
(failure) {
|
||||
debugPrint('❌ [AuthTcp] 切换设备失败:$failure');
|
||||
debugPrint('❌ sendAuthPacket[AuthTcp] 切换设备失败:$failure');
|
||||
throw Exception('切换设备失败:$failure');
|
||||
},
|
||||
(success) {
|
||||
debugPrint('✅ [AuthTcp] 设备切换成功,服务端应开始推送数据');
|
||||
debugPrint('✅ sendAuthPacket[AuthTcp] 设备切换成功,服务端应开始推送数据');
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [AuthTcp] 设备订阅流程异常:$e');
|
||||
debugPrint('❌ sendAuthPacket[AuthTcp] 设备订阅流程异常:$e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/entities/user_entity.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/net_message_dispatcher.dart';
|
||||
|
||||
import '../../../../core/app/app_user_cubit.dart';
|
||||
import '../../../../core/infrastructure/logging/sentry_logger_impl.dart';
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../../../core/storage/user_storage.dart';
|
||||
import '../../../devices/domain/usecases/get_user_device_usecase.dart';
|
||||
@@ -34,16 +37,27 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
|
||||
/// App 启动时检查本地缓存
|
||||
Future<void> appStarted() async {
|
||||
final logger = GetIt.I<ILoggerService>() as SentryLoggerImpl;
|
||||
// print("App 启动时检查本地缓存");
|
||||
final user = await storage.getUser();
|
||||
logger.logWithLevel(
|
||||
'启动时检查本地缓存',
|
||||
level: 'INFO',
|
||||
data: {'user': user, 'data': user}
|
||||
);
|
||||
if (user != null) {
|
||||
// 1. 建立 TCP 连接 (远程控制)
|
||||
// print("App 启动时检查本地缓存user != null) ");
|
||||
// 1. print("App 启动时检查本地缓存user != null) ");
|
||||
// print("App 启动时检查本地缓存 tcp.connect) ");
|
||||
tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT);
|
||||
//await _authTcpDatasource.sendAuthPacket();//包括发送认证包和获取列表和切换函数
|
||||
|
||||
await _authTcpDatasource.sendAuthPacket();//包括发送认证包和获取列表和切换函数
|
||||
// 2. 同步全局 App 状态
|
||||
appCubit.setAuth(user);
|
||||
// 3. 进入已登录状态
|
||||
emit(AuthAuthenticated(user));
|
||||
} else {
|
||||
// print("App 启动时检查本地缓存user=null) ");
|
||||
emit(AuthUnauthenticated());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user