完成开始作业的指令和握手搭配确保指令正确接受和发送-最初版本
This commit is contained in:
@@ -20,6 +20,7 @@ import '../../domain/usecases/delete_work_record_usecase.dart';
|
||||
import '../../domain/usecases/get_device_location_usecase.dart';
|
||||
import '../../domain/usecases/get_work_record_usecase.dart';
|
||||
import '../../domain/usecases/route_planning_usecase.dart';
|
||||
import '../../services/path_planning_service.dart';
|
||||
import 'device_status_bloc.dart';
|
||||
import 'device_status_event.dart';
|
||||
import 'devices_state.dart';
|
||||
@@ -39,6 +40,9 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
final RoutePlanningUseCase _routePlanningUseCase; //
|
||||
final DeviceStatusBloc _deviceStatusBloc; // 🔥 新增字段
|
||||
final TcpClient _tcpClient;
|
||||
// 🔥 关键:注入全局服务
|
||||
final PathPlanningService _pathPlanningService;
|
||||
|
||||
DevicesCubit(
|
||||
this.repository,
|
||||
this._getUserDeviceUseCase,
|
||||
@@ -52,9 +56,8 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
this._generatePathUseCase,
|
||||
this._routePlanningUseCase,
|
||||
this._bindDeviceUseCase,
|
||||
this._deviceStatusBloc, this._tcpClient,
|
||||
|
||||
|
||||
this._deviceStatusBloc,
|
||||
this._tcpClient, this._pathPlanningService,
|
||||
) : super(const DevicesState());
|
||||
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
@@ -346,14 +349,21 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
|
||||
// 开始路径规划
|
||||
Future<void> startRoutePlanning(Queue<DeviceAddPathPointModel> locationQueue) async {
|
||||
// 清空全局 Service 中的队列
|
||||
// _routePlanningUseCase.clearLocationQueue();
|
||||
_pathPlanningService.updateQueue(locationQueue as List<DeviceAddPathPointModel>);
|
||||
// 开始路径规划
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _routePlanningUseCase.startRoutePlanning(locationQueue);
|
||||
//传入全局的Service 中路径规划队列
|
||||
Queue<DeviceAddPathPointModel> queue = _pathPlanningService.getQueue() ;
|
||||
final result = await _routePlanningUseCase.startRoutePlanning(queue);
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '路径规划启动失败')),
|
||||
(_) => emit(state.copyWith(isLoading: false, errorMessage: '')),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 暂停
|
||||
Future<void> pauseRoutePlanning() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
75
lib/features/devices/services/path_planning_service.dart
Normal file
75
lib/features/devices/services/path_planning_service.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
// 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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user