From f3bcba15b1fd9932b31aed4e4a9aa9d34931e673 Mon Sep 17 00:00:00 2001 From: Songzex <2402265378@qq.com> Date: Wed, 11 Mar 2026 17:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=8A=B2=E8=A7=84?= =?UTF-8?q?=E5=88=92=E6=9A=82=E5=81=9C=E5=92=8C=E7=BB=A7=E7=BB=AD=E5=92=8C?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E7=9A=84tcp=E6=8C=87=E4=BB=A4=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/network/net_message_dispatcher.dart | 6 +- lib/core/network/tcp/tcp_client.dart | 6 +- .../route_planning_repository_impl.dart | 84 +++++++++++++++++-- .../widgets/map/testmap_pages.dart | 25 +++++- 4 files changed, 106 insertions(+), 15 deletions(-) diff --git a/lib/core/network/net_message_dispatcher.dart b/lib/core/network/net_message_dispatcher.dart index d3e9289a..4754ddb5 100644 --- a/lib/core/network/net_message_dispatcher.dart +++ b/lib/core/network/net_message_dispatcher.dart @@ -96,9 +96,11 @@ class NetMessageDispatcher { ///取全局的路径规划发送实体 queue /// 发送下一个指令 移除上一条数据 var queue = _pathPlanningService.getQueue(); - //去除第一条数据 + //去除上一条数据 queue.removeFirst(); - _pathPlanningService.updateQueue(queue as List); + /// _pathPlanningService.updateQueue(queue as List); + _pathPlanningService.updateQueue(queue.toList()); + debugPrint('📡 [Dispatcher] 准备触发下一个路径点发送,队列剩余:${queue.length}'); routePlanningRepository.startRoutePlanning(queue); return true; } else { diff --git a/lib/core/network/tcp/tcp_client.dart b/lib/core/network/tcp/tcp_client.dart index 5e7efae0..ce6c1e15 100644 --- a/lib/core/network/tcp/tcp_client.dart +++ b/lib/core/network/tcp/tcp_client.dart @@ -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"); - } diff --git a/lib/features/devices/data/repositories/route_planning_repository_impl.dart b/lib/features/devices/data/repositories/route_planning_repository_impl.dart index 232fce3d..366207ab 100644 --- a/lib/features/devices/data/repositories/route_planning_repository_impl.dart +++ b/lib/features/devices/data/repositories/route_planning_repository_impl.dart @@ -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 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状态 } + } diff --git a/lib/features/home/presentation/widgets/map/testmap_pages.dart b/lib/features/home/presentation/widgets/map/testmap_pages.dart index 66f187a0..50b3fc3c 100644 --- a/lib/features/home/presentation/widgets/map/testmap_pages.dart +++ b/lib/features/home/presentation/widgets/map/testmap_pages.dart @@ -159,6 +159,7 @@ class _MapPageEnterpriseState extends State { ]; // 其他 + StreamSubscription? _pathPlanningSub; @override void initState() { @@ -190,6 +191,9 @@ class _MapPageEnterpriseState extends State { } }); + // 🔥 关键修复:开始监听路径规划指令应答 + _setupPathPlanningListener(); + // 监听地图移动事件,实时更新连线 _mapController.mapEventStream.listen((event) { if (event is MapEventMove || event is MapEventMoveEnd) { @@ -205,6 +209,21 @@ class _MapPageEnterpriseState extends State { }); } + // 🔥 核心修复:监听路径规划应答流 + void _setupPathPlanningListener() { + debugPrint('🔗 [MapPage] 开始监听路径规划指令应答 (CMD: 0x01)'); + + _pathPlanningSub = _dispatcher.onPathPlanningResponse().listen( + (packet) { + debugPrint('✅ [MapPage] 收到路径规划应答,准备发送下一个点'); + // 这里不需要做任何事情,因为 NetMessageDispatcher 内部已经处理了 + // 它会自动从队列中移除已发送的点并触发下一个点 + }, + onDone: () => debugPrint('⚠️ [MapPage] 路径规划流已结束'), + onError: (e) => debugPrint('❌ [MapPage] 路径规划流错误:$e'), + ); + } + // 新增:手动清除本地存储数据 Future _clearLocalData() async { try { @@ -916,6 +935,7 @@ class _MapPageEnterpriseState extends State { void dispose() { _traceManager.reset(); _mapController.dispose(); + _pathPlanningSub?.cancel(); // 🔥 取消订阅 super.dispose(); } @@ -1475,6 +1495,7 @@ class _MapPageEnterpriseState extends State { _traceManager.setMode(TPMode.NAVIGATION); setState(() { + isStartWork = true; // 🔥 关键:停止作业标志 _workStatus = WorkStatus.working; }); @@ -1513,6 +1534,7 @@ class _MapPageEnterpriseState extends State { /// 暂停作业 void _pauseWork() async { setState(() { + isStartWork = false; // 🔥 关键:停止作业标志 _workStatus = WorkStatus.paused; }); context.read().updateAppState(AppState.none); @@ -1522,6 +1544,7 @@ class _MapPageEnterpriseState extends State { void _stopWork() async { setState(() { + isStartWork = false; // 🔥 关键:停止作业标志 _workStatus = WorkStatus.idle; }); context.read().updateAppState(AppState.none); @@ -1529,7 +1552,7 @@ class _MapPageEnterpriseState extends State { //修改App的状态 debugPrint('停止作业:${_selectedPlot!.plotName}'); - ///ToastUtils.showError(context, '作业已停止'); + ToastUtils.showError(context, '作业已停止'); } /// 继续作业