优化掉核心功能中print打印影响性能的代码用日志对象代替打印
This commit is contained in:
@@ -2,12 +2,15 @@ import 'dart:ffi';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/storage/user_storage.dart';
|
||||
|
||||
class RemoteHttpDatasource {
|
||||
final Dio _dio;
|
||||
final UserStorage _userStorage; // 🔥 新增字段
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
|
||||
RemoteHttpDatasource(this._dio, this._userStorage);
|
||||
@@ -44,23 +47,26 @@ class RemoteHttpDatasource {
|
||||
print("请求权限的数据,$deviceId,");
|
||||
try {
|
||||
final responseData = response.data as Map<String, dynamic>;
|
||||
debugPrint("📊 [HTTP 响应] 完整数据:$responseData");
|
||||
//debugPrint("📊 [HTTP 响应] 完整数据:$responseData");
|
||||
_logger.log("📊 [HTTP 响应] 完整数据:$responseData");
|
||||
|
||||
// 🔥 关键:先获取响应的 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");
|
||||
|
||||
//debugPrint("✅ [HTTP 响应] remoteControl=$hasRemoteControl");
|
||||
_logger.log("✅ [HTTP 响应] remoteControl=$hasRemoteControl");
|
||||
// 🔥 直接返回 remoteControl 的布尔值
|
||||
return hasRemoteControl;
|
||||
} else {
|
||||
debugPrint("❌ [HTTP 响应] 缺少 data 字段");
|
||||
//debugPrint("❌ [HTTP 响应] 缺少 data 字段");
|
||||
_logger.log("❌ [HTTP 响应] 缺少 data 字段");
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('❌ [RemoteHttp] 解析响应失败:$e');
|
||||
// debugPrint('❌ [RemoteHttp] 解析响应失败:$e');
|
||||
_logger.log('❌ [RemoteHttp] 解析响应失败:$e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/protocol_decoder.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../../../core/protocol/machine_protocol_codec.dart';
|
||||
@@ -19,6 +21,7 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
final DiffSteerUseCase _diffSteer;
|
||||
final RemoteHttpDatasource _remoteHttp;
|
||||
final RemoteTcpDatasource _remoteTcp;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
|
||||
RemoteControlRepositoryImpl(this._tcpClient, this._diffSteer, this._remoteHttp, this._remoteTcp);
|
||||
@@ -34,9 +37,13 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
final speeds = _diffSteer.calculate(status.originX, status.originY);
|
||||
|
||||
|
||||
debugPrint('🎮 [摇杆数据] originX: ${status.originX}, originY: ${status.originY}');
|
||||
debugPrint('⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}');
|
||||
debugPrint('🚨 [急停状态] emergency: ${status.isEmergency ? 1 : 0}');
|
||||
//debugPrint('🎮 [摇杆数据] originX: ${status.originX}, originY: ${status.originY}');
|
||||
_logger.logWithLevel('⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}');
|
||||
|
||||
//debugPrint('⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}');
|
||||
_logger.logWithLevel('⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}');
|
||||
//debugPrint('🚨 [急停状态] emergency: ${status.isEmergency ? 1 : 0}');
|
||||
_logger.logWithLevel('🚨 [急停状态] emergency: ${status.isEmergency ? 1 : 0}');
|
||||
|
||||
// 2. 调用 Codec:仅生成协议要求的 8 字节 Payload 负载数据
|
||||
final payload = MachineProtocolCodec.encodeRemoteControlPayload(
|
||||
@@ -65,10 +72,12 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
Future<bool> requestControlPermission( String deviceName, String deviceId) async {
|
||||
try {
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
debugPrint('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
// debugPrint('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
_logger.logWithLevel('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
return await _remoteHttp.requestRemoteControlViaHttp(deviceName, deviceId);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [RemoteControl] 权限请求失败:$e');
|
||||
//sdebugPrint('❌ [RemoteControl] 权限请求失败:$e');
|
||||
_logger.logWithLevel('❌ [RemoteControl] 权限请求失败:$e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,11 @@ import 'package:dart_ping/dart_ping.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.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 '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/network/net_message_dispatcher.dart';
|
||||
import '../../../devices/domain/entities/running_status_entity.dart';
|
||||
import '../../domain/entities/machine_control_status_entity.dart';
|
||||
@@ -25,6 +27,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
StreamSubscription? _stringMessageSub; //
|
||||
static const platform = MethodChannel('com.maibu.satabot/ping');
|
||||
int _currentPing = 50;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
|
||||
|
||||
RemoteControlCubit(this._repository, this._requestControlPermissionUseCase, this.dispatcher)
|
||||
@@ -39,26 +43,32 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
}
|
||||
|
||||
void _initStringMessageListener() {
|
||||
print('>>> [RemoteControl] begin 初始化 0x02 字符串监听器');
|
||||
//print('>>> [RemoteControl] begin 初始化 0x02 字符串监听器');
|
||||
_logger.logWithLevel('>>> [RemoteControl] begin 初始化 0x02 字符串监听器');
|
||||
_stringMessageSub?.cancel();
|
||||
|
||||
_stringMessageSub = dispatcher.onStringMessage().listen((message) async {
|
||||
print('>>> [RemoteControl] 收到 0x02 字符串推送:$message');
|
||||
// print('>>> [RemoteControl] 收到 0x02 字符串推送:$message');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 收到 0x02 字符串推送:$message');
|
||||
|
||||
if (message.isEmpty) {
|
||||
print('⚠️ [RemoteControl] 消息为空,跳过解析');
|
||||
//print('⚠️ [RemoteControl] 消息为空,跳过解析');
|
||||
_logger.logWithLevel('⚠️ [RemoteControl] 消息为为空,跳过解析');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 🔥 关键:使用与 DeviceStatusBloc 相同的解析方法
|
||||
print('>>> [RemoteControl] 🔍 开始解析数据(使用 fromFields)...');
|
||||
// print('>>> [RemoteControl] 🔍 开始解析数据(使用 fromFields)...');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 🔍 开始解析数据(使用 fromFields)...');
|
||||
|
||||
final fields = message.trim().split(',');
|
||||
print('>>> [RemoteControl] 字段数量:${fields.length}');
|
||||
//print('>>> [RemoteControl] 字段数量:${fields.length}');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 字段数量:${fields.length}');
|
||||
|
||||
if (fields.length < 18) {
|
||||
print('⚠️ [RemoteControl] 字段不足:${fields.length},期望 ≥18');
|
||||
//print('⚠️ [RemoteControl] 字段不足:${fields.length},期望 ≥18');
|
||||
_logger.logWithLevel('⚠️ [RemoteControl] 字段不足:${fields.length},期望 ≥18');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -83,13 +93,15 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
|
||||
|
||||
} catch (e, stackTrace) {
|
||||
print('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
// print('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
// print('>>> [RemoteControl] ❌ 堆栈跟踪:$stackTrace');
|
||||
// print('>>> [RemoteControl] ❌ 原始消息:$message');
|
||||
}
|
||||
});
|
||||
|
||||
print('>>> [RemoteControl] ✅ 0x02 字符串监听器已建立完成');
|
||||
//print('>>> [RemoteControl] ✅ 0x02 字符串监听器已建立完成');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ✅ 0x02 字符串监听器已建立完成');
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +121,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
battery: int.tryParse(newStatus.battery) ?? 0,
|
||||
));
|
||||
|
||||
debugPrint('✅ [更新运行状态] 电压:${newStatus.voltage}V, 电量:${newStatus.battery}%');
|
||||
// debugPrint('✅ [更新运行状态] 电压:${newStatus.voltage}V, 电量:${newStatus.battery}%');
|
||||
_logger.logWithLevel('✅ [更新运行状态] 电压:${newStatus.voltage}V, 电量:${newStatus.battery}%');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,11 +137,13 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
// });
|
||||
// }
|
||||
Future<void> _initPacketListener() async {
|
||||
print('>>> [RemoteControl] begin 初始化 0x12 监听器');
|
||||
//print('>>> [RemoteControl] begin 初始化 0x12 监听器');
|
||||
_logger.logWithLevel('>>> [RemoteControl] begin 0x12 监听器');
|
||||
_kickOutSub?.cancel(); // 防止重复监听
|
||||
|
||||
_kickOutSub = dispatcher.onCommand(0x12).listen((packet) {
|
||||
print('>>> [RemoteControl] 收到 0x12 原始包,payload 长度=${packet.payload.length}');
|
||||
//print('>>> [RemoteControl] 收到 0x12 原始包,payload 长度=${packet.payload.length}');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 收到 0x12 原始包,payload 长度=${packet.payload.length}');
|
||||
|
||||
try {
|
||||
// 🔥 关键:手动去掉最后 2 个 CRC 字节
|
||||
@@ -139,7 +154,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
jsonString = utf8.decode(packet.payload);
|
||||
}
|
||||
|
||||
print('>>> [RemoteControl] 去除 CRC 后的 JSON: $jsonString');
|
||||
//print('>>> [RemoteControl] 去除 CRC 后的 JSON: $jsonString');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 去除 CRC 后的 JSON: $jsonString');
|
||||
|
||||
final jsonMap = jsonDecode(jsonString);
|
||||
|
||||
@@ -148,12 +164,14 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
final platform = jsonMap['platform'];
|
||||
final respondData = jsonMap['respond'];
|
||||
|
||||
print('>>> [RemoteControl] 📋 requestType=$requestType, platform=$platform');
|
||||
//print('>>> [RemoteControl] 📋 requestType=$requestType, platform=$platform');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 📋 requestType=$requestType, platform=$platform');
|
||||
|
||||
// 情况 1: 响应格式 - {"respond":{"switchResult":true,"deviceId":"...","holder":"you"}}
|
||||
if (respondData != null && respondData is Map) {
|
||||
final switchResult = respondData['switchResult'];
|
||||
print('>>> [RemoteControl] 📊 收到切换结果响应:switchResult = $switchResult');
|
||||
//print('>>> [RemoteControl] 📊 收到切换结果响应:switchResult = $switchResult');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 📊 收到切换结果响应:switchResult = $switchResult');
|
||||
|
||||
if (!isClosed) {
|
||||
if (switchResult == true) {
|
||||
@@ -171,33 +189,39 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
else if (requestType == 'switch_control') {
|
||||
// 🔥 关键判断:只有当是其他平台(web)请求时才弹窗
|
||||
if (platform != null && platform.toString().toLowerCase() != 'app') {
|
||||
print('>>> [RemoteControl] 🚨 $platform 端请求控制权,打开弹窗询问用户');
|
||||
//print('>>> [RemoteControl] 🚨 $platform 端请求控制权,打开弹窗询问用户');
|
||||
_logger.logWithLevel('>>> [RemoteControl] 🚨 $platform 端请求控制权,打开弹窗询问用户');
|
||||
if (!isClosed) {
|
||||
emit(state.copyWith(showPermissionRequestDialog: true));
|
||||
}
|
||||
} else {
|
||||
print('>>> [RemoteControl] ℹ️ APP 自己的请求回显,忽略不弹窗');
|
||||
//print('>>> [RemoteControl] ℹ️ APP 自己的请求回显,忽略不弹窗');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ℹ️ APP 自己的请求回显,忽略不弹窗');
|
||||
}
|
||||
}
|
||||
// 情况 3: 异地登录通知 - {"request":"have_logged_in",...}
|
||||
else if (requestType == 'have_logged_in') {
|
||||
print('>>> [RemoteControl] ⚠️ 检测到异地登录,打开弹窗提示');
|
||||
//print('>>> [RemoteControl] ⚠️ 检测到异地登录,打开弹窗提示');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ⚠️ 检测到异地登录,打开弹窗提示');
|
||||
if (!isClosed) {
|
||||
emit(state.copyWith(showPermissionRequestDialog: true));
|
||||
}
|
||||
}
|
||||
else {
|
||||
print('>>> [RemoteControl] ℹ️ 未知类型的 0x12 包,忽略');
|
||||
// print('>>> [RemoteControl] ℹ️ 未知类型的 0x12 包,忽略');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ℹ️ 未知类型的 0x12 包,忽略');
|
||||
}
|
||||
} catch (e) {
|
||||
print('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
//print('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ❌ 解析失败:$e');
|
||||
}
|
||||
});
|
||||
final c = getNetworkDelay();
|
||||
emit(state.copyWith(
|
||||
ping: await c, // 这里直接使用异步返回的数值
|
||||
));
|
||||
print('>>> [RemoteControl] ✅ 0x12 监听器已建立完成');
|
||||
//print('>>> [RemoteControl] ✅ 0x12 监听器已建立完成');
|
||||
_logger.logWithLevel('>>> [RemoteControl] ✅ 0x12 监听器已建立完成');
|
||||
}
|
||||
|
||||
|
||||
@@ -222,7 +246,8 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
|
||||
// 4. 更新功能开关 (比如割刀速度、灯光、点火等)
|
||||
void updateFunction({int? mower, int? lift, int? ignition, bool? emergency}) {
|
||||
debugPrint('🔧 [updateFunction] 调用 - mower: $mower, lift: $lift, ignition: $ignition, emergency: $emergency');
|
||||
// debugPrint('🔧 [updateFunction] 调用 - mower: $mower, lift: $lift, ignition: $ignition, emergency: $emergency');
|
||||
_logger.logWithLevel('🔧 [updateFunction] 调用 - mower: $mower, lift: $lift, ignition: $ignition, emergency: $emergency');
|
||||
final updatedEntity = state.controlEntity.copyWith(
|
||||
mower: mower,
|
||||
lift: lift,
|
||||
@@ -235,6 +260,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
isEmergency: emergency ?? state.isEmergency,
|
||||
));
|
||||
debugPrint('✅ [updateFunction] 状态已更新并 emit');
|
||||
_logger.logWithLevel('✅ [updateFunction] 状态已更新并 emit');
|
||||
}
|
||||
|
||||
void updateOriginY(int y) {
|
||||
@@ -322,14 +348,16 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
// 3. 处理结果
|
||||
result.fold(
|
||||
(failure) {
|
||||
print('❌ [RemoteControl] 请求控制权限失败:${failure.message}');
|
||||
//print('❌ [RemoteControl] 请求控制权限失败:${failure.message}');
|
||||
_logger.logWithLevel('❌ [RemoteControl] 请求控制权限失败:${failure.message}');
|
||||
// 可以在这里显示错误提示或重新打开弹窗
|
||||
emit(state.copyWith(showPermissionRequestDialog: true));
|
||||
|
||||
},
|
||||
(success) {
|
||||
// 更新状态
|
||||
print('✅ [RemoteControl] 请求控制权限成功:$success');
|
||||
//print('✅ [RemoteControl] 请求控制权限成功:$success');
|
||||
_logger.logWithLevel('✅ [RemoteControl] 请求控制权限成功:$success');
|
||||
///处理result
|
||||
if(success){
|
||||
emit(state.copyWith(hasPermission: true));
|
||||
@@ -343,12 +371,14 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
}
|
||||
/// 发送底盘指令
|
||||
void sendChassisCommand(int i) {
|
||||
debugPrint(' [底盘指令] ${i}');
|
||||
// debugPrint(' [底盘指令] ${i}');
|
||||
_logger.logWithLevel(' [底盘指令] ${i}');
|
||||
updateFunction(lift: i);
|
||||
}
|
||||
/// 发送割刀指令
|
||||
void sendMowerCommand(int i) {
|
||||
debugPrint(' [割刀指令] ${i}');
|
||||
// debugPrint(' [割刀指令] ${i}');
|
||||
_logger.logWithLevel(' [割刀指令] ${i}');
|
||||
updateFunction(mower: i);
|
||||
}
|
||||
/// 发送点火指令
|
||||
@@ -356,6 +386,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
|
||||
//void updateFunction({int? mower, int? lift, int? ignition, bool? emergency})
|
||||
// updateFunction(mower:0, lift: 0, ignition: i, emergency: false);
|
||||
debugPrint(' [点火指令] ${i}');
|
||||
_logger.logWithLevel(' [点火指令] ${i}');
|
||||
updateFunction(ignition: i);
|
||||
}
|
||||
// 发送障碍物识别指令
|
||||
|
||||
Reference in New Issue
Block a user