添加一键起飞功能

This commit is contained in:
孙小云 2026-03-10 14:56:42 +08:00
parent 7d632576ea
commit fb387aaa1e
5 changed files with 125 additions and 117 deletions

View File

@ -1,6 +1,5 @@
package com.ruoyi.device.controller;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.device.api.domain.*;
@ -11,8 +10,7 @@ import com.ruoyi.device.domain.impl.machine.command.CommandResult;
import com.ruoyi.device.domain.impl.machine.command.CommandType;
import com.ruoyi.device.domain.impl.machine.state.MachineStates;
import com.ruoyi.device.service.FlightService;
import com.ruoyi.task.api.RemoteTaskService;
import com.ruoyi.task.api.domain.TaskDTO;
import com.ruoyi.task.api.enums.StatusEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -20,7 +18,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -237,7 +234,7 @@ public class AircraftFlyController extends BaseController
}
//从配置文件获取
private String airlineFileUrl = "";
private final static String airlineFileUrl = "https://minio-dx.t-aaron.com:2443/th-airport/testFile/191ec54c-062c-4828-aab6-cefc901add78.waypoints";
/**
* 无人机一键起飞
@ -250,33 +247,34 @@ public class AircraftFlyController extends BaseController
public R<String> takeoff(@RequestBody DroneTakeoffRequest request)
{
flightService.onClickTakeOff(request.getSn(),airlineFileUrl);
Long taskId = flightService.createClickTakeOffTask(request.getSn(),airlineFileUrl);
log.info("一键起飞,生成一键起飞任务");
if(true){
return R.ok("OK");
}
log.info("收到无人机起飞请求: sn={}, messageID={}", request.getSn(), request.getMessageID());
log.info("收到无人机起飞请求: sn={} ", request.getSn());
try {
java.util.Map<String, Object> params = new java.util.HashMap<>();
if(Objects.isNull(request.getMessageID())){
params.put("messageID", UUID.randomUUID().toString());
}else {
params.put("messageID", request.getMessageID());
}
params.put("airlineFileUrl", request.getAirlineFileUrl());
params.put("airlineFileUrl", airlineFileUrl);
params.put("flyBatteryMin", request.getFlyBatteryMin());
params.put("messageID", taskId);
CompletableFuture<CommandResult> future = machineCommandManager.executeCommand(request.getSn(), CommandType.TAKE_OFF, params);
CommandResult result = future.get();
if (result.isSuccess()) {
log.info("无人机起飞成功: sn={}", request.getSn());
flightService.updateFlightStatus(taskId, StatusEnum.RUNNING);
return R.ok("起飞命令执行成功");
} else {
log.error("无人机起飞失败: sn={}, reason={}", request.getSn(), result.getErrorMessage());
flightService.updateFlightStatus(taskId, StatusEnum.FAILED);
return R.fail("起飞命令执行失败: " + result.getErrorMessage());
}
} catch (Exception e) {
log.error("无人机起飞异常: sn={}", request.getSn(), e);
flightService.updateFlightStatus(taskId, StatusEnum.FAILED);
return R.fail("起飞命令执行异常: " + e.getMessage());
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.device.service;
import com.ruoyi.device.mapper.entity.FlightEntity;
import com.ruoyi.task.api.enums.StatusEnum;
import java.util.Map;
@ -13,7 +14,7 @@ import java.util.Map;
public interface FlightService
{
public Long onClickTakeOff(String sn,String url);
public Long createClickTakeOffTask(String sn, String url);
/**
* 获取或创建飞行记录通过messageID匹配
@ -24,7 +25,7 @@ public interface FlightService
* @param messageId 消息ID对应flightIdExternal
* @return 飞行记录
*/
FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId);
// FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId);
// /**
// * 获取最新的飞行记录包括已返航的
@ -50,7 +51,7 @@ public interface FlightService
* @param flightId 飞行ID
* @param status 状态自检中飞行中已返航
*/
void updateFlightStatus(Long flightId, String status);
void updateFlightStatus(Long flightId, StatusEnum status);
// /**
// * 更新返航时间

View File

@ -7,6 +7,7 @@ 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 com.ruoyi.task.api.enums.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -66,13 +67,13 @@ public class FlightEventCallback implements IAirportFlyControlCallback, IAirport
log.info("【FlightEventCallback】处理control消息: deviceSn={}, action={}, messageID={}", deviceSn, action, messageID);
if ("airlineFlight".equals(action) && messageID != null && !messageID.isEmpty()) {
// 创建飞行记录如果不存在
flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
log.info("【FlightEventCallback】创建/获取飞行记录: deviceSn={}, messageID={}", deviceSn, messageID);
// // 创建飞行记录如果不存在
// flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
// log.info("【FlightEventCallback】创建/获取飞行记录: deviceSn={}, messageID={}", deviceSn, messageID);
} else if ("immediateReturn".equals(action) && messageID != null && !messageID.isEmpty()) {
FlightEntity flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
flightService.updateFlightStatus(flight.getFlightId(), "RETURNING");
log.info("【FlightEventCallback】更新飞行状态为RETURNING: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
// FlightEntity flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
// flightService.updateFlightStatus(flight.getFlightId(), "RETURNING");
// log.info("【FlightEventCallback】更新飞行状态为RETURNING: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
}
}
@ -90,49 +91,51 @@ public class FlightEventCallback implements IAirportFlyControlCallback, IAirport
return;
}
// 通过 messageID 获取飞行记录
FlightEntity flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
if (flight == null) {
log.error("【FlightEventCallback】获取飞行记录失败: deviceSn={}, messageID={}", deviceSn, messageID);
return;
}
log.info("【FlightEventCallback】处理control/data消息: deviceSn={}, flightId={}, messageID={}, msg={}, code={}",
deviceSn, flight.getFlightId(), messageID, msg, code);
Long taskId = Long.valueOf(messageID);
//
// // 通过 messageID 获取飞行记录
// FlightEntity flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
// if (flight == null) {
// log.error("【FlightEventCallback】获取飞行记录失败: deviceSn={}, messageID={}", deviceSn, messageID);
// return;
// }
//
// log.info("【FlightEventCallback】处理control/data消息: deviceSn={}, flightId={}, messageID={}, msg={}, code={}",
// deviceSn, flight.getFlightId(), messageID, msg, code);
// 判断是否为 [地面站]无人机起飞成功
if (msg != null && msg.contains("[地面站]无人机起飞成功")) {
// 起飞成功存到 device_flight_log
handleFlightLog(deviceSn, msg, flight);
handleFlightLog(deviceSn, msg, taskId);
// 更新状态为 FLYING
log.info("【FlightEventCallback】检测到起飞成功更新状态为FLYING: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
flightService.updateFlightStatus(flight.getFlightId(), "FLYING");
log.info("【FlightEventCallback】检测到起飞成功更新状态为FLYING: deviceSn={}, flightId={}", deviceSn, taskId);
flightService.updateFlightStatus(taskId, StatusEnum.RUNNING);
return;
}
// 检查 device_flight_log 是否有数据判断是否已起飞
boolean hasTakenOff = flightService.hasFlightLog(flight.getFlightId());
boolean hasTakenOff = flightService.hasFlightLog(taskId);
if (hasTakenOff) {
// 已起飞所有消息存到 device_flight_log
log.info("【FlightEventCallback】已起飞存入飞行日志: deviceSn={}, flightId={}, msg={}", deviceSn, flight.getFlightId(), msg);
handleFlightLog(deviceSn, msg, flight);
log.info("【FlightEventCallback】已起飞存入飞行日志: deviceSn={}, flightId={}, msg={}", deviceSn, taskId, msg);
handleFlightLog(deviceSn, msg, taskId);
// 检查是否任务完成
String dataContent = data.getString("data");
if ("操作成功".equals(msg) && "[地面站]任务飞行完成".equals(dataContent)) {
log.info("【FlightEventCallback】检测到任务完成更新状态为HOME: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
flightService.updateFlightStatus(flight.getFlightId(), "HOME");
log.info("【FlightEventCallback】检测到任务完成更新状态为HOME: deviceSn={}, flightId={}", deviceSn, taskId);
flightService.updateFlightStatus(taskId, StatusEnum.COMPLETED);
}
} else {
// 未起飞所有消息存到 device_pre_check_log
log.info("【FlightEventCallback】未起飞存入自检日志: deviceSn={}, flightId={}, msg={}, code={}", deviceSn, flight.getFlightId(), msg, code);
handlePreCheckLog(deviceSn, msg, code, flight);
log.info("【FlightEventCallback】未起飞存入自检日志: deviceSn={}, flightId={}, msg={}, code={}", deviceSn, taskId, msg, code);
handlePreCheckLog(deviceSn, msg, code, taskId);
// 检查是否自检失败code=1 表示失败
if (code != null && (code == 1 || code == -1)) {
log.info("【FlightEventCallback】检测到自检失败(code=1)更新状态为ERROR: deviceSn={}, flightId={}", deviceSn, flight.getFlightId());
flightService.updateFlightStatus(flight.getFlightId(), "ERROR");
log.info("【FlightEventCallback】检测到自检失败(code=1)更新状态为ERROR: deviceSn={}, flightId={}", deviceSn, taskId);
flightService.updateFlightStatus(taskId, StatusEnum.FAILED);
}
}
}
@ -141,57 +144,57 @@ public class FlightEventCallback implements IAirportFlyControlCallback, IAirport
* 保存自检日志
* @param code 0=成功1=失败
*/
private void handlePreCheckLog(String deviceSn, String msg, Integer code, FlightEntity flight) {
if (flight == null) {
log.error("【FlightEventCallback】飞行记录为空无法保存自检日志: deviceSn={}, msg={}", deviceSn, msg);
return;
}
log.info("【FlightEventCallback】准备保存自检日志: deviceSn={}, flightId={}, msg={}, code={}",
deviceSn, flight.getFlightId(), msg, code);
private void handlePreCheckLog(String deviceSn, String msg, Integer code, Long taskId) {
// if (flight == null) {
// log.error("【FlightEventCallback】飞行记录为空无法保存自检日志: deviceSn={}, msg={}", deviceSn, msg);
// return;
// }
//
// log.info("【FlightEventCallback】准备保存自检日志: deviceSn={}, flightId={}, msg={}, code={}",
// deviceSn, flight.getFlightId(), msg, code);
try {
// code=0 表示成功code=1 表示失败
Boolean success = (code != null && code == 0);
PreCheckLogEntity logEntity = new PreCheckLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setFlightId(taskId);
logEntity.setLogContent(msg);
logEntity.setSuccess(success);
flightService.insertPreCheckLog(logEntity);
log.info("【FlightEventCallback】成功保存自检日志: deviceSn={}, flightId={}, logId={}, msg={}, code={}, success={}",
deviceSn, flight.getFlightId(), logEntity.getLogId(), msg, code, success);
deviceSn, taskId, logEntity.getLogId(), msg, code, success);
} catch (Exception e) {
log.error("【FlightEventCallback】保存自检日志失败: deviceSn={}, flightId={}, msg={}, error={}",
deviceSn, flight.getFlightId(), msg, e.getMessage(), e);
deviceSn, taskId, msg, e.getMessage(), e);
}
}
/**
* 保存飞行日志
*/
private void handleFlightLog(String deviceSn, String message, FlightEntity flight) {
if (flight == null) {
log.error("【FlightEventCallback】飞行记录为空无法保存飞行日志: deviceSn={}, message={}", deviceSn, message);
return;
}
private void handleFlightLog(String deviceSn, String message, Long taskId) {
// if (flight == null) {
// log.error("【FlightEventCallback】飞行记录为空无法保存飞行日志: deviceSn={}, message={}", deviceSn, message);
// return;
// }
log.info("【FlightEventCallback】准备保存飞行日志: deviceSn={}, flightId={}, message={}", deviceSn, flight.getFlightId(), message);
log.info("【FlightEventCallback】准备保存飞行日志: deviceSn={}, flightId={}, message={}", deviceSn, taskId, message);
try {
FlightLogEntity logEntity = new FlightLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setFlightId(taskId);
logEntity.setLogContent(message);
flightService.insertFlightLog(logEntity);
log.info("【FlightEventCallback】成功保存飞行日志: deviceSn={}, taskId={}, logId={}, message={}",
deviceSn, taskId, logEntity.getLogId(), message);
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);
log.error("【FlightEventCallback】保存飞行日志失败: deviceSn={}, taskId={}, message={}, error={}",
deviceSn, taskId, message, e.getMessage(), e);
}
}
}

View File

@ -9,6 +9,7 @@ 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 com.ruoyi.task.api.enums.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -64,17 +65,19 @@ public class FlightLogCallback implements IDroneRealTimeCallback {
log.info("【FlightLogCallback】开始处理自检日志: deviceSn={}, messageId={}, jianchaJson={}", deviceSn, messageID, jianchaJson);
try {
FlightEntity flight;
if (messageID != null && !messageID.isEmpty()) {
log.info("【FlightLogCallback】通过messageId获取飞行记录: deviceSn={}, messageId={}", deviceSn, messageID);
flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
} else {
log.warn("【FlightLogCallback】获取当前飞行记录: deviceSn={} messageID为空", deviceSn);
return;
}
Long taskId = Long.valueOf(messageID);
// FlightEntity flight;
//
// if (messageID != null && !messageID.isEmpty()) {
// log.info("【FlightLogCallback】通过messageId获取飞行记录: deviceSn={}, messageId={}", deviceSn, messageID);
// flight = flightService.getOrCreateFlightByMessageId(deviceSn, messageID);
// } else {
// log.warn("【FlightLogCallback】获取当前飞行记录: deviceSn={} messageID为空", deviceSn);
// return;
// }
log.info("【FlightLogCallback】获取到飞行记录: deviceSn={}, flightId={}, status={}", deviceSn, flight.getFlightId(), flight.getStatus());
// log.info("【FlightLogCallback】获取到飞行记录: deviceSn={}, flightId={}, status={}", deviceSn, flight.getFlightId(), flight.getStatus());
JSONArray checkItems = JSON.parseArray(jianchaJson);
if (checkItems == null || checkItems.isEmpty()) {
@ -82,12 +85,12 @@ public class FlightLogCallback implements IDroneRealTimeCallback {
return;
}
log.info("【FlightLogCallback】解析到{}个自检项: deviceSn={}, flightId={}", checkItems.size(), deviceSn, flight.getFlightId());
// log.info("【FlightLogCallback】解析到{}个自检项: deviceSn={}, flightId={}", checkItems.size(), deviceSn, flight.getFlightId());
for (int i = 0; i < checkItems.size(); i++) {
JSONObject item = checkItems.getJSONObject(i);
PreCheckLogEntity logEntity = new PreCheckLogEntity();
logEntity.setFlightId(flight.getFlightId());
logEntity.setFlightId(taskId);
String check = item.getString("check");
String value = item.getString("value");
@ -97,22 +100,22 @@ public class FlightLogCallback implements IDroneRealTimeCallback {
String logContent = check + " " + value + " " + statusText;
if(Boolean.FALSE.equals(result)){
flightService.updateFlightStatus(flight.getFlightId(), "ERROR");
flightService.updateFlightStatus(taskId, StatusEnum.FAILED);
}
logEntity.setLogContent(logContent);
logEntity.setSuccess(result != null ? result : false);
log.info("【FlightLogCallback】准备插入自检日志[{}/{}]: deviceSn={}, flightId={}, check={}, value={}, result={}",
i + 1, checkItems.size(), deviceSn, flight.getFlightId(), check, value, result);
i + 1, checkItems.size(), deviceSn, taskId, check, value, result);
flightService.insertPreCheckLog(logEntity);
log.info("【FlightLogCallback】成功插入自检日志[{}/{}]: deviceSn={}, flightId={}, logId={}",
i + 1, checkItems.size(), deviceSn, flight.getFlightId(), logEntity.getLogId());
i + 1, checkItems.size(), deviceSn, taskId, logEntity.getLogId());
}
log.info("【FlightLogCallback】完成保存自检日志: deviceSn={}, flightId={}, 检查项数量={}",
deviceSn, flight.getFlightId(), checkItems.size());
deviceSn, taskId, checkItems.size());
} catch (Exception e) {
log.error("【FlightLogCallback】保存自检日志失败: deviceSn={}, messageId={}, jiancha={}, error={}",
deviceSn, messageID, jianchaJson, e.getMessage(), e);

View File

@ -43,7 +43,7 @@ public class FlightServiceImpl implements FlightService
@Autowired
private RemoteTaskService remoteTaskService;
public Long onClickTakeOff(String sn,String routeUrl){
public Long createClickTakeOffTask(String sn, String routeUrl){
TaskDTO taskDTO = new TaskDTO();
taskDTO.setActualStartTime(new Date());
taskDTO.setStartTime(new Date());
@ -51,37 +51,38 @@ public class FlightServiceImpl implements FlightService
taskDTO.setTaskCategory(TaskCategoryEnum.MANUAL_FLIGHT);
taskDTO.setRouteId(-1L);
taskDTO.setUavId(sn);
taskDTO.setStatus(StatusEnum.RUNNING);
taskDTO.setStatus(StatusEnum.PENDING);
taskDTO.setRouteUrl(routeUrl);
R<Long> taskId = remoteTaskService.createTaskWithoutPlan(taskDTO,SecurityConstants.INNER);
return taskId.getData();
}
@Override
@Transactional(rollbackFor = Exception.class)
public FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId) {
// 先查询是否存在相同 deviceSn flight_id_extern 的记录
FlightEntity flight = flightMapper.selectFlightByDeviceSnAndFlightIdExternal(deviceSn, messageId);
if (flight != null) {
// 如果存在直接返回
log.info("找到已存在的飞行记录: deviceSn={}, messageId={}, flightId={}",
deviceSn, messageId, flight.getFlightId());
return flight;
}
// 如果不存在创建新的飞行记录
flight = new FlightEntity();
flight.setDeviceSn(deviceSn);
flight.setFlightIdExternal(messageId);
flight.setStatus("CHECKING");
flightMapper.insertFlight(flight);
log.info("创建新的飞行记录: deviceSn={}, messageId={}, flightId={}, status=CHECKING",
deviceSn, messageId, flight.getFlightId());
return flight;
}
//// @Override
//// @Transactional(rollbackFor = Exception.class)
// public FlightEntity getOrCreateFlightByMessageId(String deviceSn, String messageId) {
// // 先查询是否存在相同 deviceSn flight_id_extern 的记录
// FlightEntity flight = flightMapper.selectFlightByDeviceSnAndFlightIdExternal(deviceSn, messageId);
//
// if (flight != null) {
// // 如果存在直接返回
// log.info("找到已存在的飞行记录: deviceSn={}, messageId={}, flightId={}",
// deviceSn, messageId, flight.getFlightId());
// return flight;
// }
//
// // 如果不存在创建新的飞行记录
// flight = new FlightEntity();
// flight.setDeviceSn(deviceSn);
// flight.setFlightIdExternal(messageId);
// flight.setStatus("CHECKING");
// flightMapper.insertFlight(flight);
//
// log.info("创建新的飞行记录: deviceSn={}, messageId={}, flightId={}, status=CHECKING",
// deviceSn, messageId, flight.getFlightId());
//
// return flight;
// }
// @Override
// public FlightEntity getLatestFlight(String deviceSn) {
@ -100,14 +101,16 @@ public class FlightServiceImpl implements FlightService
// }
@Override
public void updateFlightStatus(Long flightId, String status) {
flightMapper.updateFlightStatus(flightId, status);
log.info("更新飞行状态: flightId={}, status={}", flightId, status);
public void updateFlightStatus(Long flightId,StatusEnum status) {
if ("HOME".equals(status) ||"ERROR".equals(status)) {
flightMapper.updateReturnTime(flightId);
log.info("更新返航时间: flightId={}", flightId);
}
remoteTaskService.updateTaskStatus(flightId,status,SecurityConstants.INNER);
// flightMapper.updateFlightStatus(flightId, status);
// log.info("更新飞行状态: flightId={}, status={}", flightId, status);
//
// if ("HOME".equals(status) ||"ERROR".equals(status)) {
// flightMapper.updateReturnTime(flightId);
// log.info("更新返航时间: flightId={}", flightId);
// }
}
// @Override