完善自主作业下发、暂停恢复和轨迹保留
This commit is contained in:
@@ -7,14 +7,16 @@ class WorkTraceInput {
|
||||
required this.recording,
|
||||
required this.visible,
|
||||
this.completedPoints = const [],
|
||||
this.recordedPoints = const [],
|
||||
});
|
||||
final String? taskId;
|
||||
final bool recording;
|
||||
final bool visible;
|
||||
final List<LatLng> completedPoints;
|
||||
final List<LatLng> recordedPoints;
|
||||
}
|
||||
|
||||
/// 新版作业绿线:首个到达点前累计定位,之后保留到达点并接一段实时车位。
|
||||
/// 新版作业绿线:用到达点恢复历史,用实时定位持续累计本次已走路线。
|
||||
/// 不写任务历史、不下发指令,也不依赖旧版 TracePoint 的可变状态。
|
||||
class WorkTraceRecorder {
|
||||
WorkTraceRecorder({this.maxGpsPoints = 600, this.minimumStepMeters = 0.3})
|
||||
@@ -29,16 +31,12 @@ class WorkTraceRecorder {
|
||||
bool _recording = false;
|
||||
final _gpsPoints = <LatLng>[];
|
||||
List<LatLng> _completed = const [];
|
||||
LatLng? _tail;
|
||||
bool _hasLivePositions = false;
|
||||
|
||||
bool get hasCompletedPoints => _completed.isNotEmpty;
|
||||
List<LatLng> get points {
|
||||
if (!_visible) return const [];
|
||||
if (_completed.isEmpty) return List.unmodifiable(_gpsPoints);
|
||||
return List.unmodifiable([
|
||||
..._completed,
|
||||
if (_tail != null && _tail != _completed.last) _tail!,
|
||||
]);
|
||||
return List.unmodifiable(_gpsPoints.isEmpty ? _completed : _gpsPoints);
|
||||
}
|
||||
|
||||
void synchronize({required String deviceId, required WorkTraceInput input}) {
|
||||
@@ -53,34 +51,39 @@ class WorkTraceRecorder {
|
||||
(input.taskId?.isNotEmpty ?? false);
|
||||
_recording = _visible && input.recording;
|
||||
if (!_visible) {
|
||||
_gpsPoints.clear();
|
||||
_completed = const [];
|
||||
_tail = null;
|
||||
// 同一任务暂时隐藏或状态切换不丢历史;换任务、明确清轨迹时才 reset。
|
||||
return;
|
||||
}
|
||||
if (_gpsPoints.isEmpty && input.recordedPoints.isNotEmpty) {
|
||||
_gpsPoints.addAll(input.recordedPoints.where(_valid));
|
||||
_hasLivePositions = _gpsPoints.isNotEmpty;
|
||||
}
|
||||
final completed = <LatLng>[];
|
||||
for (final point in input.completedPoints) {
|
||||
if (_valid(point) && (completed.isEmpty || completed.last != point)) {
|
||||
completed.add(point);
|
||||
}
|
||||
}
|
||||
if (_completed.isEmpty && completed.isNotEmpty) _gpsPoints.clear();
|
||||
_completed = List.unmodifiable(completed);
|
||||
if (completed.length >= _completed.length) {
|
||||
_completed = List.unmodifiable(completed);
|
||||
// 没有实时定位时用到达点恢复轨迹;收到实时定位后不再用迟到的
|
||||
// 到达点替换本地路线,避免暂停/恢复后旧路线消失或只剩一条尾线。
|
||||
if (!_hasLivePositions && completed.isNotEmpty) {
|
||||
_gpsPoints
|
||||
..clear()
|
||||
..addAll(completed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool addPosition(LatLng point) {
|
||||
if (!_recording || !_valid(point)) return false;
|
||||
if (_completed.isNotEmpty) {
|
||||
if (_tail == point) return false;
|
||||
_tail = point;
|
||||
return true;
|
||||
}
|
||||
if (_gpsPoints.isNotEmpty &&
|
||||
_distance(_gpsPoints.last, point) < minimumStepMeters) {
|
||||
return false;
|
||||
}
|
||||
_gpsPoints.add(point);
|
||||
_tail = point;
|
||||
_hasLivePositions = true;
|
||||
if (_gpsPoints.length > maxGpsPoints) {
|
||||
_gpsPoints.removeRange(0, _gpsPoints.length - maxGpsPoints);
|
||||
}
|
||||
@@ -102,6 +105,6 @@ class WorkTraceRecorder {
|
||||
_recording = false;
|
||||
_gpsPoints.clear();
|
||||
_completed = const [];
|
||||
_tail = null;
|
||||
_hasLivePositions = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ class PvWorkMap extends StatefulWidget {
|
||||
this.recordLocationTrace = true,
|
||||
this.workTrace,
|
||||
this.taskTrace,
|
||||
this.savedRoute = const <LatLng>[],
|
||||
this.onTaskTraceChanged,
|
||||
this.onTaskRouteChanged,
|
||||
this.expectedTaskId,
|
||||
this.traceDiagnosticsEnabled = false,
|
||||
this.showDevice = true,
|
||||
@@ -164,6 +167,9 @@ class PvWorkMap extends StatefulWidget {
|
||||
final bool recordLocationTrace;
|
||||
final List<LatLng>? workTrace;
|
||||
final WorkTraceInput? taskTrace;
|
||||
final List<LatLng> savedRoute;
|
||||
final ValueChanged<List<LatLng>>? onTaskTraceChanged;
|
||||
final ValueChanged<List<LatLng>>? onTaskRouteChanged;
|
||||
final String? expectedTaskId;
|
||||
final bool traceDiagnosticsEnabled;
|
||||
final bool showDevice;
|
||||
@@ -196,7 +202,13 @@ class _PvWorkMapState extends State<PvWorkMap> {
|
||||
return;
|
||||
}
|
||||
_taskTraceRecorder.synchronize(deviceId: widget.deviceSn, input: input);
|
||||
if (_pos != null) _taskTraceRecorder.addPosition(_pos!);
|
||||
if (_route.isEmpty && widget.savedRoute.isNotEmpty && input.visible) {
|
||||
_route = List.of(widget.savedRoute);
|
||||
_routeTaskId = int.tryParse(widget.expectedTaskId ?? '') ?? -1;
|
||||
}
|
||||
if (_pos != null && _taskTraceRecorder.addPosition(_pos!)) {
|
||||
widget.onTaskTraceChanged?.call(_taskTraceRecorder.points);
|
||||
}
|
||||
}
|
||||
|
||||
void _logTraceRender(List<LatLng> points) {
|
||||
@@ -405,6 +417,7 @@ class _PvWorkMapState extends State<PvWorkMap> {
|
||||
}
|
||||
_route = [for (final p in msg.data) LatLng(p.lat, p.lng)];
|
||||
});
|
||||
widget.onTaskRouteChanged?.call(List.of(_route));
|
||||
}
|
||||
|
||||
/// 定位推送来了:攒轨迹 + 按需把机器移到正中间
|
||||
@@ -446,7 +459,9 @@ class _PvWorkMapState extends State<PvWorkMap> {
|
||||
_pos = p;
|
||||
widget.onDevicePosition?.call(p);
|
||||
if (widget.taskTrace != null) {
|
||||
_taskTraceRecorder.addPosition(p);
|
||||
if (_taskTraceRecorder.addPosition(p)) {
|
||||
widget.onTaskTraceChanged?.call(_taskTraceRecorder.points);
|
||||
}
|
||||
} else if (widget.recordLocationTrace) {
|
||||
_appendTrace(p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user