完善路线返航和障碍物显示
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../../core/localization/app_localizations.dart';
|
||||
import '../../../../core/network/mqtt/domain/entities/mower_obstacle_entity.dart';
|
||||
import '../../../../core/network/mqtt/domain/repositories/mower_realtime_repository.dart';
|
||||
|
||||
/// 三处业务地图共用的实时感知层,生命周期与地图一致。
|
||||
class MowerObstacleLayer extends StatefulWidget {
|
||||
const MowerObstacleLayer({super.key, required this.deviceSn});
|
||||
|
||||
final String deviceSn;
|
||||
|
||||
@override
|
||||
State<MowerObstacleLayer> createState() => _MowerObstacleLayerState();
|
||||
}
|
||||
|
||||
class _MowerObstacleLayerState extends State<MowerObstacleLayer> {
|
||||
late Stream<List<MowerObstacleEntity>> _stream = _watchObstacles();
|
||||
|
||||
Stream<List<MowerObstacleEntity>> _watchObstacles() {
|
||||
final sn = widget.deviceSn.trim();
|
||||
return sn.isEmpty
|
||||
? const Stream.empty()
|
||||
: sl<MowerRealtimeRepository>().watchObstacles(sn);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant MowerObstacleLayer oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.deviceSn.trim() != widget.deviceSn.trim()) {
|
||||
_stream = _watchObstacles();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
return StreamBuilder<List<MowerObstacleEntity>>(
|
||||
// 换设备后丢弃旧快照,等待新设备的首帧。
|
||||
key: ValueKey(widget.deviceSn.trim()),
|
||||
stream: _stream,
|
||||
builder: (context, snapshot) {
|
||||
final objects = snapshot.hasError
|
||||
? const <MowerObstacleEntity>[]
|
||||
: snapshot.data ?? const <MowerObstacleEntity>[];
|
||||
return MarkerLayer(
|
||||
markers: [
|
||||
for (final object in objects)
|
||||
Marker(
|
||||
point: LatLng(object.latitude, object.longitude),
|
||||
width: 36,
|
||||
height: 36,
|
||||
child: _marker(object, l10n),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _marker(MowerObstacleEntity object, AppLocalizations l10n) {
|
||||
final (icon, color) = switch (object.classId) {
|
||||
0 => (Icons.directions_bus, const Color(0xFF7C3AED)),
|
||||
1 => (Icons.directions_car, const Color(0xFF1677FF)),
|
||||
2 => (Icons.two_wheeler, const Color(0xFFEA580C)),
|
||||
3 => (Icons.person, const Color(0xFFE11D48)),
|
||||
4 => (Icons.local_shipping, const Color(0xFF0D9488)),
|
||||
_ => (Icons.warning_amber_rounded, const Color(0xFF64748B)),
|
||||
};
|
||||
final category = l10n.translate('perception_obstacle.${object.categoryKey}');
|
||||
final title = object.trackId == null
|
||||
? category
|
||||
: '$category #${object.trackId}';
|
||||
final details = <String>[
|
||||
title,
|
||||
if (object.distance != null && object.distance! >= 0)
|
||||
l10n.translate('perception_obstacle.distance', {
|
||||
'value': object.distance!.toStringAsFixed(1),
|
||||
}),
|
||||
if (object.confidence != null &&
|
||||
object.confidence! >= 0 && object.confidence! <= 1)
|
||||
l10n.translate('perception_obstacle.confidence', {
|
||||
'value': (object.confidence! * 100).toStringAsFixed(0),
|
||||
}),
|
||||
];
|
||||
return Tooltip(
|
||||
message: details.join('\n'),
|
||||
triggerMode: TooltipTriggerMode.tap,
|
||||
child: Semantics(
|
||||
label: title,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: color, width: 2),
|
||||
boxShadow: const [
|
||||
BoxShadow(color: Colors.black26, blurRadius: 3),
|
||||
],
|
||||
),
|
||||
child: Icon(icon, color: color, size: 24),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -19,18 +19,20 @@ import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
|
||||
import '../theme/pv_colors.dart';
|
||||
import '../models/work_trace_recorder.dart';
|
||||
import 'mower_obstacle_layer.dart';
|
||||
|
||||
/// HUD 作业地图:与「路径规划」同源、同图层、同样式的**只读**实时地图。
|
||||
///
|
||||
/// 图层顺序与配色完全照抄路径规划(testmap_pages._buildMap),只保留展示所需的部分:
|
||||
/// ① ESRI 卫星底图 ② 场站无人机高清叠加层 ③ 规划路线(金黄) ④ 已走轨迹(绿) ⑤ 机器指针。
|
||||
/// 打点、障碍物、保存、开始作业等交互一概不做;路径规划的文件零改动。
|
||||
/// 实时感知目标由独立图层展示;区域编辑与作业操作由外部页面承载。
|
||||
///
|
||||
/// 数据全部来自 MQTT(TCP 链路已停用):
|
||||
/// - 机器位置/航向:全局单例 [DeviceStatusBloc] 的 `DeviceStatusUpdated`
|
||||
/// (mower/{sn}/property/location/post,bloc 内 500ms 节流),与路径规划同一条流;
|
||||
/// - 规划路线:`MowerRealtimeRepository.routeStatusStream`(device/{sn}/route/post,
|
||||
/// 广播流,这里只是多一个订阅者,不会影响路径规划页的订阅)。
|
||||
/// - 感知障碍物:`mower/{sn}/perception/obstacle/post`,由图层按设备独立订阅。
|
||||
///
|
||||
/// 与路径规划的唯一行为差异(按需求新增):机器**始终显示在正中间**。用户手拖/缩放
|
||||
/// 会暂停跟随,右下角「定位」按钮恢复。
|
||||
@@ -54,6 +56,7 @@ class PvWorkMap extends StatefulWidget {
|
||||
this.draftObstacle = false,
|
||||
this.draftCrosshair = false,
|
||||
this.returnPoints = const <LatLng>[],
|
||||
this.returnObstacleHoles = const <List<LatLng>>[],
|
||||
this.followDevice = true,
|
||||
this.centerOnDeviceOnOpen = false,
|
||||
this.recenterSignal = 0,
|
||||
@@ -79,7 +82,7 @@ class PvWorkMap extends StatefulWidget {
|
||||
});
|
||||
|
||||
/// 设备 SN:用于过滤路线推送(deviceId 不匹配的消息直接丢弃)。
|
||||
/// 订阅本身由页面层 `startMqttRealtimeListening` 保证,这里不重复切换全局设备。
|
||||
/// 机器状态订阅由页面层保证;障碍物图层按此 SN 独立订阅,不切换全局设备。
|
||||
final String deviceSn;
|
||||
|
||||
/// 是否允许拖动/缩放:左栏小卡传 false(避免与列表滚动、摇杆抢手势),
|
||||
@@ -141,6 +144,8 @@ class PvWorkMap extends StatefulWidget {
|
||||
/// 一键返航点:紫色虚线串联 + 紫色编号徽标(首点显示「H」)。
|
||||
/// 与作业区橙色打点、禁行区红色框配色区分,互不影响。
|
||||
final List<LatLng> returnPoints;
|
||||
/// 返航障碍独立显示,不依赖圈地/禁行区的编辑开关。
|
||||
final List<List<LatLng>> returnObstacleHoles;
|
||||
|
||||
/// 是否让地图随设备位置自动居中。区域编辑时传 false:
|
||||
/// 否则每次定位推送都会把地图挪走,正在打的位置就跑了。
|
||||
@@ -831,6 +836,9 @@ class _PvWorkMapState extends State<PvWorkMap> {
|
||||
],
|
||||
),
|
||||
|
||||
/// 感知目标与禁行区草稿分别显示,始终使用当前地图的设备 SN。
|
||||
MowerObstacleLayer(deviceSn: widget.deviceSn),
|
||||
|
||||
/// 机器指针(同一个 HeadingMarkerPainter:绿圆 + 白描边 + 航向箭头)
|
||||
if (marker != null)
|
||||
MarkerLayer(
|
||||
@@ -911,6 +919,37 @@ class _PvWorkMapState extends State<PvWorkMap> {
|
||||
],
|
||||
),
|
||||
|
||||
if (widget.returnObstacleHoles.isNotEmpty) ...[
|
||||
PolygonLayer(
|
||||
polygons: [
|
||||
for (final hole in widget.returnObstacleHoles)
|
||||
if (hole.length >= 3)
|
||||
Polygon(
|
||||
points: hole,
|
||||
color: Colors.red.withValues(alpha: 0.25),
|
||||
borderColor: Colors.red,
|
||||
borderStrokeWidth: 2.5,
|
||||
isFilled: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
MarkerLayer(
|
||||
markers: [
|
||||
for (final hole in widget.returnObstacleHoles)
|
||||
if (hole.isNotEmpty)
|
||||
Marker(
|
||||
point: LatLng(
|
||||
hole.map((p) => p.latitude).reduce((a, b) => a + b) / hole.length,
|
||||
hole.map((p) => p.longitude).reduce((a, b) => a + b) / hole.length,
|
||||
),
|
||||
width: 28,
|
||||
height: 28,
|
||||
child: const Icon(Icons.block, color: Colors.red, size: 24),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
/// ===== 一键返航点:紫色虚线串联(末点连到准星)=====
|
||||
if (widget.returnPoints.isNotEmpty)
|
||||
PolylineLayer(
|
||||
|
||||
Reference in New Issue
Block a user