编辑 application.yml:
mqtt:
broker: tcp://192.168.0.30:1883
username: your_username
password: your_password
clientId: server_client_001
deviceIds: ld000001,ld000002 # 你的设备ID列表
subscribeTopics: # 留空使用默认订阅
qos: 1
启动Spring Boot应用后,系统会自动:
访问: http://localhost:8080/swagger-ui.html
找到"MQTT消息管理"分组,可以看到所有可用的API接口。
curl -X POST "http://localhost:8080/mqtt/ld/map/list?deviceId=ld000001"
curl -X POST "http://localhost:8080/mqtt/ld/navigation/start?deviceId=ld000001&mapName=map_a"
curl -X POST "http://localhost:8080/mqtt/ld/task/gotoByNid?deviceId=ld000001&mapName=map_a&nid=19"
启动后查看日志,应该能看到:
MQTT消息监听器初始化完成
接收MQTT消息 -> 主题:/robot4inspection/ld000001/localization/pose,QoS:0
处理LD导航系统消息 -> 设备ID:ld000001,短主题:/localization/pose
# 1. 请求地图列表
POST /mqtt/ld/map/list?deviceId=ld000001
# 2. 开启导航(使用某个地图)
POST /mqtt/ld/navigation/start?deviceId=ld000001&mapName=map_a
# 3. 位姿初始化(如果需要)
POST /mqtt/ld/localization/initByNid?deviceId=ld000001&nid=1
# 4. 前往目标点
POST /mqtt/ld/task/gotoByNid?deviceId=ld000001&mapName=map_a&nid=19
# 5. 监听到达事件(自动接收)
# 系统会自动接收 /task/target/event/arrive 消息
# 6. 如需暂停
POST /mqtt/ld/task/pause?deviceId=ld000001
# 7. 继续任务
POST /mqtt/ld/task/resume?deviceId=ld000001
# 8. 取消任务
POST /mqtt/ld/task/cancel?deviceId=ld000001
@Autowired
private MqttSendService mqttSendService;
@Autowired
private RedisCache redisCache;
// 开启导航
boolean success = mqttSendService.startNavigation("ld000001", "map_a");
// 前往目标点
mqttSendService.gotoTargetByNid("ld000001", "map_a", 19);
// 暂停任务
mqttSendService.pauseTask("ld000001");
// 从Redis获取实时位姿
RobotPoseInfo poseInfo = redisCache.getCacheObject("ld:pose:ld000001");
if (poseInfo != null) {
List<Double> xyz = poseInfo.getPose().getXyz();
Integer confidence = poseInfo.getConfidence();
Integer runState = poseInfo.getRunState();
}
// 从Redis获取任务实时信息
TaskRealtimeInfo taskInfo = redisCache.getCacheObject("ld:task:realtime:ld000001");
if (taskInfo != null) {
String driveMode = taskInfo.getDriveMode();
Double remainDistance = taskInfo.getOdom().getRemain();
}
编辑 MqttMessageListener.java,在对应的处理方法中添加业务逻辑:
private void handleArriveEvent(String deviceId, String payload) {
try {
JSONObject json = JSON.parseObject(payload);
JSONArray argsArray = json.getJSONArray("args");
if (argsArray != null && !argsArray.isEmpty()) {
ArriveEventInfo arriveInfo = argsArray.getObject(0, ArriveEventInfo.class);
if ("ok".equals(arriveInfo.getStatus())) {
// 到达成功 - 实现你的业务逻辑
log.info("机器人{}成功到达目标点", deviceId);
// 例如: 更新数据库任务状态
// taskService.updateTaskStatus(deviceId, "completed");
} else {
// 到达失败 - 处理错误
TaskErrorCode errorCode = TaskErrorCode.fromCode(arriveInfo.getError());
log.error("机器人{}到达失败: {}", deviceId, errorCode.getDescription());
// 例如: 记录错误日志
// errorLogService.recordError(deviceId, errorCode);
}
}
} catch (Exception e) {
log.error("解析到达事件失败", e);
}
}
A: 检查:
A: 检查:
A: 检查:
A: 使用MQTT客户端工具:
README.mdLD导航系统MQTT接口.mdCHANGELOG.md如有问题,请查看: