Files
flutterApp/lib/features/home/presentation/widgets/common/tracepoint.dart

312 lines
7.1 KiB
Dart
Raw Normal View History

2026-02-26 14:08:02 +08:00
import 'dart:convert';
import 'dart:async';
// ======================== 枚举定义 ========================
/// 轨迹模式枚举
enum TPMode {
NAVIGATION, // 导航模式:绘制规划已完成路径和当前路径
2026-04-15 16:38:54 +08:00
LOCATION, // 定位模式:仅绘制当前点(永远只有1个)
2026-02-26 14:08:02 +08:00
TRACK, // 轨迹模式:实时绘制历史轨迹
}
/// 导航模式下的操作动作枚举
enum TPAction {
UPDATE, // 更新当前点
ADD, // 添加完成点
}
// ======================== 尝试锁实现 ========================
class TryLock {
bool _locked = false;
bool tryLock() {
if (_locked) return false;
_locked = true;
return true;
}
void release() {
_locked = false;
}
bool get isLocked => _locked;
}
// ======================== 环形队列实现 ========================
class CircQueue<T> {
2026-04-15 16:38:54 +08:00
final int capacity;
final List<T?> buffer;
int head = 0;
int tail = 0;
bool isFull = false;
final bool deepCopy;
final int maxMemoryBytes;
int currentMemoryBytes = 0;
CircQueue(this.capacity, {this.deepCopy = true, this.maxMemoryBytes = 1024 * 1024}) : buffer = List<T?>.filled(capacity, null);
2026-02-26 14:08:02 +08:00
void clear() {
head = tail;
isFull = false;
currentMemoryBytes = 0;
}
bool isEmpty() {
return head == tail && !isFull;
}
bool isFullFn() {
return isFull;
}
int _estimateSize(T item) {
try {
return utf8.encode(jsonEncode(item)).length;
} catch (e) {
return 0;
}
}
T? _deepClone(T? item) {
if (!deepCopy || item == null) return item;
try {
return jsonDecode(jsonEncode(item)) as T;
} catch (e) {
return item;
}
}
bool enter(T item) {
final itemSize = _estimateSize(item);
if (isFullFn() || (currentMemoryBytes + itemSize > maxMemoryBytes)) {
return false;
}
final clone = _deepClone(item);
buffer[tail] = clone;
tail = (tail + 1) % capacity;
currentMemoryBytes += itemSize;
if (tail == head) isFull = true;
return true;
}
T? out() {
if (isEmpty()) return null;
final item = buffer[head];
if (item != null) {
currentMemoryBytes -= _estimateSize(item);
}
head = (head + 1) % capacity;
isFull = false;
return _deepClone(item);
}
bool discard(int len) {
for (int i = 0; i < len; i++) {
if (isEmpty()) return false;
final index = (tail - 1 + capacity) % capacity;
final item = buffer[index];
if (item != null) {
currentMemoryBytes -= _estimateSize(item);
}
tail = index;
isFull = false;
}
return true;
}
int getDepth() {
return isFull ? capacity : (tail + capacity - head) % capacity;
}
Future<bool> send(T item, {int timeoutMs = 0}) async {
final start = DateTime.now().millisecondsSinceEpoch;
while (!enter(item)) {
2026-03-13 12:37:59 +08:00
if (timeoutMs > 0 && DateTime.now().millisecondsSinceEpoch - start >= timeoutMs) {
2026-02-26 14:08:02 +08:00
return false;
}
await Future.delayed(const Duration(milliseconds: 1));
}
return true;
}
Future<T?> recv({int timeoutMs = 0}) async {
final start = DateTime.now().millisecondsSinceEpoch;
while (true) {
final item = out();
if (item != null) return item;
2026-03-13 12:37:59 +08:00
if (timeoutMs > 0 && DateTime.now().millisecondsSinceEpoch - start >= timeoutMs) {
2026-02-26 14:08:02 +08:00
return null;
}
await Future.delayed(const Duration(milliseconds: 1));
}
}
List<T?> toList() {
final result = <T?>[];
int i = head;
int count = getDepth();
while (count-- > 0) {
result.add(_deepClone(buffer[i]));
i = (i + 1) % capacity;
}
return result;
}
}
class TracePoint<T> {
late final CircQueue<T> _queue;
List<T> _tracePoint = [];
int _completePointIndex = 0;
late final TryLock _lock;
TPMode _mode = TPMode.LOCATION;
void Function(T)? onCurrentPointUpdated;
void Function(T)? onCompletePointAdded;
bool tryLock() => _lock.tryLock();
void release() => _lock.release();
TracePoint({int queueCapacity = 5, int maxMemoryBytes = 1024 * 1024}) {
2026-03-13 12:37:59 +08:00
_queue = CircQueue<T>(queueCapacity, deepCopy: true, maxMemoryBytes: maxMemoryBytes);
2026-02-26 14:08:02 +08:00
_lock = TryLock();
reset();
}
/// 复位所有状态
void reset() {
_queue.clear();
2026-04-15 16:38:54 +08:00
_tracePoint.clear();
2026-02-26 14:08:02 +08:00
_completePointIndex = 0;
_lock.release();
}
2026-04-15 16:38:54 +08:00
/// 设置模式
2026-02-26 14:08:02 +08:00
void setMode(TPMode mode) {
reset();
_mode = mode;
}
2026-03-14 20:05:42 +08:00
TPMode getMode() {
return _mode;
}
2026-02-26 14:08:02 +08:00
/// 添加/更新轨迹点
void upsert(T point, [TPAction act = TPAction.UPDATE]) {
2026-04-15 16:38:54 +08:00
// 严格按模式分发
2026-02-26 14:08:02 +08:00
switch (_mode) {
case TPMode.NAVIGATION:
2026-04-15 16:38:54 +08:00
if (act == TPAction.UPDATE) {
_updateCurrentPoint(point);
} else {
_addCompletePoint(point);
}
2026-02-26 14:08:02 +08:00
break;
2026-04-15 16:38:54 +08:00
2026-02-26 14:08:02 +08:00
case TPMode.LOCATION:
2026-04-15 16:38:54 +08:00
// LOCATION 永远只保留最新一个点
2026-02-26 14:08:02 +08:00
_updateCurrentPoint(point);
break;
2026-04-15 16:38:54 +08:00
2026-02-26 14:08:02 +08:00
case TPMode.TRACK:
2026-04-15 16:38:54 +08:00
// TRACK 每个点都直接添加,不进队列
2026-02-26 14:08:02 +08:00
_addCompletePoint(point);
break;
}
}
2026-04-15 16:38:54 +08:00
/// 获取轨迹列表
2026-02-26 14:08:02 +08:00
List<T>? getTracePoint() {
if (_lock.tryLock()) {
final trace = List<T>.from(_tracePoint);
_lock.release();
return trace;
} else {
print('getTracePoint: lock!!!');
return null;
}
}
2026-04-15 16:38:54 +08:00
/// 更新当前点(LOCATION 永远只有1个)
2026-02-26 14:08:02 +08:00
void _updateCurrentPoint(T point) {
if (_lock.tryLock()) {
try {
switch (_mode) {
case TPMode.NAVIGATION:
2026-04-15 16:38:54 +08:00
if (_tracePoint.isNotEmpty) {
2026-02-26 14:08:02 +08:00
_tracePoint = _tracePoint.sublist(0, _completePointIndex);
_tracePoint.add(point);
onCurrentPointUpdated?.call(point);
}
break;
2026-04-15 16:38:54 +08:00
2026-02-26 14:08:02 +08:00
case TPMode.LOCATION:
2026-04-15 16:38:54 +08:00
// 核心:永远清空,只保留最新一个
_tracePoint.clear();
_tracePoint.add(point);
2026-02-26 14:08:02 +08:00
onCurrentPointUpdated?.call(point);
break;
2026-04-15 16:38:54 +08:00
2026-02-26 14:08:02 +08:00
default:
break;
}
} finally {
_lock.release();
}
}
}
2026-04-15 16:38:54 +08:00
/// 添加完成点
2026-02-26 14:08:02 +08:00
void _addCompletePoint(T point) {
if (_lock.tryLock()) {
try {
2026-04-15 16:38:54 +08:00
// 先消费队列
2026-02-26 14:08:02 +08:00
while (!_queue.isEmpty()) {
final queuedPoint = _queue.out();
if (queuedPoint != null) {
_addPoint(queuedPoint);
}
}
2026-04-15 16:38:54 +08:00
// 添加当前点
2026-02-26 14:08:02 +08:00
_addPoint(point);
onCompletePointAdded?.call(point);
} catch (e) {
print('addCompletePoint failure: $e');
} finally {
_lock.release();
}
} else {
2026-04-15 16:38:54 +08:00
if (_mode != TPMode.TRACK) {
// TRACK 模式不加队列,直接丢弃旧的保证流畅
_queue.enter(point);
}
print('addCompletePoint: lock!!!');
2026-02-26 14:08:02 +08:00
}
}
2026-04-15 16:38:54 +08:00
/// 内部添加点
2026-02-26 14:08:02 +08:00
void _addPoint(T point) {
2026-04-15 16:38:54 +08:00
if (_mode == TPMode.LOCATION) {
// LOCATION 强制只保留1个
_tracePoint.clear();
_tracePoint.add(point);
_completePointIndex = 1;
2026-02-26 14:08:02 +08:00
return;
}
2026-04-15 16:38:54 +08:00
final hasCurrent = _tracePoint.isNotEmpty && _completePointIndex < _tracePoint.length;
2026-02-26 14:08:02 +08:00
2026-04-15 16:38:54 +08:00
if (hasCurrent) {
2026-02-26 14:08:02 +08:00
_tracePoint = _tracePoint.sublist(0, _completePointIndex);
2026-04-15 16:38:54 +08:00
_tracePoint.add(point);
2026-02-26 14:08:02 +08:00
} else {
_tracePoint.add(point);
}
_completePointIndex++;
}
}