Files
flutterApp/lib/features/devices/services/path_planning_service.dart
Songzex b6e4189573 完成路及规划的作业的开始工作成功.
完成路径规划的暂停功能的对接
完成路径规划的继续功能的对接
完成路径规划的停止功能的对接
2026-03-11 10:01:36 +08:00

77 lines
2.2 KiB
Dart

// lib/features/devices/domain/services/path_planning_service.dart
import 'dart:collection';
import 'package:flutter/cupertino.dart';
import '../data/models/device_add_path_point_model.dart';
class PathPlanningService {
static final PathPlanningService _instance = PathPlanningService._internal();
factory PathPlanningService() => _instance;
PathPlanningService._internal();
// 🔥 核心:全局唯一的可变队列
final Queue<DeviceAddPathPointModel> locationQueue = Queue();
bool isRunning = false;
int currentIndex = 0;
int totalCount = 0;
/// 🔥 存:生成路径后立即存储
void updateQueue(List<DeviceAddPathPointModel> locations) {
debugPrint('进入[PathPlanningService]updateQueue方法尝试更新空队列');
if (locations.isEmpty) {
debugPrint('⚠️ [PathPlanningService] 尝试更新空队列');
return;
}
locationQueue.clear();
locationQueue.addAll(locations);
currentIndex = 0;
totalCount = locations.length;
debugPrint('✅ [PathPlanningService] 队列已更新,共 $totalCount 个点');
}
/// 🔥 取:发送时取出第一个点
DeviceAddPathPointModel? removeFirst() {
if (locationQueue.isEmpty) {
debugPrint('⚠️ [PathPlanningService] 队列为空');
return null;
}
final point = locationQueue.removeFirst();
currentIndex++;
debugPrint(
'📍 [PathPlanningService] 取出第 $currentIndex/$totalCount 个点:'
'${point.latitude}, ${point.longitude}',
);
return point;
}
/// 🔥 获取进度
int get remainingCount => locationQueue.length;
int get completedCount => currentIndex;
double get progress => totalCount == 0 ? 0.0 : currentIndex / totalCount;
bool get isEmpty => locationQueue.isEmpty;
bool get isNotEmpty => locationQueue.isNotEmpty;
/// 🔥 清空(作业完成或停止时)
void clear() {
locationQueue.clear();
isRunning = false;
currentIndex = 0;
totalCount = 0;
debugPrint('🧹 [PathPlanningService] 队列已清空');
}
int get length => locationQueue.length;
/// 获取当前队列(供 Cubit 使用)
Queue<DeviceAddPathPointModel> getQueue() {
return Queue.of(locationQueue);
}
}