feat: 航线任务与一键起飞任务
This commit is contained in:
+171
@@ -1,17 +1,40 @@
|
||||
package com.central.device.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.JsonElement;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.device.entity.WaylineRoute;
|
||||
import com.central.device.feign.FileCenterService;
|
||||
import com.central.device.message.ControlMessageConstant;
|
||||
import com.central.device.model.AgeDevice;
|
||||
import com.central.device.model.LineFile;
|
||||
import com.central.device.service.IAgeDeviceService;
|
||||
import com.central.device.service.ILineFileService;
|
||||
import com.central.device.service.IWaylineRouteService;
|
||||
import com.central.device.service.LiveStreamService;
|
||||
import com.central.device.util.IotRequestResult;
|
||||
import com.central.device.util.RequestUtils;
|
||||
import com.central.device.util.TencentLiveUtil;
|
||||
import com.central.device.websocket.manager.DeviceCommandManager;
|
||||
import com.central.device.websocket.model.CommandStatus;
|
||||
import com.central.device.websocket.model.DeviceCommand;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 设备命令控制器
|
||||
* 提供HTTP接口接收前端命令请求,发送命令到设备
|
||||
@@ -25,9 +48,29 @@ import org.springframework.web.bind.annotation.*;
|
||||
@Tag(name = "设备命令控制")
|
||||
public class DeviceCommandController {
|
||||
|
||||
@Value("${jetlinks.server.baseUrl}")
|
||||
private String baseUrl;
|
||||
|
||||
@Autowired
|
||||
private DeviceCommandManager commandManager;
|
||||
|
||||
@Autowired
|
||||
private ILineFileService lineFileService;
|
||||
|
||||
@Autowired
|
||||
private FileCenterService fileCenterService;
|
||||
|
||||
@Autowired
|
||||
private IWaylineRouteService iWaylineRouteService;
|
||||
|
||||
@Autowired
|
||||
private RequestUtils requestUtils;
|
||||
|
||||
@Resource
|
||||
private IAgeDeviceService iAgeDeviceService;
|
||||
|
||||
private final ControlMessageConstant controlMessageConstant = new ControlMessageConstant();
|
||||
|
||||
/**
|
||||
* 发送设备命令
|
||||
*/
|
||||
@@ -388,6 +431,134 @@ public class DeviceCommandController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行航线任务
|
||||
* 参考writeProperties逻辑,用于航线任务执行(不包含订单业务逻辑和幂等性检查)
|
||||
*/
|
||||
@Operation(summary = "执行航线任务")
|
||||
@PostMapping("/wayline/execute")
|
||||
public Result<String> executeWayline(@RequestParam("deviceSn") String deviceSn,
|
||||
@RequestParam("waylineId") String waylineId,
|
||||
@RequestParam("missionCode") String missionCode) {
|
||||
log.info("开始执行航线任务: deviceSn={}, waylineId={}, missionCode={}", deviceSn, waylineId, missionCode);
|
||||
try {
|
||||
// 1. 参数校验
|
||||
if (deviceSn == null || deviceSn.isEmpty()) {
|
||||
return Result.failed("设备SN不能为空");
|
||||
}
|
||||
if (waylineId == null || waylineId.isEmpty()) {
|
||||
return Result.failed("航线ID不能为空");
|
||||
}
|
||||
if (missionCode == null || missionCode.isEmpty()) {
|
||||
return Result.failed("任务编码不能为空");
|
||||
}
|
||||
|
||||
//根据routeid去获取航迹文件
|
||||
WaylineRoute route = iWaylineRouteService.getByRouteId(waylineId);
|
||||
if(route==null){
|
||||
return Result.failed("航线文件不存在");
|
||||
}
|
||||
// 2. 获取航线文件信息
|
||||
LineFile lineFile = lineFileService.findLineFileByWaylineId(route.getFileWaylineId());
|
||||
if (lineFile == null) {
|
||||
log.error("航线文件不存在: waylineId={}", waylineId);
|
||||
return Result.failed("航线文件不存在");
|
||||
}
|
||||
|
||||
// 3. 构建控制消息
|
||||
JsonObject controlMessage = buildWaylineControlMessage(deviceSn, waylineId, missionCode, lineFile);
|
||||
if (controlMessage == null) {
|
||||
return Result.failed("构建航线控制消息失败");
|
||||
}
|
||||
// 4. 发送设备控制请求到IoT平台
|
||||
String url = baseUrl + "/device/instance/" + deviceSn + "/property";
|
||||
log.info("发送航线任务到IoT平台: url={}, message={}", url, controlMessage);
|
||||
java.util.Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
||||
// 5. 处理响应结果
|
||||
return handleWaylineResponse(result, missionCode, deviceSn);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("执行航线任务异常: deviceSn={}, waylineId={}, missionCode={}, error={}",
|
||||
deviceSn, waylineId, missionCode, e.getMessage(), e);
|
||||
return Result.failed("执行航线任务异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建航线控制消息
|
||||
*/
|
||||
private JsonObject buildWaylineControlMessage(String deviceSn, String waylineId, String missionCode, LineFile lineFile) {
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
// 使用 flighttask_prepare 命令模板
|
||||
JsonObject jsonObject = gson.fromJson(controlMessageConstant.CONTROL_MESSAGE_MAP.get("flighttask_prepare"), JsonObject.class);
|
||||
JsonObject data = jsonObject.getAsJsonObject("data");
|
||||
|
||||
// 设置任务编码(代替订单号)
|
||||
data.addProperty("order_no", missionCode);
|
||||
|
||||
// 设置航线文件路径(从objectKey获取完整URL)
|
||||
String waylineFilePath = fileCenterService.getFileViewUrl(lineFile.getWaylineId());
|
||||
data.addProperty("wayline_link_url", waylineFilePath);
|
||||
// 设置航线文件MD5签名
|
||||
data.addProperty("file_md5", lineFile.getSign());
|
||||
// 设置设备视频流地址
|
||||
try {
|
||||
data.add("stream_urls", gson.fromJson(getStreamUrls(deviceSn).toJSONString(), JsonElement.class));
|
||||
} catch (Exception e) {
|
||||
log.error("生成设备推流/拉流地址异常: deviceSn={}, error={}", deviceSn, e.getMessage(), e);
|
||||
data.addProperty("stream_urls", "{}");
|
||||
}
|
||||
log.info("构建航线控制消息完成: deviceSn={}, waylineId={}, missionCode={}", deviceSn, waylineId, missionCode);
|
||||
return jsonObject;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("构建航线控制消息失败: deviceSn={}, waylineId={}, error={}", deviceSn, waylineId, e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理航线任务响应结果
|
||||
*/
|
||||
private Result<String> handleWaylineResponse(java.util.Map<String, String> result, String missionCode, String deviceSn) throws JsonProcessingException {
|
||||
com.alibaba.fastjson.JSONObject object = com.alibaba.fastjson.JSONObject.parseObject(result.get(""));
|
||||
if (object == null) {
|
||||
log.error("请求IoT平台异常,返回结果为空: missionCode={}", missionCode);
|
||||
return Result.failed("请求IoT平台异常");
|
||||
}
|
||||
IotRequestResult requestResult = IotRequestResult.getInstance(object);
|
||||
log.info("航线任务IoT响应: missionCode={}, status={}, message={}", missionCode, requestResult.getStatus(), requestResult.getMessage());
|
||||
|
||||
if (requestResult.getStatus() == 200) {
|
||||
log.info("航线任务下发成功: missionCode={}, deviceSn={}", missionCode, deviceSn);
|
||||
return Result.succeed(missionCode, "航线任务执行成功");
|
||||
} else if (requestResult.getStatus() == 500) {
|
||||
if ("Device is offline".equals(requestResult.getMessage())) {
|
||||
log.warn("设备离线,无法执行航线任务: missionCode={}, deviceSn={}", missionCode, deviceSn);
|
||||
return Result.failed("设备已离线,无法执行航线任务");
|
||||
}
|
||||
log.error("航线任务执行失败: missionCode={}, message={}", missionCode, requestResult.getMessage());
|
||||
return Result.failed("航线任务执行失败: " + requestResult.getMessage());
|
||||
}
|
||||
|
||||
return Result.failed("航线任务执行失败,请重试");
|
||||
}
|
||||
|
||||
|
||||
public JSONObject getStreamUrls(String deviceId) {
|
||||
JSONObject result = new JSONObject();
|
||||
AgeDevice device = iAgeDeviceService.getByDeviceSn(deviceId);
|
||||
if(Objects.nonNull(device)){
|
||||
result.put("pushUrl", device.getVideoStreamUrl());
|
||||
}
|
||||
result.put("playRtmpUrl", "");
|
||||
result.put("playFlvUrl", "");
|
||||
result.put("playM3u8Url", "");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取状态描述
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user