diff --git a/lib/core/network/dio_client.dart b/lib/core/network/dio_client.dart index 1c7b5d02..898cb898 100644 --- a/lib/core/network/dio_client.dart +++ b/lib/core/network/dio_client.dart @@ -9,8 +9,8 @@ class DioClient { final dio = Dio( BaseOptions( baseUrl: HttpApiConsts.baseUrl, - connectTimeout: const Duration(seconds: 12), - receiveTimeout: const Duration(seconds: 12), + connectTimeout: const Duration(seconds: 10), + receiveTimeout: const Duration(seconds: 10), headers: {'Content-Type': 'application/json'}, ), ); diff --git a/lib/core/network/tcp/tcp_client.dart b/lib/core/network/tcp/tcp_client.dart index 4c89881b..c50b298f 100644 --- a/lib/core/network/tcp/tcp_client.dart +++ b/lib/core/network/tcp/tcp_client.dart @@ -328,7 +328,7 @@ class TcpClient { (failure) { // 处理失败:打印日志或抛出异常 debugPrint('❌ [AuthTcp] 获取设备列表失败:$failure'); - _sendAuthPacket(); + // _sendAuthPacket(); throw Exception('获取设备列表失败:$failure'); }, (devices) async { @@ -349,7 +349,7 @@ class TcpClient { await switchResult.fold( (failure) { debugPrint('❌ [AuthTcp] 切换设备失败:$failure'); - _sendAuthPacket(); + // _sendAuthPacket(); throw Exception('切换设备失败:$failure'); }, (success) { diff --git a/lib/features/auth/presentation/bloc/auth_cubit.dart b/lib/features/auth/presentation/bloc/auth_cubit.dart index 1f9f92a7..4575dc20 100644 --- a/lib/features/auth/presentation/bloc/auth_cubit.dart +++ b/lib/features/auth/presentation/bloc/auth_cubit.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter/cupertino.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:get_it/get_it.dart'; import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart'; @@ -50,7 +51,8 @@ class AuthCubit extends Cubit { // 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();//包括发送认证包和获取列表和切换函数 + tcp.startHeartbeat(interval: const Duration(seconds: 4)); // 2. 同步全局 App 状态 appCubit.setAuth(user); // 3. 进入已登录状态 @@ -64,10 +66,10 @@ class AuthCubit extends Cubit { /// 当 HTTP 登录/注册成功后调用 Future loginSuccess(UserEntity user) async { await storage.saveUser(user); - await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); + //await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); // await _authTcpDatasource.sendAuthPacket(); //包括发送认证包和获取列表和切换函数 - tcp.startHeartbeat(interval: const Duration(seconds: 4)); //启动心跳 - appCubit.setAuth(user); + //tcp.startHeartbeat(interval: const Duration(seconds: 4)); //启动心跳 + // appCubit.setAuth(user); emit(AuthAuthenticated(user)); } @@ -92,6 +94,8 @@ class AuthCubit extends Cubit { }); } + + @override Future close() { _kickOutSub?.cancel(); // 销毁 Cubit 时关闭监听 diff --git a/lib/features/devices/presentation/bloc/devices_cubit.dart b/lib/features/devices/presentation/bloc/devices_cubit.dart index 4adba58c..8e956169 100644 --- a/lib/features/devices/presentation/bloc/devices_cubit.dart +++ b/lib/features/devices/presentation/bloc/devices_cubit.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'package:flutter/rendering.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:get_it/get_it.dart'; import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart'; import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart'; import 'package:maibu_satabot_v2/features/devices/domain/usecases/generate_path_usecase.dart'; @@ -173,6 +174,7 @@ class DevicesCubit extends Cubit { isLoading: false, ), ); + }); } catch (e) { emit(state.copyWith(isLoading: false, errorMessage: e.toString())); diff --git a/lib/features/home/presentation/pages/home_page.dart b/lib/features/home/presentation/pages/home_page.dart index 15943387..8e8604c3 100644 --- a/lib/features/home/presentation/pages/home_page.dart +++ b/lib/features/home/presentation/pages/home_page.dart @@ -1,9 +1,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:get_it/get_it.dart'; import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart'; import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart'; import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart'; // 导入设备状态类 +import '../../../../core/consts/tcp_consts.dart'; +import '../../../../core/network/tcp/tcp_client.dart'; import '../../../devices/domain/entities/device_entity.dart'; import '../widgets/ImmersionHeader.dart'; import '../widgets/quick_actions_grid.dart'; @@ -36,6 +39,11 @@ class HomePage extends StatelessWidget { final devicesCubit = context.read(); // 仅在设备列表为空或首次进入时请求(避免重复请求) devicesCubit.fetchAllDevices(username); + //context.read().connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: device.deviceName); + print("开始tcp连接。。。。"); + final tcpClient = GetIt.instance(); + tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: "MC700AIR-CN-JS-1762486208349-0000001A-FD"); + tcpClient.startHeartbeat(interval: const Duration(seconds: 4)); } });