优化掉核心功能中print打印影响性能的代码用日志对象代替打印

This commit is contained in:
2026-04-15 08:43:39 +08:00
parent 507935166e
commit b25315fc11
13 changed files with 256 additions and 121 deletions

View File

@@ -2,6 +2,7 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
import 'package:fpdart/fpdart.dart';
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
@@ -9,23 +10,29 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_run_hos
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_hostrity_work_repository.dart';
import '../../../../core/logging/i_logger_service.dart';
class DeviceHostrityWorkRepositoryImpl implements DeviceHostrityWorkRepositoryRepository {
final Dio client;
final UserStorage userStorage;
final ILoggerService _logger = GetIt.I<ILoggerService>();
DeviceHostrityWorkRepositoryImpl({required this.client, required this.userStorage});
@override
Future<Either<DeviceFailure, List<DeviceRunStatisticsEntity>>> getDeviceHRunStatistics({required String deviceId}) async {
try {
debugPrint('开始获取用户信息...');
// debugPrint('开始获取用户信息...');
_logger.logWithLevel('开始获取用户信息...', level: 'info');
final user = await userStorage.getUser();
if (user == null || user.token == null) {
debugPrint('用户未登录或缺少 token');
// debugPrint('用户未登录或缺少 token');
_logger.logWithLevel('用户未登录或缺少 token', level: 'error');
return Left(DeviceFailure.unauthorized(message: 'User not logged in or token is missing'));
}
final token = user.token!;
debugPrint('用户信息获取成功,Token: $token');
// debugPrint('用户信息获取成功,Token: $token');
_logger.logWithLevel('用户信息获取成功,Token: $token', level: 'info');
final response = await client.get(
'https://serviceri.satabot.com/iot/device/getDeviceRunStatistics?deviceId=$deviceId',
@@ -49,18 +56,22 @@ class DeviceHostrityWorkRepositoryImpl implements DeviceHostrityWorkRepositoryRe
.toList();
return Right(statistics);
} else {
debugPrint('服务器返回错误码: ${jsonBody['code']}');
//debugPrint('服务器返回错误码: ${jsonBody['code']}');
_logger.logWithLevel('服务器返回错误码: ${jsonBody['code']}', level: 'error');
return Left(DeviceFailure.serverError(message: '服务器返回错误码: ${jsonBody['code']}'));
}
} else {
debugPrint('网络请求失败,状态码: ${response.statusCode}');
// debugPrint('网络请求失败,状态码: ${response.statusCode}');
_logger.logWithLevel('网络请求失败,状态码: ${response.statusCode}', level: 'error');
return Left(DeviceFailure.networkError(message: '网络请求失败,状态码: ${response.statusCode}'));
}
} on DioException catch (e) {
debugPrint('Dio 请求失败: ${e.message}');
//debugPrint('Dio 请求失败: ${e.message}');
_logger.logWithLevel('Dio 请求失败: ${e.message}', level: 'error');
return Left(DeviceFailure.networkError(message: e.message ?? '未知错误'));
} catch (e) {
debugPrint('请求过程中发生异常: $e');
// debugPrint('请求过程中发生异常: $e');
_logger.logWithLevel('请求过程中发生异常: $e', level: 'error');
return Left(DeviceFailure.unknownError(message: e.toString()));
}
}

View File

@@ -1,5 +1,6 @@
import 'dart:collection';
import 'package:get_it/get_it.dart';
import 'package:isar_community/isar.dart';
import 'package:maibu_satabot_v2/core/logging/i_logger_service.dart';
import 'package:maibu_satabot_v2/features/devices/data/models/device_add_path_point_model.dart';
@@ -13,6 +14,8 @@ import '../models/route_plan_send_entity.dart';
class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
final TcpClient tcp;
late PathPlanner _planner;
final ILoggerService _logger = GetIt.I<ILoggerService>();
// 🔥 新增:存储 AppState 的引用
dynamic _appState;
@@ -23,7 +26,8 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
@override
Future<void> startRoutePlanning(Queue<DeviceAddPathPointModel> locationQueue) async {
// 直接传入 DeviceAddPathPointModel 列表(无需转换)
print("[底层开始发送指令了]");
// print("[底层开始发送指令了]");
_logger.log("开始路径规划");
final List<DeviceAddPathPointModel> locations = locationQueue.toList();
_planner.startRoutePlanning(locations);
}
@@ -60,6 +64,7 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
// true = 使用新的 ACK 握手机制 (将来)
// false = 使用现有逻辑 (现在)
class PathPlanningMode {
static bool useAckHandshake = false; // 默认使用现有逻辑
}
@@ -68,7 +73,7 @@ class PathPlanner {
final Queue<DeviceAddPathPointModel> locationQueue = Queue();
final TcpClient tcpClient;
bool isStart = false;
final ILoggerService _logger = GetIt.I<ILoggerService>();
// 新增:作业控制状态
bool _isPaused = false;
bool _isStopped = false;
@@ -80,7 +85,7 @@ class PathPlanner {
PathPlanner(this.tcpClient);
void sendNextLocation() {
print("[在发送指令sendNextLocation方法中]");
_logger.log("[发送sendNextLocation方法指令]");
// if (isStart && locationQueue.isEmpty) {
// isStart = false;
// print("[track]");
@@ -89,17 +94,19 @@ class PathPlanner {
// }
// 关键检查:暂停或停止时不再发送
if (_isPaused) {
print("⏸️ 当前处于暂停状态,停止发送指令");
_logger.log("[暂停状态],停止发送指令");
return;
}
if (_isStopped || (isStart && locationQueue.isEmpty)) {
isStart = false;
if (_isStopped) {
print("⏹️ 已停止作业,清空队列");
//print("⏹️ 已停止作业,清空队列");
_logger.log("[路径点发送完毕]");
locationQueue.clear(); // 清空剩余队列
} else {
print("[路径点发送完毕]");
//print("[路径点发送完毕]");
_logger.log("[路径点发送完毕]");
}
_isStopped = false; // 重置停止标志
return;
@@ -114,7 +121,8 @@ class PathPlanner {
speed: 1000,
);
print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
//print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
_logger.log("[发送指令]");
tcpClient.sendPathPoint( routePlanSendEntity);
isStart = true;
}
@@ -130,43 +138,51 @@ class PathPlanner {
isStart = true;
_isPaused = false; // 🔥 只在第一次调用时重置
_isStopped = false; // 🔥 只在第一次调用时重置
print("[首次加载] 队列长度:${locations.length}");
// print("[首次加载] 队列长度:${locations.length}");
_logger.log("[首次加载] 队列长度:${locations.length}");
} else {
// 🔥 后续调用(来自 Dispatcher)只更新队列,不重置状态
print("[更新队列] 当前处于 ${_isPaused ? '暂停' : _isStopped ? '停止' : '工作'} 状态");
//print("[更新队列] 当前处于 ${_isPaused ? '暂停' : _isStopped ? '停止' : '工作'} 状态");
_logger.log("[更新队列] 当前处于 ${_isPaused ? '暂停' : _isStopped ? '停止' : '工作'} 状态");
if (!_isPaused && !_isStopped) {
// 如果不是暂停/停止状态,才添加新数据
locationQueue.addAll(locations);
}
}
print("[发送指令要转换类型了完成]");
// print("[发送指令要转换类型了完成]");
_logger.log("[发送指令要转换类型了完成]");
// 🔥 关键检查:如果是暂停/停止状态,直接返回
if (_isPaused) {
print("⏸️ 当前处于暂停状态,拒绝发送指令");
// print("⏸️ 当前处于暂停状态,拒绝发送指令");
_logger.log("[暂停状态]");
return;
}
if (_isStopped) {
print("⏹️ 当前处于停止状态,拒绝发送指令");
// print("⏹️ 当前处于停止状态,拒绝发送指令");
_logger.log("[停止状态]");
return;
}
print("[发送指令要转换类型了完成]");
// sendNextLocation();
// 🔥 根据全局开关选择使用哪套发送逻辑
if (PathPlanningConfig.useAckHandshake) {
print("🔥 使用 ACK 握手机制发送路径点");
// print("🔥 使用 ACK 握手机制发送路径点");
_logger.log("[使用 ACK 握手机制发送路径点]");
sendNextLocationWithAck(sl<NetMessageDispatcher>());
} else {
print("🔥 使用现有逻辑发送路径点");
// print("🔥 使用现有逻辑发送路径点");
_logger.log("[使用现有逻辑发送路径点]");
sendNextLocation();
}
}
/// 暂停
void pauseRPWork() {
print("⏸️ 暂停作业,设置_isPaused = true");
//print("⏸️ 暂停作业,设置_isPaused = true");
_logger.log("[暂停作业,设置_isPaused = true]");
_isPaused = true;
var entity = new RoutePlanSendEntity(
@@ -176,12 +192,14 @@ class PathPlanner {
targetLongitude: 0,
speed: 0,
);
print("📡 发送暂停指令到设备...");
// print("📡 发送暂停指令到设备...");
_logger.log("[发送暂停指令到设备...]");
tcpClient.sendDeviceStateChange(entity);
}
/// 恢复
void resumeRPWork() {
print("▶️ 恢复作业,设置_isPaused = false");
//print("▶️ 恢复作业,设置_isPaused = false");
_logger.log("[恢复作业,设置_isPaused = false]");
_isPaused = false;
var entity = new RoutePlanSendEntity(
@@ -191,7 +209,8 @@ class PathPlanner {
targetLongitude: 0,
speed: 0,
);
print("📡 发送恢复指令到设备...");
// print("📡 发送恢复指令到设备...");
_logger.log("[发送恢复指令到设备...]");
tcpClient.sendDeviceStateChange(entity);
// 恢复后继续发送下一个点
@@ -200,7 +219,8 @@ class PathPlanner {
// sendNextLocation();
// }
if (locationQueue.isNotEmpty) {
print("📍 恢复发送下一个路径点");
//print("📍 恢复发送下一个路径点");
_logger.log("[恢复发送下一个路径点]");
if (PathPlanningConfig.useAckHandshake) {
sendNextLocationWithAck(sl<NetMessageDispatcher>());
} else {
@@ -210,12 +230,14 @@ class PathPlanner {
}
/// 停止
void stopRoutePlanning() {
print("⏹️ 停止作业,设置_isStopped = true");
// print("⏹️ 停止作业,设置_isStopped = true");
_logger.log("[停止作业,设置_isStopped = true]");
_isStopped = true;
_isPaused = false; // 清除暂停状态
// 🔥 核心修复:清空队列
print("🗑️ 清空待发送队列,剩余 ${locationQueue.length} 个点");
// print("🗑️ 清空待发送队列,剩余 ${locationQueue.length} 个点");
_logger.log("[清空待发送队列,剩余 ${locationQueue.length} 个点]");
locationQueue.clear();
@@ -228,7 +250,8 @@ class PathPlanner {
targetLongitude: 0,
speed: 0,
);
print("📡 发送停止指令到设备...");
// print("📡 发送停止指令到设备...");
_logger.log("[发送停止指令到设备...]");
tcpClient.sendDeviceStateChange(entity);
// 发送完成工作播报指令
sendWorkCompleteBroadcast();
@@ -243,32 +266,38 @@ class PathPlanner {
targetLongitude: 0,
speed: 0,
);
print("发送完成工作播报指令...");
// print("发送完成工作播报指令...");
_logger.log("[发送完成工作播报指令...]");
tcpClient.sendDeviceStateChange(entity);
}
// 🔥 新增:ACK 握手机制的发送方法 (新模式)
void sendNextLocationWithAck(NetMessageDispatcher dispatcher) {
print("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
// print("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
_logger.log("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
// 🔥 关键检查:如果正在等待 ACK,不要重复发送
if (_waitingForAck) {
print("⏳ 正在等待 ACK 确认,跳过发送");
//print("⏳ 正在等待 ACK 确认,跳过发送");
_logger.log("[正在等待 ACK 确认,跳过发送]");
return;
}
// 🔥 关键检查:暂停或停止时不再发送
if (_isPaused) {
print("⏸️ 当前处于暂停状态,停止发送指令");
//print("⏸️ 当前处于暂停状态,停止发送指令");
_logger.log("[暂停状态]");
return;
}
if (_isStopped || (isStart && locationQueue.isEmpty)) {
isStart = false;
if (_isStopped) {
print("️ 已停止作业,清空队列");
// print("️ 已停止作业,清空队列");
_logger.log("[已停止作业,清空队列]");
locationQueue.clear(); // 清空剩余队列
} else {
print("[路径点发送完毕]");
_logger.log("[路径点发送完毕]");
}
_isStopped = false; // 重置停止标志
return;
@@ -287,14 +316,17 @@ class PathPlanner {
speed: 1000,
);
print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
//print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
_logger.log("[发送路径点] Lat:${entity.latitude} Lng:${entity.longitude}");
tcpClient.sendPathPoint( routePlanSendEntity);
isStart = true;
// 🔥 关键:标记为正在等待 ACK
_waitingForAck = true;
dispatcher.setExpectedPointIndex(pointIndex); // 🔥 通知 Dispatcher
print(" 已锁定发送,等待 ACK 确认 (0x02)...");
//sprint(" 已锁定发送,等待 ACK 确认 (0x02)...");
_logger.log("[已锁定发送,等待 ACK 确认 (0x02)...]");
}
}