feat: 客户端pad
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
package com.central.device.controller;
|
||||
|
||||
import com.central.common.model.Result;
|
||||
import com.central.device.model.AgeDevice;
|
||||
import com.central.device.model.vo.BoatRouteListVO;
|
||||
import com.central.device.model.vo.ThreeDModelFileVO;
|
||||
import com.central.device.service.IAgeDeviceService;
|
||||
import com.central.device.service.IBoatRouteService;
|
||||
import com.central.device.service.IThreeDModelFileService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 客户端Pad页面专用接口
|
||||
* Gateway放行 /client/**,无需认证
|
||||
*
|
||||
* @author dzc
|
||||
* @date 2026-03-11
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/client")
|
||||
@Tag(name = "客户端Pad接口")
|
||||
@RequiredArgsConstructor
|
||||
public class ClientController {
|
||||
|
||||
private final IAgeDeviceService ageDeviceService;
|
||||
private final IBoatRouteService boatRouteService;
|
||||
private final IThreeDModelFileService threeDModelFileService;
|
||||
|
||||
/**
|
||||
* 校验设备SN是否存在
|
||||
*/
|
||||
@Operation(summary = "设备SN校验登录")
|
||||
@GetMapping("/auth/check")
|
||||
public Result<?> checkDeviceSn(
|
||||
@Parameter(description = "设备SN", required = true)
|
||||
@RequestParam String deviceSn) {
|
||||
AgeDevice device = ageDeviceService.getByDeviceSn(deviceSn);
|
||||
if (device == null) {
|
||||
return Result.failed("设备不存在或未绑定");
|
||||
}
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("deviceSn", device.getDeviceSn());
|
||||
info.put("deviceName", device.getDeviceName());
|
||||
info.put("deviceType", device.getDeviceType());
|
||||
return Result.succeed(info, "验证成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备航线列表
|
||||
*/
|
||||
@Operation(summary = "获取设备航线列表")
|
||||
@GetMapping("/routes")
|
||||
public Result<List<BoatRouteListVO>> getRoutes(
|
||||
@Parameter(description = "设备SN", required = true)
|
||||
@RequestParam String deviceSn) {
|
||||
List<BoatRouteListVO> routes = boatRouteService.listByDeviceSn(deviceSn);
|
||||
return Result.succeed(routes, "查询成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备关联的3D模型
|
||||
*/
|
||||
@Operation(summary = "获取设备3D模型列表")
|
||||
@GetMapping("/models")
|
||||
public Result<List<ThreeDModelFileVO>> getModels(
|
||||
@Parameter(description = "设备SN", required = true)
|
||||
@RequestParam String deviceSn) {
|
||||
List<ThreeDModelFileVO> models = threeDModelFileService.getByDeviceSn(deviceSn);
|
||||
return Result.succeed(models, "查询成功");
|
||||
}
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
package com.central.device.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.central.device.model.vo.BoatRealtimeVO;
|
||||
import com.central.device.service.IBoatTrackService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 客户端SSE遥测数据控制器
|
||||
* Gateway放行 /client/**,无需认证
|
||||
*
|
||||
* @author dzc
|
||||
* @date 2026-03-11
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/client/sse")
|
||||
@Tag(name = "客户端SSE遥测接口")
|
||||
@RequiredArgsConstructor
|
||||
public class ClientSseController {
|
||||
|
||||
private final IBoatTrackService boatTrackService;
|
||||
|
||||
private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>();
|
||||
|
||||
@Operation(summary = "客户端订阅设备遥测数据")
|
||||
@GetMapping(value = "/telemetry/{deviceSn}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public ResponseEntity<SseEmitter> subscribeTelemetry(
|
||||
@Parameter(description = "设备SN", required = true)
|
||||
@PathVariable String deviceSn,
|
||||
@Parameter(description = "客户端会话ID(可选)")
|
||||
@RequestHeader(value = "X-Client-Session-Id", required = false) String clientSessionId) {
|
||||
|
||||
String connectionId;
|
||||
if (clientSessionId != null && !clientSessionId.trim().isEmpty()) {
|
||||
connectionId = "client_" + clientSessionId + "_" + deviceSn;
|
||||
} else {
|
||||
connectionId = "client_" + UUID.randomUUID().toString() + "_" + deviceSn;
|
||||
}
|
||||
|
||||
// 移除已有连接
|
||||
if (emitters.containsKey(connectionId)) {
|
||||
removeConnection(connectionId, deviceSn);
|
||||
}
|
||||
|
||||
// 创建SSE连接(永不超时)
|
||||
SseEmitter emitter = new SseEmitter(0L);
|
||||
emitters.put(connectionId, emitter);
|
||||
|
||||
// 注册遥测回调
|
||||
boatTrackService.registerTelemetryCallback(deviceSn, connectionId, (data) -> {
|
||||
sendTelemetryData(connectionId, data);
|
||||
});
|
||||
|
||||
emitter.onCompletion(() -> {
|
||||
log.info("[ClientSSE] 连接完成: connectionId={}, deviceSn={}", connectionId, deviceSn);
|
||||
removeConnection(connectionId, deviceSn);
|
||||
});
|
||||
emitter.onTimeout(() -> {
|
||||
log.info("[ClientSSE] 连接超时: connectionId={}, deviceSn={}", connectionId, deviceSn);
|
||||
removeConnection(connectionId, deviceSn);
|
||||
});
|
||||
emitter.onError(e -> {
|
||||
log.warn("[ClientSSE] 连接错误: connectionId={}, error={}", connectionId, e.getMessage());
|
||||
removeConnection(connectionId, deviceSn);
|
||||
});
|
||||
|
||||
// 发送初始连接事件
|
||||
try {
|
||||
emitter.send(SseEmitter.event()
|
||||
.name("connected")
|
||||
.data("{\"connectionId\":\"" + connectionId + "\",\"deviceSn\":\"" + deviceSn + "\"}"));
|
||||
} catch (IOException e) {
|
||||
log.error("[ClientSSE] 发送初始事件失败: {}", e.getMessage());
|
||||
removeConnection(connectionId, deviceSn);
|
||||
}
|
||||
|
||||
log.info("[ClientSSE] 客户端订阅遥测: connectionId={}, deviceSn={}", connectionId, deviceSn);
|
||||
return ResponseEntity.ok().body(emitter);
|
||||
}
|
||||
|
||||
@Operation(summary = "客户端断开SSE连接")
|
||||
@DeleteMapping("/telemetry/{deviceSn}")
|
||||
public ResponseEntity<Void> disconnect(
|
||||
@PathVariable String deviceSn,
|
||||
@RequestHeader(value = "X-Client-Session-Id", required = false) String clientSessionId) {
|
||||
if (clientSessionId != null) {
|
||||
String connectionId = "client_" + clientSessionId + "_" + deviceSn;
|
||||
removeConnection(connectionId, deviceSn);
|
||||
}
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "客户端断开SSE连接(Beacon)")
|
||||
@PostMapping("/telemetry/{deviceSn}/disconnect")
|
||||
public ResponseEntity<Void> disconnectBeacon(
|
||||
@PathVariable String deviceSn,
|
||||
@RequestBody(required = false) Map<String, String> body) {
|
||||
String sessionId = body != null ? body.get("sessionId") : null;
|
||||
if (sessionId != null) {
|
||||
String connectionId = "client_" + sessionId + "_" + deviceSn;
|
||||
removeConnection(connectionId, deviceSn);
|
||||
}
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
private void sendTelemetryData(String connectionId, BoatRealtimeVO data) {
|
||||
SseEmitter emitter = emitters.get(connectionId);
|
||||
if (emitter == null) return;
|
||||
try {
|
||||
emitter.send(SseEmitter.event()
|
||||
.name("boat-telemetry")
|
||||
.data(JSON.toJSONString(data)));
|
||||
} catch (IOException e) {
|
||||
log.warn("[ClientSSE] 发送遥测数据失败: connectionId={}, error={}", connectionId, e.getMessage());
|
||||
removeConnection(connectionId, data.getDeviceSn());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean removeConnection(String connectionId, String deviceSn) {
|
||||
SseEmitter emitter = emitters.remove(connectionId);
|
||||
if (emitter != null) {
|
||||
try {
|
||||
emitter.complete();
|
||||
} catch (Exception e) {
|
||||
// 忽略
|
||||
}
|
||||
}
|
||||
boatTrackService.unregisterTelemetryCallback(deviceSn, connectionId);
|
||||
return emitter != null;
|
||||
}
|
||||
}
|
||||
@@ -113,3 +113,10 @@ sse:
|
||||
heartbeat:
|
||||
enabled: true
|
||||
interval: 30000 # 心跳间隔(ms)
|
||||
|
||||
# 客户端接口放行(无需认证)
|
||||
zlt:
|
||||
security:
|
||||
ignore:
|
||||
httpUrls:
|
||||
- /client/**
|
||||
|
||||
+37
-1
@@ -25,6 +25,8 @@ import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -220,15 +222,49 @@ public class MediaServiceImpl implements IMediaService {
|
||||
|
||||
/**
|
||||
* 基于模板配置构建clips参数
|
||||
* fileIndex 匹配文件路径中的拍照顺序号(如 DJI_20260311104552_0014_V.mp4 中的 0014 → 14)
|
||||
*/
|
||||
private String buildClipsParamFromConfig(List<OrderFile> videoFiles, ClipsConfig config) {
|
||||
Map<String, String> clips = new HashMap<>();
|
||||
for (ClipsConfig.ClipMapping m : config.getMappings()) {
|
||||
clips.put(m.getSlot(), getUrlBySafeIndex(videoFiles, m.getFileIndex()));
|
||||
clips.put(m.getSlot(), getUrlBySequenceNumber(videoFiles, m.getFileIndex()));
|
||||
}
|
||||
return JSON.toJSONString(clips);
|
||||
}
|
||||
|
||||
/** 从文件路径中提取拍照顺序号,如 DJI_20260311104552_0014_V.mp4 → 14 */
|
||||
private static final Pattern SEQUENCE_PATTERN = Pattern.compile("DJI_\\d+_(\\d+)_");
|
||||
|
||||
private int extractSequenceNumber(String filePath) {
|
||||
if (filePath == null) {
|
||||
return -1;
|
||||
}
|
||||
// 取文件名部分,避免匹配到目录名中的序号
|
||||
int lastSlash = filePath.lastIndexOf('/');
|
||||
String fileName = (lastSlash >= 0) ? filePath.substring(lastSlash + 1) : filePath;
|
||||
Matcher matcher = SEQUENCE_PATTERN.matcher(fileName);
|
||||
if (matcher.find()) {
|
||||
return Integer.parseInt(matcher.group(1));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置的 fileIndex 匹配素材路径中的拍照顺序号,找到对应文件
|
||||
*/
|
||||
private String getUrlBySequenceNumber(List<OrderFile> videoFiles, int fileIndex) {
|
||||
if (videoFiles == null || videoFiles.isEmpty() || fileIndex < 0) {
|
||||
return "";
|
||||
}
|
||||
for (OrderFile file : videoFiles) {
|
||||
if (extractSequenceNumber(file.getFilePath()) == fileIndex) {
|
||||
return fileCenterService.getViewUrlByPath(file.getFilePath());
|
||||
}
|
||||
}
|
||||
log.warn("未找到拍照顺序号={}的素材文件", fileIndex);
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认剪辑参数构建(原有硬编码逻辑,作为兜底)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user