完成开始作业的指令和握手搭配确保指令正确接受和发送-最初版本

This commit is contained in:
2026-03-06 11:24:33 +08:00
parent bfc16447c3
commit 3b7445e850
4 changed files with 203 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import 'package:maibu_satabot_v2/features/devices/domain/usecases/select_work_re
import 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/update_devicename_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
import 'package:maibu_satabot_v2/features/devices/services/path_planning_service.dart';
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_cubit.dart';
import 'package:maibu_satabot_v2/features/my/repository/my_repository.dart';
import 'package:maibu_satabot_v2/features/my/repository/my_repository_impl.dart';
@@ -71,9 +72,14 @@ Future<void> init() async {
sl.registerLazySingleton(
() => TcpClient(sl<UserStorage>(), getUserDeviceUseCase: sl<GetUserDeviceUseCase>(), switchDeviceUseCase: sl<SwitchDeviceUseCase>()),
);
sl.registerLazySingleton(() => PathPlanningService());
/// 1.1.3 NetMessageDispatcher:消息调度器,并将 TcpClient 注入给它
sl.registerLazySingleton(() => NetMessageDispatcher(sl()));
sl.registerLazySingleton(() => NetMessageDispatcher(
sl<TcpClient>(),
sl<RoutePlanningRepository>(),
sl<PathPlanningService>(),
));
/// 1.2 --- 本地存储 (LocalStorage) ---
/// 1.2.1 SharedPreferences:本地存储库
@@ -164,7 +170,7 @@ Future<void> init() async {
/// 5. 状态管理 (Cubit/Bloc)
sl.registerLazySingleton(() => AppUserCubit()); // AuthCubit 依赖它,必须先注册
sl.registerLazySingleton(() => GetDeviceLocationUseCase(sl()));
sl.registerLazySingleton(() => DevicesCubit(sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(),sl(),sl()));
sl.registerLazySingleton(() => DevicesCubit(sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(), sl(),sl(),sl(),sl()));
sl.registerFactory(() => RemoteControlCubit(sl()));
/* // 工厂模式(留存,每次获取新实例)

View File

@@ -1,12 +1,20 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:maibu_satabot_v2/features/devices/data/models/device_add_path_point_model.dart';
import '../../features/devices/domain/repositories/route_planning_repository.dart';
import '../../features/devices/services/path_planning_service.dart';
import 'protocol_decoder.dart';
import 'tcp/tcp_client.dart';
// import '../../features/auth/data/models/user_model.dart';
class NetMessageDispatcher {
final TcpClient tcpClient;
NetMessageDispatcher(this.tcpClient);
final PathPlanningService _pathPlanningService;
final RoutePlanningRepository routePlanningRepository;
NetMessageDispatcher(this.tcpClient, this.routePlanningRepository, this._pathPlanningService);
/// 过滤特定指令的流
@@ -36,6 +44,103 @@ class NetMessageDispatcher {
return onCommand(cmd).map((p) => jsonDecode(utf8.decode(p.payload)));
}
//监听路径规划的开始作业的指令的应答
// if (data[0] == (byte) 0xAB && data[1] == (byte) 0xAA && data[2] == (byte) 0x01) {
// System.out.println("=====下位机回复成功=====" + data[0] + " " + data[1] + " " + data[2] + " " + data[3] + " " +
// data[4] + " " + data[5] + " " + data[6] + " " + data[7] + " " + data[8] + " " + data[9]);
// if (data[5] == (byte) 0x01) {
// Point point = new Point();
// //发送下一个指令
// }
/// 监听路径规划的开始作业指令的应答 (0x01 指令)
/// 协议格式:AB AA 01 [DATA] CRC(2) AA AB
/// 验证头部:data[0]=0xAB, data[1]=0xAA, data[2]=0x01
/// 检查状态位:data[5] == 0x01 表示回复成功
Stream<RawPacket> onPathPlanningResponse() {
debugPrint('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x01)');
return onCommand(0x01).where((packet) {
// 构造完整数据包用于验证
final fullData = <int>[
0xAB, 0xAA, 0x01,
...packet.payload,
0x00, 0x00, // CRC 占位
0xAA, 0xAB
];
// 验证头部协议
if (fullData[0] == 0xAB &&
fullData[1] == 0xAA &&
fullData[2] == 0x01) {
debugPrint('[Dispatcher] 下位机回复成功 - 完整数据包:${fullData.join(" ")}');
// 检查状态位 (索引 5 对应 payload 的第 2 个字节)
if (fullData.length > 5 && fullData[5] == 0x01) {
debugPrint('✅ [Dispatcher] 状态位验证通过:0x01 - 可以发送下一个指令');
///取全局的路径规划发送实体 queue
// if (isStart && locationQueue.isEmpty) {
// isStart = false;
// print("[track]");
// tcpClient.disconnect();
// return;
// }
//
// final entity = locationQueue.removeFirst(); // 类型:DeviceAddPathPointModel
// final routePlanSendEntity = RoutePlanSendEntity(
// commandType: 0x01,
// pointCounts: 1,
// targetLatitude: entity.latitude,
// targetLongitude: entity.longitude,
// speed: 1000,
// );
/// 发送下一个指令 移除上一条数据
var queue = _pathPlanningService.getQueue();
//去除第一条数据
queue.removeFirst();
_pathPlanningService.updateQueue(queue as List<DeviceAddPathPointModel>);
routePlanningRepository.startRoutePlanning(queue);
return true;
} else {
debugPrint('⚠️ [Dispatcher] 状态位异常:${fullData.length > 5 ? fullData[5].toRadixString(16) : "N/A"}');
}
} else {
debugPrint('❌ [Dispatcher] 协议头验证失败');
}
return false;
});
}
/// 监听特定状态的回复(可复用)
/// [commandCode] 指令码
/// [statusIndex] 状态字节在 payload 中的索引
/// [expectedStatus] 期望的状态值
Stream<RawPacket> onStatusResponse({
required int commandCode,
required int statusIndex,
required int expectedStatus,
}) {
debugPrint('🔍 [Dispatcher] 监听指令应答 CMD: 0x${commandCode.toRadixString(16)}, 期望状态:0x${expectedStatus.toRadixString(16)}');
return onCommand(commandCode).where((packet) {
if (packet.payload.length > statusIndex) {
final status = packet.payload[statusIndex];
if (status == expectedStatus) {
debugPrint('✅ [Dispatcher] 状态验证通过 - CMD: 0x${commandCode.toRadixString(16)}, 状态:0x${status.toRadixString(16)}');
return true;
} else {
debugPrint('⚠️ [Dispatcher] 状态不匹配 - 期望:0x${expectedStatus.toRadixString(16)}, 实际:0x${status.toRadixString(16)}');
}
} else {
debugPrint('❌ [Dispatcher] Payload 长度不足 - 索引:$statusIndex, 实际长度:${packet.payload.length}');
}
return false;
});
}
}