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

130 lines
5.5 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) {
try {
JSONObject data = JSONObject.parseObject(payload);
if (data == null) {
return;
}
String msg = data.getString("msg");
String messageID = data.getString("messageID");
String action = data.getString("action");
if (action == null || action.isEmpty()) {
JSONObject dataObj = data.getJSONObject("data");
if (dataObj != null) {
action = dataObj.getString("action");
}
}
FlightEntity flight;
if (messageID != null && !messageID.isEmpty()) {
flight = handleFlightIdExternal(deviceSn, messageID);
} else {
flight = flightService.getOrCreateCurrentFlight(deviceSn);
}
if (msg != null && !msg.isEmpty()) {
2026-02-25 13:17:42 +08:00
if (msg.contains("自检")) {
handlePreCheckLog(deviceSn, msg, data, flight);
} else {
handleFlightLog(deviceSn, msg, flight);
}
2026-02-25 13:06:56 +08:00
handleFlightStatus(deviceSn, action, data, flight);
}
} catch (Exception e) {
log.error("处理飞行事件失败: deviceSn={}, topic={}, error={}", deviceSn, topic, e.getMessage(), e);
}
}
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) {
log.warn("飞行记录为空,无法保存飞行日志: deviceSn={}, message={}", deviceSn, message);
return;
}
try {
FlightLogEntity logEntity = new FlightLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setLogContent(message);
flightService.insertFlightLog(logEntity);
log.info("保存飞行日志: deviceSn={}, flightId={}, message={}",
deviceSn, flight.getFlightId(), message);
} catch (Exception e) {
log.error("保存飞行日志失败: deviceSn={}, message={}, error={}",
deviceSn, message, e.getMessage(), e);
}
}
2026-02-25 13:17:42 +08:00
private void handlePreCheckLog(String deviceSn, String msg, JSONObject data, FlightEntity flight) {
if (flight == null) {
log.warn("飞行记录为空,无法保存自检日志: deviceSn={}, msg={}", deviceSn, msg);
return;
}
try {
Boolean success = msg.contains("通过") || msg.contains("成功");
PreCheckLogEntity logEntity = new PreCheckLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setLogContent(msg);
logEntity.setSuccess(success);
flightService.insertPreCheckLog(logEntity);
log.info("保存自检日志: deviceSn={}, flightId={}, msg={}, success={}",
deviceSn, flight.getFlightId(), msg, success);
} catch (Exception e) {
log.error("保存自检日志失败: deviceSn={}, msg={}, error={}",
deviceSn, msg, e.getMessage(), e);
}
}
2026-02-25 13:06:56 +08:00
private void handleFlightStatus(String deviceSn, String action, JSONObject data, FlightEntity flight) {
if (flight == null) {
log.warn("飞行记录为空,无法更新状态: deviceSn={}, action={}", deviceSn, action);
return;
}
try {
String msg = data.getString("msg");
String dataContent = data.getString("data");
if ((msg != null && msg.contains("起飞成功")) || (dataContent != null && dataContent.contains("起飞成功"))) {
flightService.updateFlightStatus(flight.getFlightId(), "飞行中");
log.info("飞行状态更新: deviceSn={}, status=飞行中", deviceSn);
} else if ((msg != null && msg.contains("返航成功")) || (dataContent != null && dataContent.contains("返航成功")) ||
(dataContent != null && dataContent.contains("任务飞行完成"))) {
flightService.updateFlightStatus(flight.getFlightId(), "已返航");
log.info("飞行状态更新: deviceSn={}, status=已返航", deviceSn);
}
} catch (Exception e) {
log.error("更新飞行状态失败: deviceSn={}, action={}, error={}",
deviceSn, action, e.getMessage(), e);
}
}
}