修复emit异步导致的竞态+tcp缓冲取挤压+网络堵塞+定时器恢复的造成归0被吞的情况
完成APP端释放权限的接口的集成和数据返回获取
This commit is contained in:
@@ -28,6 +28,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
static const platform = MethodChannel('com.maibu.satabot/ping');
|
||||
int _currentPing = 50;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
DateTime? _lastStopTime; // 🔥 记录最后一次停止时间
|
||||
Timer? _clearStopTimeTimer; // 🔥 用于管理 _lastStopTime 的清除
|
||||
|
||||
|
||||
|
||||
@@ -226,9 +228,12 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
|
||||
|
||||
// 2. 开启 100ms 控制循环 (在进入遥控页面或点击“开始”时调用)
|
||||
void startControlLoop() {
|
||||
/* void startControlLoop() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
|
||||
// 🔥 关键修复:每次循环都重新读取最新的 state
|
||||
final currentEntity = state.controlEntity;
|
||||
|
||||
// 🔥 安全检查:无权限或急停时不发送
|
||||
if (!state.hasPermission || state.isEmergency) {
|
||||
debugPrint('⚠️ [定时器] 跳过发送 - hasPermission=${state.hasPermission}, isEmergency=${state.isEmergency}');
|
||||
@@ -236,11 +241,53 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
}
|
||||
|
||||
|
||||
debugPrint('⏰ [定时器] 发送控制指令 - originX=${state.controlEntity.originX}, originY=${state.controlEntity.originY}');
|
||||
_logger.logWithLevel('真实的发送的实体 - originX: ${state.controlEntity.originX}, originY: ${state.controlEntity.originY}');
|
||||
_repository.sendControlMachineCmd(state.controlEntity);
|
||||
debugPrint('⏰ [定时器] 发送控制指令 - originX=${currentEntity.originX}, originY=${currentEntity.originY}');
|
||||
_logger.logWithLevel('真实的发送的实体 - originX: ${currentEntity.originX}, originY: ${currentEntity.originY}');
|
||||
_repository.sendControlMachineCmd(currentEntity);
|
||||
});
|
||||
emit(state.copyWith(status: RemoteControlStatus.controlling));
|
||||
}*/
|
||||
void startControlLoop() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
|
||||
if (isClosed) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
// ✅ 必须通过方法拿最新 state,不能直接读!
|
||||
final entity = _getCurrentControlEntity();
|
||||
|
||||
// 🔥 关键日志:检查权限状态
|
||||
if (!state.hasPermission) {
|
||||
debugPrint('⚠️ [定时器] 无权限,跳过发送 - hasPermission=${state.hasPermission}, isEmergency=${state.isEmergency}');
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.isEmergency) {
|
||||
debugPrint('🚨 [定时器] 急停状态,跳过发送');
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 新增:如果刚停止5000ms内,暂停所有发送(包括零值)- 与_lastStopTime清除时间一致
|
||||
if (_lastStopTime != null) {
|
||||
final elapsed = DateTime.now().difference(_lastStopTime!).inMilliseconds;
|
||||
if (elapsed < 5000) {
|
||||
debugPrint('⚠️ [定时器] 停止保护期内,暂停所有发送 - 已过${elapsed}ms');
|
||||
return; // 保护期内不发送任何指令,让_sendStopCommandRepeatedly独占通道
|
||||
}
|
||||
// 🔥 关键修复:不在这里清空 _lastStopTime,让它继续生效以拦截 updateOriginX/Y
|
||||
}
|
||||
|
||||
_logger.logWithLevel('[定时器] 发送控制指令 - originX=${entity.originX}, originY=${entity.originY}');
|
||||
debugPrint('📤 [定时器] 准备发送 - originX=${entity.originX}, originY=${entity.originY}, hasPermission=${state.hasPermission}, _lastStopTime=$_lastStopTime');
|
||||
_repository.sendControlMachineCmd(entity);
|
||||
});
|
||||
}
|
||||
//通过方法拿最新 state
|
||||
MachineControlStatusEntity _getCurrentControlEntity() {
|
||||
_logger.logWithLevel('真实的发送的实体 - originX: ${state.controlEntity.originX}, originY: ${state.controlEntity.originY}');
|
||||
return state.controlEntity;
|
||||
}
|
||||
|
||||
// 3. 更新摇杆数据
|
||||
@@ -272,36 +319,109 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
}
|
||||
|
||||
void updateOriginY(int y) {
|
||||
debugPrint('📥 [updateOriginY] 收到参数 y=$y');
|
||||
final updatedEntity = state.controlEntity.copyWith(
|
||||
y: y,
|
||||
);
|
||||
debugPrint('📤 [updateOriginY] 创建 updatedEntity.y=${updatedEntity.originY}');
|
||||
emit(state.copyWith(controlEntity: updatedEntity));
|
||||
debugPrint('✅ [updateOriginY] emit 完成,当前 state.controlEntity.originY=${state.controlEntity.originY}');
|
||||
|
||||
// 🔥 关键修复:如果归零,立即发送停止指令,使用更新后的实体
|
||||
if (y == 0) {
|
||||
_logger.logWithLevel('🛑 [updateOriginY] 检测到归零,立即发送停止指令');
|
||||
debugPrint('🛑 [updateOriginY] 发送 updatedEntity: originX=${updatedEntity.originX}, originY=${updatedEntity.originY}');
|
||||
_repository.sendControlMachineCmd(updatedEntity);
|
||||
debugPrint('📥 [updateOriginY] 被调用 - y=$y, _lastStopTime=$_lastStopTime');
|
||||
// 🔥 关键修复:如果刚停止5000ms内,拒绝非零值的更新 - 与定时器保护期一致
|
||||
if (_lastStopTime != null) {
|
||||
final elapsed = DateTime.now().difference(_lastStopTime!).inMilliseconds;
|
||||
debugPrint('📥 [updateOriginY] 检查保护期 - elapsed=${elapsed}ms, y=$y');
|
||||
if (elapsed < 5000 && y != 0) {
|
||||
debugPrint('⚠️ [updateOriginY] 保护期内拒绝非零值 - y=$y, 已过${elapsed}ms');
|
||||
return; // 拒绝更新
|
||||
}
|
||||
} else {
|
||||
debugPrint('📥 [updateOriginY] _lastStopTime为null,跳过保护期检查');
|
||||
}
|
||||
|
||||
final updatedEntity = state.controlEntity.copyWith(y: y);
|
||||
emit(state.copyWith(controlEntity: updatedEntity));
|
||||
debugPrint('📝 [updateOriginY] state已更新 - originY=$y');
|
||||
}
|
||||
|
||||
void updateOriginX(int x) {
|
||||
debugPrint('📥 [updateOriginX] 收到参数 x=$x');
|
||||
final updatedEntity = state.controlEntity.copyWith(
|
||||
x: x,
|
||||
);
|
||||
debugPrint('📤 [updateOriginX] 创建 updatedEntity.x=${updatedEntity.originX}');
|
||||
emit(state.copyWith(controlEntity: updatedEntity));
|
||||
debugPrint('✅ [updateOriginX] emit 完成,当前 state.controlEntity.originX=${state.controlEntity.originX}');
|
||||
|
||||
// 🔥 关键修复:如果归零,立即发送停止指令,使用更新后的实体
|
||||
if (x == 0) {
|
||||
_logger.logWithLevel('🛑 [updateOriginX] 检测到归零,立即发送停止指令');
|
||||
debugPrint('🛑 [updateOriginX] 发送 updatedEntity: originX=${updatedEntity.originX}, originY=${updatedEntity.originY}');
|
||||
_repository.sendControlMachineCmd(updatedEntity);
|
||||
debugPrint('📥 [updateOriginX] 被调用 - x=$x, _lastStopTime=$_lastStopTime');
|
||||
// 🔥 关键修复:如果刚停止5000ms内,拒绝非零值的更新 - 与定时器保护期一致
|
||||
if (_lastStopTime != null) {
|
||||
final elapsed = DateTime.now().difference(_lastStopTime!).inMilliseconds;
|
||||
debugPrint('📥 [updateOriginX] 检查保护期 - elapsed=${elapsed}ms, x=$x');
|
||||
if (elapsed < 5000 && x != 0) {
|
||||
debugPrint('⚠️ [updateOriginX] 保护期内拒绝非零值 - x=$x, 已过${elapsed}ms');
|
||||
return; // 拒绝更新
|
||||
}
|
||||
} else {
|
||||
debugPrint('📥 [updateOriginX] _lastStopTime为null,跳过保护期检查');
|
||||
}
|
||||
|
||||
final updatedEntity = state.controlEntity.copyWith(x: x);
|
||||
emit(state.copyWith(controlEntity: updatedEntity));
|
||||
debugPrint('📝 [updateOriginX] state已更新 - originX=$x');
|
||||
}
|
||||
|
||||
/// 🔥 安全方法:同时清零双轴,确保只发送一次完全停止指令
|
||||
Future<void> stopAllMovement() async {
|
||||
debugPrint('🛑 [stopAllMovement] 开始执行 - 当前state: originX=${state.controlEntity.originX}, originY=${state.controlEntity.originY}');
|
||||
|
||||
// 🔥 关键修复1:取消旧的清除定时器,防止多个定时器竞争
|
||||
_clearStopTimeTimer?.cancel();
|
||||
|
||||
// 🔥 关键修复2:立即设置 _lastStopTime,防止在 stopAllMovement 执行期间 state 被更新
|
||||
_lastStopTime = DateTime.now();
|
||||
debugPrint('🛑 [stopAllMovement] _lastStopTime 已设置为 $_lastStopTime');
|
||||
|
||||
// 🔥 关键修复3:先暂停定时器,防止定时器在停止期间发送旧的运动指令
|
||||
_timer?.cancel();
|
||||
_timer = null; // 🔥 彻底清空,防止重复启动
|
||||
|
||||
final updatedEntity = state.controlEntity.copyWith(x: 0, y: 0);
|
||||
|
||||
// 一次性更新双轴(即使无权限也要更新本地state)
|
||||
emit(state.copyWith(controlEntity: updatedEntity));
|
||||
|
||||
// 🔥 安全底线:停止指令必须无视权限强制发送!
|
||||
debugPrint('🛑 [stopAllMovement] 双轴归零,发送停止指令 - originX=0, originY=0');
|
||||
await _sendStopCommandRepeatedly(updatedEntity); // ✅ 等待所有指令发送完成
|
||||
|
||||
// 🔥 恢复定时器(但保护期内不会发送任何指令)
|
||||
await Future.delayed(const Duration(milliseconds: 500)); // 🔥 等待500ms让下位机执行停止
|
||||
startControlLoop();
|
||||
|
||||
// 🔥 关键修复4:使用 Timer 而不是 Future.delayed,确保只有一个定时器在运行
|
||||
_clearStopTimeTimer = Timer(const Duration(seconds: 5), () {
|
||||
_lastStopTime = null;
|
||||
debugPrint('✅ [保护期] 5秒后自动清除标记');
|
||||
});
|
||||
|
||||
debugPrint('✅ [stopAllMovement] 执行完成');
|
||||
}
|
||||
|
||||
/// 🔥 统一方法:连续发送30次停止指令,彻底清空TCP缓冲区
|
||||
Future<void> _sendStopCommandRepeatedly(MachineControlStatusEntity stopEntity) async {
|
||||
debugPrint('🛑 [紧急停止] 开始连续发送30次停止指令 - originX=${stopEntity.originX}, originY=${stopEntity.originY}');
|
||||
// 🔥 注意:_lastStopTime 已经在 stopAllMovement() 中设置了,这里不需要再设置
|
||||
|
||||
int sentCount = 0;
|
||||
// 🔥 关键修复:每次发送间隔5ms,避免TCP合并/丢弃
|
||||
for (int i = 0; i < 20; i++) {
|
||||
_repository.sendControlMachineCmd(stopEntity);
|
||||
sentCount++;
|
||||
if (i % 5 == 0) {
|
||||
debugPrint('🛑 [紧急停止] 已发送第${i + 1}次');
|
||||
}
|
||||
if (i < 19) {
|
||||
await Future.delayed(const Duration(milliseconds: 5));
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 延迟后再发送10次(双重保险,对抗网络抖动)
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
_repository.sendControlMachineCmd(stopEntity);
|
||||
sentCount++;
|
||||
if (i < 9) {
|
||||
await Future.delayed(const Duration(milliseconds: 5));
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('✅ [紧急停止] 所有30次停止指令已发出');
|
||||
}
|
||||
|
||||
// 5. 停止控制循环
|
||||
@@ -330,6 +450,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
@override
|
||||
Future<void> close() {
|
||||
_timer?.cancel(); // 退出页面时务必销毁定时器
|
||||
_clearStopTimeTimer?.cancel(); // 🔥 清除保护期定时器
|
||||
_kickOutSub?.cancel();
|
||||
_stringMessageSub?.cancel();
|
||||
return super.close();
|
||||
|
||||
@@ -50,11 +50,13 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
},
|
||||
onPress: () {
|
||||
_isTouching = true;
|
||||
debugPrint('onPress');
|
||||
_triggerVibration();
|
||||
},
|
||||
onPanEnd: () {
|
||||
onPanEnd: () async {
|
||||
// 🔥 松手 100% 归零
|
||||
_stopJoystick();
|
||||
debugPrint('onPanEnd');
|
||||
await _stopJoystick();
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -73,11 +75,13 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
}
|
||||
|
||||
// 统一停车方法(万能保险)
|
||||
void _stopJoystick() {
|
||||
Future<void> _stopJoystick() async {
|
||||
_isTouching = false;
|
||||
_lastSentY = 0;
|
||||
debugPrint('✅ 摇杆已停止 -> 强制 Y=0 停车');
|
||||
context.read<RemoteControlCubit>().updateOriginY(0);
|
||||
debugPrint('✅ 摇杆已停止 -> 强制 X=0, Y=0 停车');
|
||||
|
||||
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
|
||||
await context.read<RemoteControlCubit>().stopAllMovement();
|
||||
}
|
||||
|
||||
void _triggerVibration() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:cc_ui_kit/cc_ui_kit.dart';
|
||||
import 'package:cc_ui_kit/cc_ui_kit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:vibration/vibration.dart';
|
||||
@@ -46,10 +46,12 @@ class _RightJoystickAreaState extends State<RightJoystickArea> {
|
||||
context.read<RemoteControlCubit>().updateOriginX(finalX);
|
||||
},
|
||||
onPress: _triggerVibration,
|
||||
onPanEnd: () {
|
||||
debugPrint('🕹️ [右摇杆] 松手,强制X=0');
|
||||
onPanEnd: () async {
|
||||
debugPrint('🕹️ [右摇杆] 松手,强制 X=0, Y=0');
|
||||
_lastX = 0;
|
||||
context.read<RemoteControlCubit>().updateOriginX(0);
|
||||
|
||||
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
|
||||
await context.read<RemoteControlCubit>().stopAllMovement();
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user