Merge branch 'feature/my' of http://1.95.137.212:57001/APP/FlutterApp into feature/my
This commit is contained in:
@@ -185,8 +185,8 @@ Future<void> init() async {
|
|||||||
sl.registerLazySingleton(() => AppUserCubit()); // AuthCubit 依赖它,必须先注册
|
sl.registerLazySingleton(() => AppUserCubit()); // AuthCubit 依赖它,必须先注册
|
||||||
sl.registerLazySingleton(() => GetDeviceLocationUseCase(sl()));
|
sl.registerLazySingleton(() => GetDeviceLocationUseCase(sl()));
|
||||||
sl.registerLazySingleton(() => DevicesCubit(sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(),sl(),sl(),sl()));
|
sl.registerLazySingleton(() => DevicesCubit(sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(),sl(),sl(),sl()));
|
||||||
sl.registerFactory(() => RemoteControlCubit(sl(),sl()));
|
sl.registerFactory(() => RemoteControlCubit(sl(),sl(),sl()));
|
||||||
/* // 工厂模式(留存,每次获取新实例)
|
/* // 工厂模式(留存,每次获取新实例)r
|
||||||
|
|
||||||
sl.registerFactory(() => DeviceStatusBloc(sl<NetMessageDispatcher>()));
|
sl.registerFactory(() => DeviceStatusBloc(sl<NetMessageDispatcher>()));
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -41,13 +41,22 @@ class RemoteHttpDatasource {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
data: {'deviceId': deviceId, 'platform': "app"});
|
data: {'deviceId': deviceId, 'platform': "app"});
|
||||||
|
print("请求权限的数据,$deviceId,");
|
||||||
try {
|
try {
|
||||||
final data = response.data as Map<String, dynamic>;
|
final responseData = response.data as Map<String, dynamic>;
|
||||||
final success = data['success'] as bool? ?? false;
|
debugPrint("📊 [HTTP 响应] 完整数据:$responseData");
|
||||||
if (success) {
|
|
||||||
return true;
|
// 🔥 关键:先获取响应的 data 字段,再获取 remoteControl
|
||||||
|
final dataField = responseData['data'] as Map<String, dynamic>?;
|
||||||
|
if (dataField != null) {
|
||||||
|
final bool hasRemoteControl = dataField['remoteControl'] as bool? ?? false;
|
||||||
|
|
||||||
|
debugPrint("✅ [HTTP 响应] remoteControl=$hasRemoteControl");
|
||||||
|
|
||||||
|
// 🔥 直接返回 remoteControl 的布尔值
|
||||||
|
return hasRemoteControl;
|
||||||
} else {
|
} else {
|
||||||
|
debugPrint("❌ [HTTP 响应] 缺少 data 字段");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:maibu_satabot_v2/features/remote_control/data/models/running_status_model.dart';
|
import 'package:maibu_satabot_v2/features/remote_control/data/models/running_status_model.dart';
|
||||||
import 'package:maibu_satabot_v2/features/remote_control/presentation/bloc/remote_control_state.dart';
|
import 'package:maibu_satabot_v2/features/remote_control/presentation/bloc/remote_control_state.dart';
|
||||||
|
|
||||||
|
import '../../../../core/network/net_message_dispatcher.dart';
|
||||||
import '../../domain/entities/machine_control_status_entity.dart';
|
import '../../domain/entities/machine_control_status_entity.dart';
|
||||||
import '../../domain/repositories/remote_control_repository.dart';
|
import '../../domain/repositories/remote_control_repository.dart';
|
||||||
import '../../domain/usecase/remote_control_usecase.dart';
|
import '../../domain/usecase/remote_control_usecase.dart';
|
||||||
@@ -12,8 +15,10 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
|||||||
final RemoteControlRepository _repository;
|
final RemoteControlRepository _repository;
|
||||||
final RequestControlPermissionUseCase _requestControlPermissionUseCase;
|
final RequestControlPermissionUseCase _requestControlPermissionUseCase;
|
||||||
Timer? _timer;
|
Timer? _timer;
|
||||||
|
StreamSubscription? _kickOutSub; // 新增:用于管理监听生命周期
|
||||||
|
final NetMessageDispatcher dispatcher;
|
||||||
|
|
||||||
RemoteControlCubit(this._repository, this._requestControlPermissionUseCase)
|
RemoteControlCubit(this._repository, this._requestControlPermissionUseCase, this.dispatcher)
|
||||||
: super(
|
: super(
|
||||||
RemoteControlState(
|
RemoteControlState(
|
||||||
controlEntity: MachineControlStatusEntity(),
|
controlEntity: MachineControlStatusEntity(),
|
||||||
@@ -24,13 +29,50 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 1. 初始化回包监听 (如 0x12 权限)
|
// 1. 初始化回包监听 (如 0x12 权限)
|
||||||
|
// void _initPacketListener() {
|
||||||
|
// _repository.responseStream.listen((packet) {
|
||||||
|
// if (packet.command == 0x12) {
|
||||||
|
// // 根据负载判断是否有权限,更新状态
|
||||||
|
// emit(state.copyWith(hasPermission: true));
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
void _initPacketListener() {
|
void _initPacketListener() {
|
||||||
_repository.responseStream.listen((packet) {
|
print('>>> [RemoteControl] begin 初始化 0x12 监听器');
|
||||||
if (packet.command == 0x12) {
|
_kickOutSub?.cancel(); // 防止重复监听
|
||||||
// 根据负载判断是否有权限,更新状态
|
|
||||||
emit(state.copyWith(hasPermission: true));
|
_kickOutSub = dispatcher.onCommand(0x12).listen((packet) {
|
||||||
|
print('>>> [RemoteControl] 收到 0x12 原始包,payload 长度=${packet.payload.length}');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 🔥 关键:手动去掉最后 2 个 CRC 字节
|
||||||
|
String jsonString;
|
||||||
|
if (packet.payload.length > 2) {
|
||||||
|
jsonString = utf8.decode(packet.payload.sublist(0, packet.payload.length - 2));
|
||||||
|
} else {
|
||||||
|
jsonString = utf8.decode(packet.payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
print('>>> [RemoteControl] 去除 CRC 后的 JSON: $jsonString');
|
||||||
|
|
||||||
|
final jsonMap = jsonDecode(jsonString);
|
||||||
|
final respond = jsonMap['request'] ?? '';
|
||||||
|
print('>>> [RemoteControl] ✅ 响应结果:$respond');
|
||||||
|
// 🔥 当 Web 端推送 switch_control 权限时,更新状态触发弹窗
|
||||||
|
if (respond == 'switch_control') {
|
||||||
|
print('>>> [RemoteControl] ✅ 获得控制权 (respond=switch_control),准备打开弹窗');
|
||||||
|
emit(state.copyWith(hasPermission: true, showPermissionRequestDialog: true));
|
||||||
|
} else if (respond == 'have_logged_in') {
|
||||||
|
// 如果是异地登录,也显示弹窗提示
|
||||||
|
print('>>> [RemoteControl] ⚠️ 检测到异地登录');
|
||||||
|
emit(state.copyWith(showPermissionRequestDialog: true));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
print('>>> [RemoteControl] ✅ 0x12 监听器已建立完成');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 开启 100ms 控制循环 (在进入遥控页面或点击“开始”时调用)
|
// 2. 开启 100ms 控制循环 (在进入遥控页面或点击“开始”时调用)
|
||||||
@@ -134,8 +176,15 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
|||||||
|
|
||||||
},
|
},
|
||||||
(success) {
|
(success) {
|
||||||
emit(state.copyWith(hasPermission: true));
|
// 更新状态
|
||||||
print('✅ c:$success');
|
print('✅ [RemoteControl] 请求控制权限成功:$success');
|
||||||
|
///处理result
|
||||||
|
if(success){
|
||||||
|
emit(state.copyWith(hasPermission: true));
|
||||||
|
}else{
|
||||||
|
emit(state.copyWith(hasPermission: false));
|
||||||
|
}
|
||||||
|
print('✅ c:$success');
|
||||||
// 权限申请已发送,等待 0x12 回包更新状态
|
// 权限申请已发送,等待 0x12 回包更新状态
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -143,4 +192,6 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user