a-tuoheng-device/src/main/java/com/ruoyi/device/service/impl/FlightEventCallback.java

165 lines
8.4 KiB
Java
Raw Normal View History

2026-02-25 13:06:56 +08:00
package com.ruoyi.device.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.device.domain.impl.tuohengmqtt.callback.IAirportFlyControlDataCallback;
import com.ruoyi.device.mapper.entity.FlightEntity;
import com.ruoyi.device.mapper.entity.FlightLogEntity;
2026-02-25 13:17:42 +08:00
import com.ruoyi.device.mapper.entity.PreCheckLogEntity;
2026-02-25 13:06:56 +08:00
import com.ruoyi.device.service.FlightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class FlightEventCallback implements IAirportFlyControlDataCallback {
@Autowired
private FlightService flightService;
@Override
public void onAirportFlyControlData(String deviceSn, String payload, String topic) {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】收到飞行事件: deviceSn={}, topic={}, payload={}", deviceSn, topic, payload);
2026-02-26 08:41:14 +08:00
2026-02-27 08:58:36 +08:00
try {
2026-02-25 13:06:56 +08:00
JSONObject data = JSONObject.parseObject(payload);
if (data == null) {
2026-02-27 08:58:36 +08:00
log.warn("【FlightEventCallback】解析payload失败: deviceSn={}, topic={}, payload={}", deviceSn, topic, payload);
2026-02-25 13:06:56 +08:00
return;
}
String msg = data.getString("msg");
String messageID = data.getString("messageID");
String action = data.getString("action");
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】解析飞行事件数据: deviceSn={}, messageID={}, msg=, action={}", deviceSn, messageID, msg, action);
2026-02-25 13:06:56 +08:00
if (action == null || action.isEmpty()) {
JSONObject dataObj = data.getJSONObject("data");
if (dataObj != null) {
action = dataObj.getString("action");
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】从data对象中获取action: deviceSn={}, action={}", deviceSn, action);
2026-02-25 13:06:56 +08:00
}
}
FlightEntity flight;
if (messageID != null && !messageID.isEmpty()) {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】通过messageID获取飞行记录: deviceSn={}, messageID={}", deviceSn, messageID);
2026-02-25 13:06:56 +08:00
flight = handleFlightIdExternal(deviceSn, messageID);
} else {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】获取当前飞行记录: deviceSn={}", deviceSn);
2026-02-25 13:06:56 +08:00
flight = flightService.getOrCreateCurrentFlight(deviceSn);
}
2026-02-27 08:58:36 +08:00
if (flight == null) {
log.error("【FlightEventCallback】获取飞行记录失败: deviceSn={}, messageID={}", deviceSn, messageID);
return;
}
log.info("【FlightEventCallback】获取到飞行记录: deviceSn={}, flightId={}, status={}", deviceSn, flight.getFlightId(), flight.getStatus());
2026-02-25 13:06:56 +08:00
if (msg != null && !msg.isEmpty()) {
2026-02-25 13:17:42 +08:00
if (msg.contains("自检")) {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】检测到自检消息: deviceSn={}, msg={}", deviceSn, msg);
2026-02-25 13:17:42 +08:00
handlePreCheckLog(deviceSn, msg, data, flight);
} else {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】检测到飞行日志消息: deviceSn={}, msg={}", deviceSn, msg);
2026-02-25 13:17:42 +08:00
handleFlightLog(deviceSn, msg, flight);
}
2026-02-25 13:06:56 +08:00
handleFlightStatus(deviceSn, action, data, flight);
2026-02-27 08:58:36 +08:00
} else {
log.warn("【FlightEventCallback】消息内容为空: deviceSn={}, messageID={}", deviceSn, messageID);
2026-02-25 13:06:56 +08:00
}
} catch (Exception e) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】处理飞行事件失败: deviceSn={}, topic={}, error={}", deviceSn, topic, e.getMessage(), e);
2026-02-25 13:06:56 +08:00
}
}
private FlightEntity handleFlightIdExternal(String deviceSn, String messageId) {
if (messageId != null && !messageId.isEmpty()) {
return flightService.getOrCreateFlightByMessageId(deviceSn, messageId);
} else {
return flightService.getOrCreateCurrentFlight(deviceSn);
}
}
private void handleFlightLog(String deviceSn, String message, FlightEntity flight) {
if (flight == null) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】飞行记录为空无法保存飞行日志: deviceSn={}, message={}", deviceSn, message);
2026-02-25 13:06:56 +08:00
return;
}
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】准备保存飞行日志: deviceSn={}, flightId={}, message={}", deviceSn, flight.getFlightId(), message);
2026-02-25 13:06:56 +08:00
try {
FlightLogEntity logEntity = new FlightLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setLogContent(message);
2026-02-27 08:58:36 +08:00
2026-02-25 13:06:56 +08:00
flightService.insertFlightLog(logEntity);
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】成功保存飞行日志: deviceSn={}, flightId={}, logId={}, message={}",
deviceSn, flight.getFlightId(), logEntity.getLogId(), message);
2026-02-25 13:06:56 +08:00
} catch (Exception e) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】保存飞行日志失败: deviceSn={}, flightId={}, message={}, error={}",
deviceSn, flight.getFlightId(), message, e.getMessage(), e);
2026-02-25 13:06:56 +08:00
}
}
2026-02-25 13:17:42 +08:00
private void handlePreCheckLog(String deviceSn, String msg, JSONObject data, FlightEntity flight) {
if (flight == null) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】飞行记录为空无法保存自检日志: deviceSn={}, msg={}", deviceSn, msg);
2026-02-25 13:17:42 +08:00
return;
}
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】准备保存自检日志: deviceSn={}, flightId={}, msg={}", deviceSn, flight.getFlightId(), msg);
2026-02-25 13:17:42 +08:00
try {
Boolean success = msg.contains("通过") || msg.contains("成功");
2026-02-27 08:58:36 +08:00
2026-02-25 13:17:42 +08:00
PreCheckLogEntity logEntity = new PreCheckLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setLogContent(msg);
logEntity.setSuccess(success);
2026-02-27 08:58:36 +08:00
2026-02-25 13:17:42 +08:00
flightService.insertPreCheckLog(logEntity);
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】成功保存自检日志: deviceSn={}, flightId={}, logId={}, msg={}, success={}",
deviceSn, flight.getFlightId(), logEntity.getLogId(), msg, success);
2026-02-25 13:17:42 +08:00
} catch (Exception e) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】保存自检日志失败: deviceSn={}, flightId={}, msg={}, error={}",
deviceSn, flight.getFlightId(), msg, e.getMessage(), e);
2026-02-25 13:17:42 +08:00
}
}
2026-02-25 13:06:56 +08:00
private void handleFlightStatus(String deviceSn, String action, JSONObject data, FlightEntity flight) {
if (flight == null) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】飞行记录为空无法更新状态: deviceSn={}, action={}", deviceSn, action);
2026-02-25 13:06:56 +08:00
return;
}
2026-02-27 08:58:36 +08:00
log.debug("【FlightEventCallback】检查是否需要更新飞行状态: deviceSn={}, flightId={}, action={}", deviceSn, flight.getFlightId(), action);
2026-02-25 13:06:56 +08:00
try {
String msg = data.getString("msg");
String dataContent = data.getString("data");
if ((msg != null && msg.contains("起飞成功")) || (dataContent != null && dataContent.contains("起飞成功"))) {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】检测到起飞成功更新状态为飞行中: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
2026-02-25 13:06:56 +08:00
flightService.updateFlightStatus(flight.getFlightId(), "飞行中");
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】飞行状态更新成功: deviceSn={}, flightId={}, status=飞行中", deviceSn, flight.getFlightId());
2026-02-25 13:06:56 +08:00
} else if ((msg != null && msg.contains("返航成功")) || (dataContent != null && dataContent.contains("返航成功")) ||
(dataContent != null && dataContent.contains("任务飞行完成"))) {
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】检测到返航成功更新状态为已返航: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
2026-02-25 13:06:56 +08:00
flightService.updateFlightStatus(flight.getFlightId(), "已返航");
2026-02-27 08:58:36 +08:00
log.info("【FlightEventCallback】飞行状态更新成功: deviceSn={}, flightId={}, status=已返航", deviceSn, flight.getFlightId());
} else {
log.debug("【FlightEventCallback】无需更新飞行状态: deviceSn={}, flightId={}, msg={}", deviceSn, flight.getFlightId(), msg);
2026-02-25 13:06:56 +08:00
}
} catch (Exception e) {
2026-02-27 08:58:36 +08:00
log.error("【FlightEventCallback】更新飞行状态失败: deviceSn={}, flightId={}, action={}, error={}",
deviceSn, flight.getFlightId(), action, e.getMessage(), e);
2026-02-25 13:06:56 +08:00
}
}
}