完成路径规划发送的握手机制-一对一校验应答-待测试
完成路径规划握手机制的开关功能。
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import 'dart:collection';
|
||||
|
||||
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';
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../../core/env/path_planning_config.dart';
|
||||
import '../../../../core/network/net_message_dispatcher.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../domain/repositories/route_planning_repository.dart';
|
||||
import '../models/route_plan_send_entity.dart';
|
||||
@@ -8,6 +12,7 @@ import '../models/route_plan_send_entity.dart';
|
||||
class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
|
||||
final TcpClient tcp;
|
||||
late PathPlanner _planner;
|
||||
|
||||
// 🔥 新增:存储 AppState 的引用
|
||||
dynamic _appState;
|
||||
|
||||
@@ -50,15 +55,26 @@ class RoutePlanningRepositoryImpl implements RoutePlanningRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// 全局开关:控制使用哪套路径规划逻辑
|
||||
// true = 使用新的 ACK 握手机制 (将来)
|
||||
// false = 使用现有逻辑 (现在)
|
||||
class PathPlanningMode {
|
||||
static bool useAckHandshake = false; // 默认使用现有逻辑
|
||||
}
|
||||
|
||||
// PathPlanner
|
||||
class PathPlanner {
|
||||
final Queue<DeviceAddPathPointModel> locationQueue = Queue();
|
||||
final TcpClient tcpClient;
|
||||
bool isStart = false;
|
||||
|
||||
// 🔥 新增:作业控制状态
|
||||
// 新增:作业控制状态
|
||||
bool _isPaused = false;
|
||||
bool _isStopped = false;
|
||||
// 新增:ACK 握手状态 (仅新模式使用)
|
||||
bool _waitingForAck = false; // 是否正在等待 ACK 确认
|
||||
int _currentPointIndex = 0; // 🔥 当前发送的点编号(从 0 开始递增)
|
||||
|
||||
|
||||
PathPlanner(this.tcpClient);
|
||||
|
||||
@@ -70,7 +86,7 @@ class PathPlanner {
|
||||
// tcpClient.disconnect();
|
||||
// return;
|
||||
// }
|
||||
// 🔥 关键检查:暂停或停止时不再发送
|
||||
// 关键检查:暂停或停止时不再发送
|
||||
if (_isPaused) {
|
||||
print("⏸️ 当前处于暂停状态,停止发送指令");
|
||||
return;
|
||||
@@ -136,7 +152,15 @@ class PathPlanner {
|
||||
return;
|
||||
}
|
||||
print("[发送指令要转换类型了完成]");
|
||||
sendNextLocation();
|
||||
// sendNextLocation();
|
||||
// 🔥 根据全局开关选择使用哪套发送逻辑
|
||||
if (PathPlanningConfig.useAckHandshake) {
|
||||
print("🔥 使用 ACK 握手机制发送路径点");
|
||||
sendNextLocationWithAck(sl<NetMessageDispatcher>());
|
||||
} else {
|
||||
print("🔥 使用现有逻辑发送路径点");
|
||||
sendNextLocation();
|
||||
}
|
||||
|
||||
}
|
||||
/// 暂停
|
||||
@@ -170,9 +194,17 @@ class PathPlanner {
|
||||
tcpClient.sendDeviceStateChange(entity);
|
||||
|
||||
// 恢复后继续发送下一个点
|
||||
// if (locationQueue.isNotEmpty) {
|
||||
// print("📍 恢复发送下一个路径点");
|
||||
// sendNextLocation();
|
||||
// }
|
||||
if (locationQueue.isNotEmpty) {
|
||||
print("📍 恢复发送下一个路径点");
|
||||
sendNextLocation();
|
||||
if (PathPlanningConfig.useAckHandshake) {
|
||||
sendNextLocationWithAck(sl<NetMessageDispatcher>());
|
||||
} else {
|
||||
sendNextLocation();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// 停止
|
||||
@@ -199,7 +231,55 @@ class PathPlanner {
|
||||
tcpClient.sendDeviceStateChange(entity);
|
||||
}
|
||||
|
||||
// 🔥 新增:ACK 握手机制的发送方法 (新模式)
|
||||
void sendNextLocationWithAck(NetMessageDispatcher dispatcher) {
|
||||
print("[在发送指令 sendNextLocationWithAck 方法中 - ACK 模式]");
|
||||
// 🔥 关键检查:如果正在等待 ACK,不要重复发送
|
||||
if (_waitingForAck) {
|
||||
print("⏳ 正在等待 ACK 确认,跳过发送");
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 关键检查:暂停或停止时不再发送
|
||||
if (_isPaused) {
|
||||
print("⏸️ 当前处于暂停状态,停止发送指令");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isStopped || (isStart && locationQueue.isEmpty)) {
|
||||
isStart = false;
|
||||
if (_isStopped) {
|
||||
print("️ 已停止作业,清空队列");
|
||||
locationQueue.clear(); // 清空剩余队列
|
||||
} else {
|
||||
print("[路径点发送完毕]");
|
||||
}
|
||||
_isStopped = false; // 重置停止标志
|
||||
return;
|
||||
}
|
||||
|
||||
final entity = locationQueue.removeFirst(); // 类型:DeviceAddPathPointModel
|
||||
|
||||
// 🔥 核心改进:使用递增的点编号,确保精准匹配
|
||||
final pointIndex = _currentPointIndex++;
|
||||
|
||||
final routePlanSendEntity = RoutePlanSendEntity(
|
||||
commandType: 0x01,
|
||||
pointCounts: pointIndex, // 🔥 使用点编号,而不是固定的 1
|
||||
targetLatitude: entity.latitude,
|
||||
targetLongitude: entity.longitude,
|
||||
speed: 1000,
|
||||
);
|
||||
|
||||
print("====Lat:${entity.latitude} ====Lng:${entity.longitude}");
|
||||
tcpClient.sendPathPoint( routePlanSendEntity);
|
||||
isStart = true;
|
||||
|
||||
// 🔥 关键:标记为正在等待 ACK
|
||||
_waitingForAck = true;
|
||||
dispatcher.setExpectedPointIndex(pointIndex); // 🔥 通知 Dispatcher
|
||||
print(" 已锁定发送,等待 ACK 确认 (0x02)...");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user