终于解决了 第二次作业之后的 路径不清除的问题 ,一直保留着上一次作业发送的完成点
This commit is contained in:
@@ -23,13 +23,11 @@ class NetMessageDispatcher {
|
||||
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
|
||||
// 🔥 新增:ACK 握手状态管理 (仅新模式使用)
|
||||
int _expectedPointIndex = -1; // 期望收到的点编号
|
||||
bool _waitingForAck = false; // 是否正在等待 ACK 确认
|
||||
bool _waitingForArrived = false; // 是否正在等待到达确认
|
||||
int _currentPointIndex = -1; // 当前发送的点索引
|
||||
|
||||
int _expectedPointIndex = -1; // 期望收到的点编号
|
||||
bool _waitingForAck = false; // 是否正在等待 ACK 确认
|
||||
bool _waitingForArrived = false; // 是否正在等待到达确认
|
||||
int _currentPointIndex = -1; // 当前发送的点索引
|
||||
|
||||
// 🔥 新增:设置期望收到的点编号(供 PathPlanner 调用)
|
||||
void setExpectedPointIndex(int index) {
|
||||
@@ -38,7 +36,6 @@ class NetMessageDispatcher {
|
||||
_logger.log('[Dispatcher-ACK] 设置期望点编号:$index');
|
||||
}
|
||||
|
||||
|
||||
NetMessageDispatcher(this.tcpClient, this.routePlanningRepository, this._pathPlanningService, {this.getAppState});
|
||||
|
||||
/// 过滤特定指令的流
|
||||
@@ -58,12 +55,12 @@ class NetMessageDispatcher {
|
||||
_logger.log('✅ 解码 0x02 成功:$result');
|
||||
return result;
|
||||
} catch (e) {
|
||||
// print('❌ 解码 0x02 失败:$e, 原始字节:${p.payload}');
|
||||
// print('❌ 解码 0x02 失败:$e, 原始字节:${p.payload}');
|
||||
_logger.log('❌ 解码 0x02 失败:$e, 原始字节:${p.payload}');
|
||||
return ''; // 返回空字符串,避免流中断
|
||||
}
|
||||
});
|
||||
/* ///return onCommand(0x02).map((p) => utf8.decode(p.payload));*/
|
||||
/* ///return onCommand(0x02).map((p) => utf8.decode(p.payload));*/
|
||||
}
|
||||
|
||||
/// 示例解析方法:将 0x12 指令解析为 JSON 并转为模型
|
||||
@@ -72,15 +69,14 @@ class NetMessageDispatcher {
|
||||
// }
|
||||
Stream<Map<String, dynamic>> onJsonMessage(int cmd) {
|
||||
//print('🔵 [Dispatcher] 开始监听 0x${cmd.toRadixString(16)} 指令的 JSON 流');
|
||||
_logger.log('🔵 [Dispatcher] 监听 0x${cmd.toRadixString(16)} 指令的 JSON 流');
|
||||
_logger.log('🔵 [Dispatcher] 监听 0x${cmd.toRadixString(16)} 指令的 JSON 流');
|
||||
return onCommand(cmd).map((p) {
|
||||
try {
|
||||
final jsonString = utf8.decode(p.payload);
|
||||
//print('🔵 [Dispatcher] 收到 0x${cmd.toRadixString(16)} 原始数据: $jsonString');
|
||||
_logger
|
||||
.log('🔵 [Dispatcher] 监听 0x${cmd.toRadixString(16)} 指令的 JSON 流');
|
||||
_logger.log('🔵 [Dispatcher] 监听 0x${cmd.toRadixString(16)} 指令的 JSON 流');
|
||||
final jsonMap = jsonDecode(jsonString);
|
||||
// print('🔵 [Dispatcher] JSON 解析成功:$jsonMap');
|
||||
// print('🔵 [Dispatcher] JSON 解析成功:$jsonMap');
|
||||
_logger.log('🔵 [Dispatcher] JSON 解析成功:$jsonMap');
|
||||
|
||||
return jsonMap;
|
||||
@@ -94,19 +90,19 @@ _logger
|
||||
|
||||
//监听路径规划的开始作业的指令的应答
|
||||
|
||||
// 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();
|
||||
// //发送下一个指令
|
||||
// }
|
||||
// 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() {
|
||||
/* Stream<RawPacket> onPathPlanningResponse() {
|
||||
final devicesCubit = sl<DevicesCubit>();
|
||||
|
||||
debugPrint('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x01)');
|
||||
@@ -196,7 +192,7 @@ _logger
|
||||
return false;
|
||||
});
|
||||
}*/
|
||||
///新版的ack和现有的融合
|
||||
///新版的ack和现有的融合
|
||||
/// 监听路径规划指令应答 (现有逻辑 - 不修改)
|
||||
/// 协议格式:AB AA 01 [DATA] CRC(2) AA AB
|
||||
/// 验证头部:data[0]=0xAB, data[1]=0xAA, data[2]=0x01
|
||||
@@ -205,7 +201,7 @@ _logger
|
||||
final devicesCubit = sl<DevicesCubit>();
|
||||
|
||||
//debugPrint('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x01) - 模式:${PathPlanningConfig.getModeDescription()}');
|
||||
_logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x01) - 模式:${PathPlanningConfig.getModeDescription()}');
|
||||
_logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x01) - 模式:${PathPlanningConfig.getModeDescription()}');
|
||||
// 🔥 根据配置决定使用哪个流
|
||||
if (PathPlanningConfig.useAckHandshake) {
|
||||
//debugPrint('⚙️ [Dispatcher] 使用 ACK 握手机制');
|
||||
@@ -219,7 +215,7 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
final currentState = getAppState!();
|
||||
if (currentState.toString() == 'AppState.none') {
|
||||
//debugPrint('⚠️ [Dispatcher] 当前 AppState 为 none,停止处理路径规划指令');
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 当前 AppState 为 none,停止处理路径规划指令');
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 当前 AppState 为 none,停止处理路径规划指令');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -228,26 +224,24 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
0xAB, 0xAA, 0x01,
|
||||
...packet.payload,
|
||||
0x00, 0x00, // CRC 占位
|
||||
0xAA, 0xAB
|
||||
0xAA, 0xAB,
|
||||
];
|
||||
|
||||
// 验证头部协议
|
||||
if (fullData[0] == 0xAB &&
|
||||
fullData[1] == 0xAA &&
|
||||
fullData[2] == 0x01) {
|
||||
|
||||
if(fullData[5] == 0x02){
|
||||
// debugPrint('下位机回复收到指令 - 完整数据包:${fullData.join(" ")}');
|
||||
if (fullData[0] == 0xAB && fullData[1] == 0xAA && fullData[2] == 0x01) {
|
||||
if (fullData[5] == 0x02) {
|
||||
// debugPrint('下位机回复收到指令 - 完整数据包:${fullData.join(" ")}');
|
||||
_logger.logWithLevel('下位机回复收到指令 - 完整数据包:${fullData.join(" ")}');
|
||||
}
|
||||
debugPrint('[Dispatcher] 下位机回复成功 - 完整数据包:${fullData.join(" ")}');
|
||||
// 检查状态位 (索引 5 对应 payload 的第 2 个字节)
|
||||
if (fullData.length > 5 && fullData[5] == 0x01) {
|
||||
// debugPrint('✅ [Dispatcher] 状态位验证通过:0x01 - 可以发送下一个指令');
|
||||
// debugPrint('✅ [Dispatcher] 状态位验证通过:0x01 - 可以发送下一个指令');
|
||||
_logger.logWithLevel('✅ [Dispatcher] 状态位验证通过:0x01 - 可以发送下一个指令');
|
||||
|
||||
///取全局的路径规划发送实体 queue
|
||||
/// 发送下一个指令 移除上一条数据
|
||||
var queue = _pathPlanningService.getQueue();
|
||||
var queue = _pathPlanningService.getQueue();
|
||||
// 🔥 关键:必须先检查空队列!
|
||||
if (queue.isEmpty) {
|
||||
devicesCubit.finishWork();
|
||||
@@ -260,31 +254,33 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 队列已经为空,跳过本次响应处理');
|
||||
return false;
|
||||
}
|
||||
|
||||
///更新完成 jwd
|
||||
var deviceAddPathPointModel = queue.first;
|
||||
var d = deviceAddPathPointModel.latitude;
|
||||
var e = deviceAddPathPointModel.longitude;
|
||||
devicesCubit.setArrivedLocation(
|
||||
deviceAddPathPointModel.latitude,
|
||||
deviceAddPathPointModel.longitude
|
||||
);
|
||||
// debugPrint("准确更新完成的路径点经纬度,$d,$e");
|
||||
devicesCubit.setArrivedLocation(deviceAddPathPointModel.latitude, deviceAddPathPointModel.longitude);
|
||||
// debugPrint("准确更新完成的路径点经纬度,$d,$e");
|
||||
_logger.logWithLevel("准确更新完成路径点经纬度,$d,$e");
|
||||
//去除上一条数据
|
||||
queue.removeFirst();
|
||||
|
||||
/// _pathPlanningService.updateQueue(queue as List<DeviceAddPathPointModel>);
|
||||
_pathPlanningService.updateQueue(queue.toList());
|
||||
DateTime now = DateTime.now();
|
||||
String timeStr = "${now.hour.toString().padLeft(2, '0')}:"
|
||||
String timeStr =
|
||||
"${now.hour.toString().padLeft(2, '0')}:"
|
||||
"${now.minute.toString().padLeft(2, '0')}:"
|
||||
"${now.second.toString().padLeft(2, '0')}";
|
||||
|
||||
//debugPrint(' $timeStr [Dispatcher] 准备触发下一个路径点发送,队列剩余:${queue.length}');
|
||||
_logger.logWithLevel(' $timeStr [Dispatcher] 触发下一个路径点发送,队列剩余:${queue.length}');
|
||||
|
||||
///如果还剩 0 个数据 就停止发送
|
||||
if (queue.length==0) {
|
||||
if (queue.length == 0) {
|
||||
//debugPrint('⚠️ [Dispatcher] 队列已空,停止发送');
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 队列已空,停止发送');
|
||||
devicesCubit.setArrivedLocation(0.0, 0.0);
|
||||
|
||||
// final devicesCubit = sl<DevicesCubit>();
|
||||
devicesCubit.finishWork();
|
||||
@@ -293,6 +289,7 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
});
|
||||
// 手动停止
|
||||
routePlanningRepository.stopRoutePlanning();
|
||||
|
||||
//debugPrint('⚠️ [Dispatcher] 队列已空,发送停止指令已发送');
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 状态位异常:${fullData.length > 5 ? fullData[5].toRadixString(16) : "N/A"}');
|
||||
return false;
|
||||
@@ -304,7 +301,7 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
_logger.logWithLevel('⚠️ [Dispatcher] 状态位异常:${fullData.length > 5 ? fullData[5].toRadixString(16) : "N/A"}');
|
||||
}
|
||||
} else {
|
||||
// debugPrint('❌ [Dispatcher] 协议头验证失败');
|
||||
// debugPrint('❌ [Dispatcher] 协议头验证失败');
|
||||
_logger.logWithLevel('❌ [Dispatcher] 协议头验证失败');
|
||||
}
|
||||
|
||||
@@ -313,14 +310,11 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// 监听特定状态的回复(可复用)
|
||||
/// [commandCode] 指令码
|
||||
/// [statusIndex] 状态字节在 payload 中的索引
|
||||
/// [expectedStatus] 期望的状态值
|
||||
/* Stream<RawPacket> onStatusResponse({
|
||||
/* Stream<RawPacket> onStatusResponse({
|
||||
required int commandCode,
|
||||
required int statusIndex,
|
||||
required int expectedStatus,
|
||||
@@ -343,8 +337,8 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
});
|
||||
}*/
|
||||
|
||||
/// 监听特定状态的回复(ack握手机制)
|
||||
// 🔥 新增:ACK 握手机制 (新模式)
|
||||
/// 监听特定状态的回复(ack握手机制)
|
||||
// 🔥 新增:ACK 握手机制 (新模式)
|
||||
/// 监听路径规划指令应答,实现完整的 ACK 握手机制
|
||||
/// 协议格式:AB AA 01 [DATA] CRC(2) AA AB
|
||||
/// 上传协议(0x01 路径规划上传):
|
||||
@@ -373,14 +367,11 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
0xAB, 0xAA, 0x01,
|
||||
...packet.payload,
|
||||
0x00, 0x00, // CRC 占位
|
||||
0xAA, 0xAB
|
||||
0xAA, 0xAB,
|
||||
];
|
||||
|
||||
// 验证头部协议
|
||||
if (fullData[0] == 0xAB &&
|
||||
fullData[1] == 0xAA &&
|
||||
fullData[2] == 0x01) {
|
||||
|
||||
if (fullData[0] == 0xAB && fullData[1] == 0xAA && fullData[2] == 0x01) {
|
||||
debugPrint('[Dispatcher-ACK] 收到路径规划应答 - 完整数据包:${fullData.join(" ")}');
|
||||
|
||||
// 🔥 检查数据包长度是否足够
|
||||
@@ -406,10 +397,9 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
// ✅ 下位机回复"收到指令"
|
||||
debugPrint('✅ [Dispatcher-ACK] ACK 确认收到 - 点编号:$pointIndex');
|
||||
_waitingForAck = false;
|
||||
_waitingForArrived = true; // 开始等待"已到达"信号
|
||||
return false; // 继续监听,不返回给流
|
||||
}
|
||||
else if (status == 0x01) {
|
||||
_waitingForArrived = true; // 开始等待"已到达"信号
|
||||
return false; // 继续监听,不返回给流
|
||||
} else if (status == 0x01) {
|
||||
// ✅ 下位机回复"已到达"
|
||||
debugPrint('✅ [Dispatcher-ACK] 已到达确认 - 点编号:$pointIndex');
|
||||
_waitingForArrived = false;
|
||||
@@ -430,10 +420,7 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
var deviceAddPathPointModel = queue.first;
|
||||
var d = deviceAddPathPointModel.latitude;
|
||||
var e = deviceAddPathPointModel.longitude;
|
||||
devicesCubit.setArrivedLocation(
|
||||
deviceAddPathPointModel.latitude,
|
||||
deviceAddPathPointModel.longitude
|
||||
);
|
||||
devicesCubit.setArrivedLocation(deviceAddPathPointModel.latitude, deviceAddPathPointModel.longitude);
|
||||
debugPrint(" 准确更新完成的路径点经纬度:$d, $e");
|
||||
|
||||
// 移除已完成的点
|
||||
@@ -441,7 +428,8 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
_pathPlanningService.updateQueue(queue.toList());
|
||||
|
||||
DateTime now = DateTime.now();
|
||||
String timeStr = "${now.hour.toString().padLeft(2, '0')}:"
|
||||
String timeStr =
|
||||
"${now.hour.toString().padLeft(2, '0')}:"
|
||||
"${now.minute.toString().padLeft(2, '0')}:"
|
||||
"${now.second.toString().padLeft(2, '0')}";
|
||||
|
||||
@@ -461,9 +449,8 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
|
||||
// 🔥 发送下一个点(触发 ACK 握手循环)
|
||||
routePlanningRepository.startRoutePlanning(queue);
|
||||
return false; // 不返回给流,避免重复处理
|
||||
}
|
||||
else {
|
||||
return false; // 不返回给流,避免重复处理
|
||||
} else {
|
||||
debugPrint('⚠️ [Dispatcher-ACK] 未知状态位:0x${status.toRadixString(16)}');
|
||||
return false;
|
||||
}
|
||||
@@ -473,6 +460,4 @@ _logger.logWithLevel('[Dispatcher] 开始监听路径规划指令应答 (CMD: 0x
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user