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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user