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; import com.ruoyi.device.mapper.entity.PreCheckLogEntity; 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) { log.info("【FlightEventCallback】收到飞行事件: deviceSn={}, topic={}, payload={}", deviceSn, topic, payload); try { JSONObject data = JSONObject.parseObject(payload); if (data == null) { log.warn("【FlightEventCallback】解析payload失败: deviceSn={}, topic={}, payload={}", deviceSn, topic, payload); return; } String msg = data.getString("msg"); String messageID = data.getString("messageID"); String action = data.getString("action"); log.info("【FlightEventCallback】解析飞行事件数据: deviceSn={}, messageID={}, msg=, action={}", deviceSn, messageID, msg, action); if (action == null || action.isEmpty()) { JSONObject dataObj = data.getJSONObject("data"); if (dataObj != null) { action = dataObj.getString("action"); log.info("【FlightEventCallback】从data对象中获取action: deviceSn={}, action={}", deviceSn, action); } } FlightEntity flight; if (messageID != null && !messageID.isEmpty()) { log.info("【FlightEventCallback】通过messageID获取飞行记录: deviceSn={}, messageID={}", deviceSn, messageID); flight = handleFlightIdExternal(deviceSn, messageID); } else { log.info("【FlightEventCallback】获取当前飞行记录: deviceSn={}", deviceSn); flight = flightService.getOrCreateCurrentFlight(deviceSn); } if (flight == null) { log.error("【FlightEventCallback】获取飞行记录失败: deviceSn={}, messageID={}", deviceSn, messageID); return; } log.info("【FlightEventCallback】获取到飞行记录: deviceSn={}, flightId={}, status={}", deviceSn, flight.getFlightId(), flight.getStatus()); if (msg != null && !msg.isEmpty()) { if (msg.contains("自检")) { log.info("【FlightEventCallback】检测到自检消息: deviceSn={}, msg={}", deviceSn, msg); handlePreCheckLog(deviceSn, msg, data, flight); } else { log.info("【FlightEventCallback】检测到飞行日志消息: deviceSn={}, msg={}", deviceSn, msg); handleFlightLog(deviceSn, msg, flight); } handleFlightStatus(deviceSn, action, data, flight); } else { log.warn("【FlightEventCallback】消息内容为空: deviceSn={}, messageID={}", deviceSn, messageID); } } catch (Exception e) { log.error("【FlightEventCallback】处理飞行事件失败: 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.error("【FlightEventCallback】飞行记录为空,无法保存飞行日志: deviceSn={}, message={}", deviceSn, message); return; } log.info("【FlightEventCallback】准备保存飞行日志: deviceSn={}, flightId={}, message={}", deviceSn, flight.getFlightId(), message); try { FlightLogEntity logEntity = new FlightLogEntity(); logEntity.setFlightId(flight.getFlightId()); logEntity.setLogContent(message); flightService.insertFlightLog(logEntity); log.info("【FlightEventCallback】成功保存飞行日志: deviceSn={}, flightId={}, logId={}, message={}", deviceSn, flight.getFlightId(), logEntity.getLogId(), message); } catch (Exception e) { log.error("【FlightEventCallback】保存飞行日志失败: deviceSn={}, flightId={}, message={}, error={}", deviceSn, flight.getFlightId(), message, e.getMessage(), e); } } private void handlePreCheckLog(String deviceSn, String msg, JSONObject data, FlightEntity flight) { if (flight == null) { log.error("【FlightEventCallback】飞行记录为空,无法保存自检日志: deviceSn={}, msg={}", deviceSn, msg); return; } log.info("【FlightEventCallback】准备保存自检日志: deviceSn={}, flightId={}, msg={}", deviceSn, flight.getFlightId(), msg); 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("【FlightEventCallback】成功保存自检日志: deviceSn={}, flightId={}, logId={}, msg={}, success={}", deviceSn, flight.getFlightId(), logEntity.getLogId(), msg, success); } catch (Exception e) { log.error("【FlightEventCallback】保存自检日志失败: deviceSn={}, flightId={}, msg={}, error={}", deviceSn, flight.getFlightId(), msg, e.getMessage(), e); } } private void handleFlightStatus(String deviceSn, String action, JSONObject data, FlightEntity flight) { if (flight == null) { log.error("【FlightEventCallback】飞行记录为空,无法更新状态: deviceSn={}, action={}", deviceSn, action); return; } log.debug("【FlightEventCallback】检查是否需要更新飞行状态: deviceSn={}, flightId={}, action={}", deviceSn, flight.getFlightId(), action); try { String msg = data.getString("msg"); String dataContent = data.getString("data"); if ((msg != null && msg.contains("起飞成功")) || (dataContent != null && dataContent.contains("起飞成功"))) { log.info("【FlightEventCallback】检测到起飞成功,更新状态为飞行中: deviceSn={}, flightId={}", deviceSn, flight.getFlightId()); flightService.updateFlightStatus(flight.getFlightId(), "飞行中"); log.info("【FlightEventCallback】飞行状态更新成功: deviceSn={}, flightId={}, status=飞行中", deviceSn, flight.getFlightId()); } else if ((msg != null && msg.contains("返航成功")) || (dataContent != null && dataContent.contains("返航成功")) || (dataContent != null && dataContent.contains("任务飞行完成"))) { log.info("【FlightEventCallback】检测到返航成功,更新状态为已返航: deviceSn={}, flightId={}", deviceSn, flight.getFlightId()); flightService.updateFlightStatus(flight.getFlightId(), "已返航"); log.info("【FlightEventCallback】飞行状态更新成功: deviceSn={}, flightId={}, status=已返航", deviceSn, flight.getFlightId()); } else { log.debug("【FlightEventCallback】无需更新飞行状态: deviceSn={}, flightId={}, msg={}", deviceSn, flight.getFlightId(), msg); } } catch (Exception e) { log.error("【FlightEventCallback】更新飞行状态失败: deviceSn={}, flightId={}, action={}, error={}", deviceSn, flight.getFlightId(), action, e.getMessage(), e); } } }