修复路劲规划暂停和继续和停止的tcp指令的数据格式
This commit is contained in:
@@ -96,9 +96,11 @@ class NetMessageDispatcher {
|
||||
///取全局的路径规划发送实体 queue
|
||||
/// 发送下一个指令 移除上一条数据
|
||||
var queue = _pathPlanningService.getQueue();
|
||||
//去除第一条数据
|
||||
//去除上一条数据
|
||||
queue.removeFirst();
|
||||
_pathPlanningService.updateQueue(queue as List<DeviceAddPathPointModel>);
|
||||
/// _pathPlanningService.updateQueue(queue as List<DeviceAddPathPointModel>);
|
||||
_pathPlanningService.updateQueue(queue.toList());
|
||||
debugPrint('📡 [Dispatcher] 准备触发下一个路径点发送,队列剩余:${queue.length}');
|
||||
routePlanningRepository.startRoutePlanning(queue);
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@@ -277,12 +277,10 @@ class TcpClient {
|
||||
void sendDeviceStateChange(RoutePlanSendEntity routePlanSendEntity) {
|
||||
if (_socket == null) return;
|
||||
final payload = routePlanSendEntity.toBytes();
|
||||
//_socket!.add(payload);
|
||||
sendRaw(0x02, payload);
|
||||
// 🔥 核心修复:直接发送字节数组,不再用 sendRaw 包装
|
||||
_socket!.add(payload);
|
||||
var counts = routePlanSendEntity.pointCounts;
|
||||
var string = routePlanSendEntity.toString();
|
||||
print("sendDeviceStateChange 底层发送指令完成,数据:$counts");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,14 +56,35 @@ class PathPlanner {
|
||||
final TcpClient tcpClient;
|
||||
bool isStart = false;
|
||||
|
||||
// 🔥 新增:作业控制状态
|
||||
bool _isPaused = false;
|
||||
bool _isStopped = false;
|
||||
|
||||
PathPlanner(this.tcpClient);
|
||||
|
||||
void sendNextLocation() {
|
||||
print("[在发送指令sendNextLocation方法中]");
|
||||
if (isStart && locationQueue.isEmpty) {
|
||||
// if (isStart && locationQueue.isEmpty) {
|
||||
// isStart = false;
|
||||
// print("[track]");
|
||||
// tcpClient.disconnect();
|
||||
// return;
|
||||
// }
|
||||
// 🔥 关键检查:暂停或停止时不再发送
|
||||
if (_isPaused) {
|
||||
print("⏸️ 当前处于暂停状态,停止发送指令");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isStopped || (isStart && locationQueue.isEmpty)) {
|
||||
isStart = false;
|
||||
print("[track]");
|
||||
tcpClient.disconnect();
|
||||
if (_isStopped) {
|
||||
print("⏹️ 已停止作业,清空队列");
|
||||
locationQueue.clear(); // 清空剩余队列
|
||||
} else {
|
||||
print("[路径点发送完毕]");
|
||||
}
|
||||
_isStopped = false; // 重置停止标志
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -82,14 +103,47 @@ class PathPlanner {
|
||||
}
|
||||
/// 开始
|
||||
void startRoutePlanning(List<DeviceAddPathPointModel> locations) {
|
||||
locationQueue.addAll(locations);
|
||||
isStart = true;
|
||||
/// locationQueue.addAll(locations);
|
||||
/// isStart = true;
|
||||
/// _isPaused = false; // 重置暂停标志
|
||||
/// _isStopped = false; // 重置停止标志
|
||||
/// 🔥 关键修复:只在队列为空时才添加(第一次调用)
|
||||
if (locationQueue.isEmpty) {
|
||||
locationQueue.addAll(locations);
|
||||
isStart = true;
|
||||
_isPaused = false; // 🔥 只在第一次调用时重置
|
||||
_isStopped = false; // 🔥 只在第一次调用时重置
|
||||
print("[首次加载] 队列长度:${locations.length}");
|
||||
} else {
|
||||
// 🔥 后续调用(来自 Dispatcher)只更新队列,不重置状态
|
||||
print("[更新队列] 当前处于 ${_isPaused ? '暂停' : _isStopped ? '停止' : '工作'} 状态");
|
||||
if (!_isPaused && !_isStopped) {
|
||||
// 如果不是暂停/停止状态,才添加新数据
|
||||
locationQueue.addAll(locations);
|
||||
}
|
||||
}
|
||||
|
||||
print("[发送指令要转换类型了完成]");
|
||||
|
||||
// 🔥 关键检查:如果是暂停/停止状态,直接返回
|
||||
if (_isPaused) {
|
||||
print("⏸️ 当前处于暂停状态,拒绝发送指令");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isStopped) {
|
||||
print("⏹️ 当前处于停止状态,拒绝发送指令");
|
||||
return;
|
||||
}
|
||||
print("[发送指令要转换类型了完成]");
|
||||
sendNextLocation();
|
||||
|
||||
}
|
||||
/// 暂停
|
||||
void pauseRPWork() {
|
||||
print("⏸️ 暂停作业,设置_isPaused = true");
|
||||
_isPaused = true;
|
||||
|
||||
var entity = new RoutePlanSendEntity(
|
||||
commandType: 0x02,
|
||||
pointCounts: 2,
|
||||
@@ -97,11 +151,14 @@ class PathPlanner {
|
||||
targetLongitude: 0,
|
||||
speed: 0,
|
||||
);
|
||||
print("📡 发送暂停指令到设备...");
|
||||
tcpClient.sendDeviceStateChange(entity);
|
||||
//更新APP状态
|
||||
}
|
||||
/// 恢复
|
||||
void resumeRPWork() {
|
||||
print("▶️ 恢复作业,设置_isPaused = false");
|
||||
_isPaused = false;
|
||||
|
||||
var entity = new RoutePlanSendEntity(
|
||||
commandType: 0x02,
|
||||
pointCounts: 3,
|
||||
@@ -109,11 +166,21 @@ class PathPlanner {
|
||||
targetLongitude: 0,
|
||||
speed: 0,
|
||||
);
|
||||
print("📡 发送恢复指令到设备...");
|
||||
tcpClient.sendDeviceStateChange(entity);
|
||||
//更新APP状态
|
||||
|
||||
// 恢复后继续发送下一个点
|
||||
if (locationQueue.isNotEmpty) {
|
||||
print("📍 恢复发送下一个路径点");
|
||||
sendNextLocation();
|
||||
}
|
||||
}
|
||||
/// 停止
|
||||
void stopRoutePlanning() {
|
||||
print("⏹️ 停止作业,设置_isStopped = true");
|
||||
_isStopped = true;
|
||||
_isPaused = false; // 清除暂停状态
|
||||
|
||||
var entity = new RoutePlanSendEntity(
|
||||
commandType: 0x02,
|
||||
pointCounts: 0,
|
||||
@@ -121,11 +188,12 @@ class PathPlanner {
|
||||
targetLongitude: 0,
|
||||
speed: 0,
|
||||
);
|
||||
print("📡 发送停止指令到设备...");
|
||||
tcpClient.sendDeviceStateChange(entity);
|
||||
//更新APP状态
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -159,6 +159,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
];
|
||||
|
||||
// 其他
|
||||
StreamSubscription? _pathPlanningSub;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -190,6 +191,9 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
}
|
||||
});
|
||||
|
||||
// 🔥 关键修复:开始监听路径规划指令应答
|
||||
_setupPathPlanningListener();
|
||||
|
||||
// 监听地图移动事件,实时更新连线
|
||||
_mapController.mapEventStream.listen((event) {
|
||||
if (event is MapEventMove || event is MapEventMoveEnd) {
|
||||
@@ -205,6 +209,21 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
});
|
||||
}
|
||||
|
||||
// 🔥 核心修复:监听路径规划应答流
|
||||
void _setupPathPlanningListener() {
|
||||
debugPrint('🔗 [MapPage] 开始监听路径规划指令应答 (CMD: 0x01)');
|
||||
|
||||
_pathPlanningSub = _dispatcher.onPathPlanningResponse().listen(
|
||||
(packet) {
|
||||
debugPrint('✅ [MapPage] 收到路径规划应答,准备发送下一个点');
|
||||
// 这里不需要做任何事情,因为 NetMessageDispatcher 内部已经处理了
|
||||
// 它会自动从队列中移除已发送的点并触发下一个点
|
||||
},
|
||||
onDone: () => debugPrint('⚠️ [MapPage] 路径规划流已结束'),
|
||||
onError: (e) => debugPrint('❌ [MapPage] 路径规划流错误:$e'),
|
||||
);
|
||||
}
|
||||
|
||||
// 新增:手动清除本地存储数据
|
||||
Future<void> _clearLocalData() async {
|
||||
try {
|
||||
@@ -916,6 +935,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
void dispose() {
|
||||
_traceManager.reset();
|
||||
_mapController.dispose();
|
||||
_pathPlanningSub?.cancel(); // 🔥 取消订阅
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -1475,6 +1495,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
_traceManager.setMode(TPMode.NAVIGATION);
|
||||
|
||||
setState(() {
|
||||
isStartWork = true; // 🔥 关键:停止作业标志
|
||||
_workStatus = WorkStatus.working;
|
||||
});
|
||||
|
||||
@@ -1513,6 +1534,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
/// 暂停作业
|
||||
void _pauseWork() async {
|
||||
setState(() {
|
||||
isStartWork = false; // 🔥 关键:停止作业标志
|
||||
_workStatus = WorkStatus.paused;
|
||||
});
|
||||
context.read<DevicesCubit>().updateAppState(AppState.none);
|
||||
@@ -1522,6 +1544,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
|
||||
void _stopWork() async {
|
||||
setState(() {
|
||||
isStartWork = false; // 🔥 关键:停止作业标志
|
||||
_workStatus = WorkStatus.idle;
|
||||
});
|
||||
context.read<DevicesCubit>().updateAppState(AppState.none);
|
||||
@@ -1529,7 +1552,7 @@ class _MapPageEnterpriseState extends State<MapPageEnterprise> {
|
||||
//修改App的状态
|
||||
debugPrint('停止作业:${_selectedPlot!.plotName}');
|
||||
|
||||
///ToastUtils.showError(context, '作业已停止');
|
||||
ToastUtils.showError(context, '作业已停止');
|
||||
}
|
||||
|
||||
/// 继续作业
|
||||
|
||||
Reference in New Issue
Block a user