自主任务接口接入和bug修复和工作记录页面
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
/// 作业页面决定开始/暂停/结束,地图只消费这个只读输入。
|
||||
class WorkTraceInput {
|
||||
const WorkTraceInput({
|
||||
required this.taskId,
|
||||
required this.recording,
|
||||
required this.visible,
|
||||
this.completedPoints = const [],
|
||||
});
|
||||
final String? taskId;
|
||||
final bool recording;
|
||||
final bool visible;
|
||||
final List<LatLng> completedPoints;
|
||||
}
|
||||
|
||||
/// 新版作业绿线:首个到达点前累计定位,之后保留到达点并接一段实时车位。
|
||||
/// 不写任务历史、不下发指令,也不依赖旧版 TracePoint 的可变状态。
|
||||
class WorkTraceRecorder {
|
||||
WorkTraceRecorder({this.maxGpsPoints = 600, this.minimumStepMeters = 0.3})
|
||||
: assert(maxGpsPoints >= 2),
|
||||
assert(minimumStepMeters >= 0);
|
||||
final int maxGpsPoints;
|
||||
final double minimumStepMeters;
|
||||
final _distance = const Distance(roundResult: false);
|
||||
String? _deviceId;
|
||||
String? _taskId;
|
||||
bool _visible = false;
|
||||
bool _recording = false;
|
||||
final _gpsPoints = <LatLng>[];
|
||||
List<LatLng> _completed = const [];
|
||||
LatLng? _tail;
|
||||
|
||||
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!,
|
||||
]);
|
||||
}
|
||||
|
||||
void synchronize({required String deviceId, required WorkTraceInput input}) {
|
||||
if (_deviceId != deviceId || _taskId != input.taskId) {
|
||||
reset();
|
||||
_deviceId = deviceId;
|
||||
_taskId = input.taskId;
|
||||
}
|
||||
_visible =
|
||||
input.visible &&
|
||||
deviceId.isNotEmpty &&
|
||||
(input.taskId?.isNotEmpty ?? false);
|
||||
_recording = _visible && input.recording;
|
||||
if (!_visible) {
|
||||
_gpsPoints.clear();
|
||||
_completed = const [];
|
||||
_tail = null;
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
if (_gpsPoints.length > maxGpsPoints) {
|
||||
_gpsPoints.removeRange(0, _gpsPoints.length - maxGpsPoints);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool _valid(LatLng point) =>
|
||||
point.latitude.isFinite &&
|
||||
point.longitude.isFinite &&
|
||||
point.latitude != 0 &&
|
||||
point.longitude != 0 &&
|
||||
point.latitude.abs() <= 90 &&
|
||||
point.longitude.abs() <= 180;
|
||||
|
||||
void reset() {
|
||||
_deviceId = null;
|
||||
_taskId = null;
|
||||
_visible = false;
|
||||
_recording = false;
|
||||
_gpsPoints.clear();
|
||||
_completed = const [];
|
||||
_tail = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user