1:修复远程遥控控制状态显示不正确的问题

2:更换端口映射为测试服的8081-59003,9001-59004
This commit is contained in:
2026-08-26 14:23:20 +08:00
parent ca616faf11
commit 7c01229b9e
22 changed files with 399 additions and 119 deletions

View File

@@ -742,15 +742,20 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
debugPrint('${_getTimePrefix()} ====权限弹窗响应结束=====');
}
/// 请求控制权限(HTTP 查询接口 /forward/device/remoteControl)
///
/// [grabControl] true = 查询后若无权限立即发 TCP 0x12 switch_control 抢占(手动点击时使用);
/// false = 仅查询,把接口 remoteControl 结果如实同步到 UI(进入页面时使用)
Future<void> requestControlPermissionS(
String deviceName,
String deviceId, {
String source = '自动',
bool grabControl = false,
}) async {
final timePrefix = _getTimePrefix();
final logPrefix = '$timePrefix 🔑 [RemoteControl] [请求权限接口-$source]';
debugPrint('$logPrefix =========================================');
debugPrint('$logPrefix 开始请求控制权');
debugPrint('$logPrefix 开始请求控制权 (grabControl=$grabControl)');
debugPrint('$logPrefix deviceName: $deviceName');
debugPrint('$logPrefix platform: $deviceId');
debugPrint(
@@ -808,8 +813,21 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
shouldLog: true,
);
// 🔥 关键逻辑: 如果没有权限 或owner为null,则发送TCP 请求
if (!hasPermission || owner == null) {
// 🔥 仅查询模式:不抢占,把接口 remoteControl 结果如实同步到 UI
if (!grabControl) {
debugPrint(
'$successLogPrefix 👀 仅查询模式,不发送TCP抢占,直接同步 remoteControl=$hasPermission',
);
_logger.logWithLevel(
'$successLogPrefix 👀 仅查询模式,直接同步 remoteControl=$hasPermission',
shouldLog: true,
);
if (state.hasPermission != hasPermission) {
emit(state.copyWith(hasPermission: hasPermission));
}
}
// 🔥 抢占模式: 如果没有权限 或owner为null,则发送TCP 请求
else if (!hasPermission || owner == null) {
debugPrint('$successLogPrefix ⚠️ APP无权限或owner为null,发送TCP请求...');
_logger.logWithLevel(
'$successLogPrefix ⚠️ APP无权限或owner为null,发送TCP请求',
@@ -850,6 +868,23 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
);
}
/// 🔥 退出遥控页时重置权限状态,避免单例状态残留导致下次进入不再查询接口
void resetPermissionState() {
if (!isClosed) {
emit(
state.copyWith(
hasPermission: false,
showPermissionRequestDialog: false,
),
);
debugPrint('🔑 [RemoteControl] 已重置权限状态 hasPermission=false');
_logger.logWithLevel(
'🔑 [RemoteControl] 已重置权限状态 hasPermission=false',
shouldLog: true,
);
}
}
/// 🔥 重置弹窗状态 - 在弹窗关闭后调用(已简化,不再需要标志位)
void resetPermissionCoolDown() {
// 标志位已移除,此方法保留以保持向后兼容性
@@ -972,9 +1007,12 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
}
}
//app退出远程遥控界面释放权限
Future<bool> releasePermission(String platform) async {
return await _repository.releasePermission(platform);
//app退出远程遥控界面释放权限(type=2),退出机器人详情页释放权限(type=1)
Future<bool> releasePermission({
required String deviceId,
required int type,
}) async {
return await _repository.releasePermission(deviceId: deviceId, type: type);
}
/// 🔥 设置待控制的设备(从机器人列表点击进入时调用)

View File

@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
@@ -129,18 +130,23 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
// 🔥 离开安全模式:启动 90 秒倒计时
GetIt.I<AuthCubit>().exitSafeMode();
// 释放远程控制权限
_cubit.releasePermission("app");
//print("远程控制要推出啦");
//final deviceState = _devicesCubit?.state;
//if (deviceState?.selectedDevice != null) {
// _cubit.releasePermission("app");
// }
// 释放远程控制权限(type=2:退出远程遥控页面释放)
final deviceId = _cubit.state.targetDevice?.deviceName;
if (deviceId != null && deviceId.isNotEmpty) {
_cubit.releasePermission(deviceId: deviceId, type: 2).then((success) {
CapsuleToast.show(
success ? '已释放远程控制权限' : '控制权限释放失败',
showCheck: success,
);
});
}
_permissionSubscription?.cancel(); // 🔥 取消订阅
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
_cubit.stopControlLoop();
// 🔥 重置权限状态,避免单例 hasPermission 残留导致下次进入不再查询接口
_cubit.resetPermissionState();
super.dispose();
}
@@ -149,7 +155,6 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
// 🔥 只监听 RemoteControlCubit,避免其他状态变化导致频繁 rebuild
final remoteCubit = context.read<RemoteControlCubit>();
final targetDevice = remoteCubit.state.targetDevice;
final hasPermission = remoteCubit.state.hasPermission;
// 🔥 调试日志:检查设备状态
debugPrint('🔍 [RemoteControlPage] build - targetDevice: ${targetDevice?.deviceName}');
@@ -159,26 +164,22 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
return _buildOfflineScaffold();
}
// 🔥 只在没有权限时自动请求,避免重复调用
if (!hasPermission && _isLoadingPermission) {
// 🔥 只使用 targetDevice
final deviceName = remoteCubit.state.targetDevice?.deviceName;
if (deviceName != null && deviceName.isNotEmpty) {
// debugPrint('🔑 [RemoteControl] 检测到无权限,自动发送权限请求 - deviceName: $deviceName');
setState(() => _isLoadingPermission = false); // 🔥 标记为已请求
remoteCubit.requestControlPermissionS(deviceName, "app").then((_) {
if (mounted) {
setState(() => _isLoadingPermission = false); // 🔥 请求完成后重置
}
});
} else {
// debugPrint('❌ [RemoteControl] 无法发送权限请求 - targetDevice 为空');
// debugPrint(' targetDevice: ${remoteCubit.state.targetDevice}');
setState(() => _isLoadingPermission = false);
}
} else {
// debugPrint('✅ [RemoteControl] 已有权限或已请求过,跳过');
// 🔥 进入页面只查询一次真实权限状态(不抢占),按接口 remoteControl 如实展示
if (_isLoadingPermission) {
// 直接置标志位,不在 build 期间调用 setState
_isLoadingPermission = false;
final deviceName = targetDevice.deviceName;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted && deviceName.isNotEmpty) {
debugPrint('🔑 [RemoteControlPage] 进入页面,仅查询权限状态 - deviceName: $deviceName');
remoteCubit.requestControlPermissionS(
deviceName,
'app',
source: '进入页面查询',
grabControl: false,
);
}
});
}
return Scaffold(

View File

@@ -97,8 +97,8 @@ class TopStatusBar extends StatelessWidget {
breathing: !remoteState.hasPermission,
onTap: () {
if (!remoteState.hasPermission) {
// 🔥 点击后重新请求权限,和刚进入页面时的逻辑一致
debugPrint('🔑 [TopStatusBar] 👆 用户手动点击“未在控制”,重新请求权限');
// 🔥 点击后发起抢占:查权限 + 无权限则发 TCP switch_control
debugPrint('🔑 [TopStatusBar] 👆 用户手动点击“未在控制”,请求抢占控制权');
final targetDevice =
_remoteControlCubit.state.targetDevice;
if (targetDevice != null) {
@@ -106,6 +106,7 @@ class TopStatusBar extends StatelessWidget {
targetDevice.deviceName,
'app',
source: '手动点击',
grabControl: true,
);
} else {
debugPrint('❌ [TopStatusBar] targetDevice 为空,无法请求权限');