路径规划修复

This commit is contained in:
mmc
2026-04-15 16:38:54 +08:00
parent 803b9bd861
commit 4c11f06513
3 changed files with 114 additions and 148 deletions

View File

@@ -16,17 +16,17 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
late PathPlanner _planner;
final ILoggerService _logger = GetIt.I<ILoggerService>();
// 🔥 新增:存储 AppState 的引用
dynamic _appState;
RoutePlanningRepositoryImpl({required this.tcp}) { // ✅ 加 {required} 和类型注解
RoutePlanningRepositoryImpl({required this.tcp}) {
// ✅ 加 {required} 和类型注解
_planner = PathPlanner(tcp);
}
@override
Future<void> startRoutePlanning(Queue<DeviceAddPathPointModel> locationQueue) async {
// 直接传入 DeviceAddPathPointModel 列表(无需转换)
// print("[底层开始发送指令了]");
// print("[底层开始发送指令了]");
_logger.log("开始路径规划");
final List<DeviceAddPathPointModel> locations = locationQueue.toList();
_planner.startRoutePlanning(locations);
@@ -55,6 +55,7 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
// TODO: implement getCurrentAppState
return _appState;
}
void setAppState(dynamic state) {
_appState = state;
}
@@ -64,8 +65,7 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
// true = 使用新的 ACK 握手机制 (将来)
// false = 使用现有逻辑 (现在)
class PathPlanningMode {
static bool useAckHandshake = false; // 默认使用现有逻辑
static bool useAckHandshake = false; // 默认使用现有逻辑
}
// PathPlanner
@@ -78,9 +78,8 @@ class PathPlanner {
bool _isPaused = false;
bool _isStopped = false;
// 新增:ACK 握手状态 (仅新模式使用)
bool _waitingForAck = false; // 是否正在等待 ACK 确认
int _currentPointIndex = 0; // 🔥 当前发送的点编号(从 0 开始递增)
bool _waitingForAck = false; // 是否正在等待 ACK 确认
int _currentPointIndex = 0; // 🔥 当前发送的点编号(从 0 开始递增)
PathPlanner(this.tcpClient);
@@ -102,7 +101,7 @@ class PathPlanner {
isStart = false;
if (_isStopped) {
//print("⏹️ 已停止作业,清空队列");
_logger.log("[路径点发送完毕]");
_logger.log("[路径点发送完毕] 停止");
locationQueue.clear(); // 清空剩余队列
} else {
//print("[路径点发送完毕]");
@@ -123,10 +122,11 @@ class PathPlanner {
//print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
_logger.log("[发送指令]");
tcpClient.sendPathPoint( routePlanSendEntity);
tcpClient.sendPathPoint(routePlanSendEntity);
isStart = true;
}
/// 开始
/// 开始
void startRoutePlanning(List<DeviceAddPathPointModel> locations) {
/// locationQueue.addAll(locations);
/// isStart = true;
@@ -138,30 +138,36 @@ class PathPlanner {
isStart = true;
_isPaused = false; // 🔥 只在第一次调用时重置
_isStopped = false; // 🔥 只在第一次调用时重置
// print("[首次加载] 队列长度:${locations.length}");
// print("[首次加载] 队列长度:${locations.length}");
_logger.log("[首次加载] 队列长度:${locations.length}");
} else {
// 🔥 后续调用(来自 Dispatcher)只更新队列,不重置状态
//print("[更新队列] 当前处于 ${_isPaused ? '暂停' : _isStopped ? '停止' : '工作'} 状态");
_logger.log("[更新队列] 当前处于 ${_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;
}
@@ -169,47 +175,36 @@ class PathPlanner {
// 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");
_logger.log("[暂停作业,设置_isPaused = true]");
_isPaused = true;
var entity = new RoutePlanSendEntity(
commandType: 0x01,
pointCounts: 2,
targetLatitude: 0,
targetLongitude: 0,
speed: 0,
);
// print("📡 发送暂停指令到设备...");
var entity = new RoutePlanSendEntity(commandType: 0x01, pointCounts: 2, targetLatitude: 0, targetLongitude: 0, speed: 0);
// print("📡 发送暂停指令到设备...");
_logger.log("[发送暂停指令到设备...]");
tcpClient.sendDeviceStateChange(entity);
}
/// 恢复
void resumeRPWork() {
//print("▶️ 恢复作业,设置_isPaused = false");
_logger.log("[恢复作业,设置_isPaused = false]");
_isPaused = false;
var entity = new RoutePlanSendEntity(
commandType: 0x01,
pointCounts: 3,
targetLatitude: 0,
targetLongitude: 0,
speed: 0,
);
// print("📡 发送恢复指令到设备...");
var entity = new RoutePlanSendEntity(commandType: 0x01, pointCounts: 3, targetLatitude: 0, targetLongitude: 0, speed: 0);
// print("📡 发送恢复指令到设备...");
_logger.log("[发送恢复指令到设备...]");
tcpClient.sendDeviceStateChange(entity);
@@ -228,52 +223,38 @@ 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();
var entity = new RoutePlanSendEntity(
commandType: 0x01,
pointCounts: 0,
targetLatitude: 0,
targetLongitude: 0,
speed: 0,
);
// print("📡 发送停止指令到设备...");
_logger.log("[发送停止指令到设备...]");
var entity = new RoutePlanSendEntity(commandType: 0x01, pointCounts: 0, targetLatitude: 0, targetLongitude: 0, speed: 0);
// print("📡 发送停止指令到设备...");
_logger.log("[发送停止指令到设备...] 按下停止按钮");
tcpClient.sendDeviceStateChange(entity);
// 发送完成工作播报指令
sendWorkCompleteBroadcast();
//sendWorkCompleteBroadcast();
}
//发送完成工作播报指令
void sendWorkCompleteBroadcast() {
var entity = new RoutePlanSendEntity(
commandType: 0x01,
pointCounts: 0xFF,
targetLatitude: 0,
targetLongitude: 0,
speed: 0,
);
// print("发送完成工作播报指令...");
var entity = new RoutePlanSendEntity(commandType: 0x01, pointCounts: 0xFF, targetLatitude: 0, targetLongitude: 0, speed: 0);
// print("发送完成工作播报指令...");
_logger.log("[发送完成工作播报指令...]");
tcpClient.sendDeviceStateChange(entity);
}
// 🔥 新增:ACK 握手机制的发送方法 (新模式)
// 🔥 新增:ACK 握手机制的发送方法 (新模式)
void sendNextLocationWithAck(NetMessageDispatcher dispatcher) {
// print("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
// print("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
_logger.log("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
// 🔥 关键检查:如果正在等待 ACK,不要重复发送
if (_waitingForAck) {
@@ -292,7 +273,7 @@ class PathPlanner {
if (_isStopped || (isStart && locationQueue.isEmpty)) {
isStart = false;
if (_isStopped) {
// print("️ 已停止作业,清空队列");
// print("️ 已停止作业,清空队列");
_logger.log("[已停止作业,清空队列]");
locationQueue.clear(); // 清空剩余队列
} else {
@@ -318,17 +299,13 @@ class PathPlanner {
//print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
_logger.log("[发送路径点] Lat:${entity.latitude} Lng:${entity.longitude}");
tcpClient.sendPathPoint( routePlanSendEntity);
tcpClient.sendPathPoint(routePlanSendEntity);
isStart = true;
// 🔥 关键:标记为正在等待 ACK
_waitingForAck = true;
dispatcher.setExpectedPointIndex(pointIndex); // 🔥 通知 Dispatcher
dispatcher.setExpectedPointIndex(pointIndex); // 🔥 通知 Dispatcher
//sprint(" 已锁定发送,等待 ACK 确认 (0x02)...");
_logger.log("[已锁定发送,等待 ACK 确认 (0x02)...]");
}
}