diff --git a/zlt-business/device-center/src/main/java/com/central/device/controller/DeviceCommandController.java b/zlt-business/device-center/src/main/java/com/central/device/controller/DeviceCommandController.java index 99357b43..414d25b4 100644 --- a/zlt-business/device-center/src/main/java/com/central/device/controller/DeviceCommandController.java +++ b/zlt-business/device-center/src/main/java/com/central/device/controller/DeviceCommandController.java @@ -141,7 +141,6 @@ public class DeviceCommandController { if (request.getTargetLatitude() == null || request.getTargetLongitude() == null) { return Result.failed("目标点经纬度不能为空"); } - // 构建参数 JSONObject params = new JSONObject(); params.put("target_latitude", request.getTargetLatitude()); @@ -157,7 +156,7 @@ public class DeviceCommandController { params.put("flight_id", request.getFlightId() != null ? request.getFlightId() : java.util.UUID.randomUUID().toString().replace("-", "").toUpperCase()); params.put("max_speed", request.getMaxSpeed() != null ? request.getMaxSpeed() : 5); params.put("flight_safety_advance_check", request.getFlightSafetyAdvanceCheck() != null ? request.getFlightSafetyAdvanceCheck() : 0); - + params.put("stream_urls", getStreamUrls(request.getDeviceSn())); String commandId = commandManager.sendCommand( request.getDeviceSn(), "takeoff_to_point", @@ -550,7 +549,7 @@ public class DeviceCommandController { JSONObject result = new JSONObject(); AgeDevice device = iAgeDeviceService.getByDeviceSn(deviceId); if(Objects.nonNull(device)){ - result.put("pushUrl", device.getVideoStreamUrl()); + result.put("pushUrl", device.getPushStreamUrl()); } result.put("playRtmpUrl", ""); result.put("playFlvUrl", ""); diff --git a/zlt-business/device-center/src/main/java/com/central/device/controller/SseAdminController.java b/zlt-business/device-center/src/main/java/com/central/device/controller/SseAdminController.java new file mode 100644 index 00000000..54326802 --- /dev/null +++ b/zlt-business/device-center/src/main/java/com/central/device/controller/SseAdminController.java @@ -0,0 +1,78 @@ +package com.central.device.controller; + +import com.central.device.sse.manager.SseEmitterManager; +import com.central.device.websocket.manager.SubscriptionPoolService; +import com.central.common.model.Result; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +/** + * SSE 连接管理控制器 + * 提供管理员手动管理 SSE 连接和 WebSocket 订阅的接口 + * + * @author dzc + * @date 2025-01-20 + */ +@Slf4j +@RestController +@RequestMapping("/admin/sse") +@Tag(name = "SSE管理接口", description = "管理员手动管理SSE连接和订阅") +public class SseAdminController { + + @Autowired + private SseEmitterManager sseEmitterManager; + + @Autowired + private SubscriptionPoolService subscriptionPoolService; + + /** + * 查看当前连接状态 + */ + @GetMapping("/status") + @Operation(summary = "查看SSE连接状态", description = "获取当前所有SSE连接和WebSocket订阅状态") + public Result> getStatus() { + Map status = sseEmitterManager.getConnectionStatus(); + + // 打印 Redis 订阅状态 + subscriptionPoolService.printSubscriptionStatus(); + + return Result.succeed(status, "查询成功"); + } + + /** + * 强制断开所有连接 + */ + @PostMapping("/disconnect-all") + @Operation(summary = "断开所有SSE连接", description = "强制断开所有SSE连接,清理Redis订阅,关闭WebSocket") + public Result disconnectAll() { + log.warn("【管理员操作】收到断开所有连接请求"); + + int disconnected = sseEmitterManager.disconnectAll(); + + String msg = String.format("已断开 %d 个SSE连接,已清理Redis订阅,已关闭WebSocket", disconnected); + log.info(msg); + + return Result.succeed(msg); + } + + /** + * 仅清理 Redis 订阅(不断开 SSE) + */ + @PostMapping("/clear-redis") + @Operation(summary = "清理Redis订阅", description = "仅清理Redis中的订阅记录,不断开SSE连接") + public Result clearRedis() { + log.warn("【管理员操作】收到清理Redis订阅请求"); + + int cleared = subscriptionPoolService.clearAllSubscriptions(); + + String msg = String.format("已清理 %d 个Redis订阅记录", cleared); + log.info(msg); + + return Result.succeed(msg); + } +} diff --git a/zlt-business/device-center/src/main/java/com/central/device/model/AgeDevice.java b/zlt-business/device-center/src/main/java/com/central/device/model/AgeDevice.java index 8cfc4312..eec8da07 100644 --- a/zlt-business/device-center/src/main/java/com/central/device/model/AgeDevice.java +++ b/zlt-business/device-center/src/main/java/com/central/device/model/AgeDevice.java @@ -50,4 +50,9 @@ public class AgeDevice extends SuperEntity { * 视频流地址(完整WHEP URL,如:http://220.205.16.27:8024/rtc/v1/whep/?app=live&stream=49214ac552fa) */ private String videoStreamUrl; + + /** + * 设备推送视频流地址(完整whip URL,如:http://220.205.16.27:8024/rtc/v1/whip/?app=live&stream=48622d43e975&secret=1F8F64FE2A7447A7) + */ + private String pushStreamUrl; } diff --git a/zlt-business/device-center/src/main/java/com/central/device/sse/manager/SseEmitterManager.java b/zlt-business/device-center/src/main/java/com/central/device/sse/manager/SseEmitterManager.java index 0be1b733..ca6512a9 100644 --- a/zlt-business/device-center/src/main/java/com/central/device/sse/manager/SseEmitterManager.java +++ b/zlt-business/device-center/src/main/java/com/central/device/sse/manager/SseEmitterManager.java @@ -529,6 +529,77 @@ public class SseEmitterManager { return deviceRefCount.getOrDefault(deviceSn, 0); } + /** + * 【管理功能】强制断开所有 SSE 连接并清理所有订阅 + * 用于管理员手动清理资源 + * + * @return 断开的连接数 + */ + public int disconnectAll() { + int count = emitters.size(); + log.warn("【管理操作】强制断开所有 SSE 连接,当前连接数: {}", count); + + // 复制一份 key 列表,避免并发修改 + java.util.List sessionIds = new java.util.ArrayList<>(emitters.keySet()); + + for (String sessionId : sessionIds) { + EmitterWrapper wrapper = emitters.get(sessionId); + if (wrapper != null) { + try { + // 完成 SSE 连接(会触发 onCompletion 回调进行清理) + wrapper.getEmitter().complete(); + } catch (Exception e) { + log.debug("完成 SSE 连接失败: {}", e.getMessage()); + } + } + } + + // 清理 Redis 订阅池 + int redisCleared = subscriptionPoolService.clearAllSubscriptions(); + log.info("Redis 订阅池已清理: {} 个 key", redisCleared); + + // 清理本地引用计数 + deviceRefCount.clear(); + + // 断开 WebSocket 连接 + jetLinksWebSocketClient.closeManually(); + log.info("WebSocket 连接已关闭"); + + return count; + } + + /** + * 【管理功能】获取当前所有 SSE 连接状态 + * + * @return 连接状态信息 + */ + public java.util.Map getConnectionStatus() { + java.util.Map status = new java.util.HashMap<>(); + status.put("totalConnections", emitters.size()); + status.put("deviceRefCounts", new java.util.HashMap<>(deviceRefCount)); + + // 按设备分组统计 + java.util.Map deviceConnections = new java.util.HashMap<>(); + java.util.Map droneConnections = new java.util.HashMap<>(); + + emitters.forEach((sessionId, wrapper) -> { + String deviceSn = wrapper.getDeviceSn(); + boolean isDrone = wrapper.getIsDroneTelemetry() != null && wrapper.getIsDroneTelemetry(); + + if (isDrone) { + droneConnections.merge(deviceSn, 1, Integer::sum); + } else { + deviceConnections.merge(deviceSn, 1, Integer::sum); + } + }); + + status.put("deviceConnections", deviceConnections); + status.put("droneConnections", droneConnections); + status.put("websocketConnected", jetLinksWebSocketClient.isConnected()); + + return status; + } + /** * 订阅无人机遥测数据(msg_alarm) * 进入操控台时调用,开始接收无人机实时遥测数据 diff --git a/zlt-business/device-center/src/main/java/com/central/device/websocket/manager/DeviceCommandManager.java b/zlt-business/device-center/src/main/java/com/central/device/websocket/manager/DeviceCommandManager.java index 88b51070..c0b927cf 100644 --- a/zlt-business/device-center/src/main/java/com/central/device/websocket/manager/DeviceCommandManager.java +++ b/zlt-business/device-center/src/main/java/com/central/device/websocket/manager/DeviceCommandManager.java @@ -208,6 +208,10 @@ public class DeviceCommandManager { data.put("flight_id", params.getString("flight_id")); data.put("max_speed", params.getIntValue("max_speed")); data.put("flight_safety_advance_check", params.getIntValue("flight_safety_advance_check")); + // 推流地址 + if (params.containsKey("stream_urls")) { + data.put("stream_urls", params.get("stream_urls")); + } break; case "return_home": diff --git a/zlt-business/device-center/doc/DEVICE_COMMAND_API.md b/zlt-business/device-center/src/main/resources/doc/DEVICE_COMMAND_API.md similarity index 100% rename from zlt-business/device-center/doc/DEVICE_COMMAND_API.md rename to zlt-business/device-center/src/main/resources/doc/DEVICE_COMMAND_API.md diff --git a/zlt-business/device-center/doc/WebSocket懒加载使用说明.md b/zlt-business/device-center/src/main/resources/doc/WebSocket懒加载使用说明.md similarity index 100% rename from zlt-business/device-center/doc/WebSocket懒加载使用说明.md rename to zlt-business/device-center/src/main/resources/doc/WebSocket懒加载使用说明.md diff --git a/zlt-business/device-center/doc/WebSocket消息处理说明.md b/zlt-business/device-center/src/main/resources/doc/WebSocket消息处理说明.md similarity index 100% rename from zlt-business/device-center/doc/WebSocket消息处理说明.md rename to zlt-business/device-center/src/main/resources/doc/WebSocket消息处理说明.md diff --git a/zlt-business/device-center/doc/指令飞行.md b/zlt-business/device-center/src/main/resources/doc/指令飞行.md similarity index 100% rename from zlt-business/device-center/doc/指令飞行.md rename to zlt-business/device-center/src/main/resources/doc/指令飞行.md diff --git a/zlt-business/device-center/doc/无人机航线管理与轨迹显示实现文档.md b/zlt-business/device-center/src/main/resources/doc/无人机航线管理与轨迹显示实现文档.md similarity index 100% rename from zlt-business/device-center/doc/无人机航线管理与轨迹显示实现文档.md rename to zlt-business/device-center/src/main/resources/doc/无人机航线管理与轨迹显示实现文档.md diff --git a/zlt-business/device-center/doc/景区地图仪表盘-后端API需求.md b/zlt-business/device-center/src/main/resources/doc/景区地图仪表盘-后端API需求.md similarity index 100% rename from zlt-business/device-center/doc/景区地图仪表盘-后端API需求.md rename to zlt-business/device-center/src/main/resources/doc/景区地图仪表盘-后端API需求.md diff --git a/zlt-business/device-center/doc/景区地图仪表盘-开发进度.md b/zlt-business/device-center/src/main/resources/doc/景区地图仪表盘-开发进度.md similarity index 100% rename from zlt-business/device-center/doc/景区地图仪表盘-开发进度.md rename to zlt-business/device-center/src/main/resources/doc/景区地图仪表盘-开发进度.md diff --git a/zlt-business/device-center/doc/景区地图仪表盘功能实现文档.md b/zlt-business/device-center/src/main/resources/doc/景区地图仪表盘功能实现文档.md similarity index 100% rename from zlt-business/device-center/doc/景区地图仪表盘功能实现文档.md rename to zlt-business/device-center/src/main/resources/doc/景区地图仪表盘功能实现文档.md diff --git a/zlt-business/device-center/doc/景区设备状态实时监控实现方案-v2.md b/zlt-business/device-center/src/main/resources/doc/景区设备状态实时监控实现方案-v2.md similarity index 100% rename from zlt-business/device-center/doc/景区设备状态实时监控实现方案-v2.md rename to zlt-business/device-center/src/main/resources/doc/景区设备状态实时监控实现方案-v2.md diff --git a/zlt-business/device-center/doc/景区设备状态实时监控实现方案.md b/zlt-business/device-center/src/main/resources/doc/景区设备状态实时监控实现方案.md similarity index 100% rename from zlt-business/device-center/doc/景区设备状态实时监控实现方案.md rename to zlt-business/device-center/src/main/resources/doc/景区设备状态实时监控实现方案.md diff --git a/zlt-business/device-center/sql/3d_model_file.sql b/zlt-business/device-center/src/main/resources/sql/3d_model_file.sql similarity index 100% rename from zlt-business/device-center/sql/3d_model_file.sql rename to zlt-business/device-center/src/main/resources/sql/3d_model_file.sql diff --git a/zlt-business/device-center/src/main/resources/sql/save_column.sql b/zlt-business/device-center/src/main/resources/sql/save_column.sql new file mode 100644 index 00000000..a68e759c --- /dev/null +++ b/zlt-business/device-center/src/main/resources/sql/save_column.sql @@ -0,0 +1 @@ +ALTER TABLE `manage_device` ADD COLUMN `push_stream_url` varchar(500) NULL COMMENT '设备推流地址' AFTER `video_stream_url`; \ No newline at end of file diff --git a/zlt-web/react-web-next/dist.zip b/zlt-web/react-web-next/dist.zip index 81250a15..861b1a37 100644 Binary files a/zlt-web/react-web-next/dist.zip and b/zlt-web/react-web-next/dist.zip differ diff --git a/zlt-web/react-web-next/src/pages/device/Device/components/DeviceForm.tsx b/zlt-web/react-web-next/src/pages/device/Device/components/DeviceForm.tsx index 3d37c0c5..22149416 100644 --- a/zlt-web/react-web-next/src/pages/device/Device/components/DeviceForm.tsx +++ b/zlt-web/react-web-next/src/pages/device/Device/components/DeviceForm.tsx @@ -61,6 +61,13 @@ const DeviceForm: React.FC = ({ title, visible, onVisibleChange placeholder="如:http://220.205.16.27:8024/rtc/v1/whep/?app=live&stream=xxx" tooltip="WHEP视频流完整地址,用于无人机图传播放" /> + diff --git a/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDeviceSSE.ts b/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDeviceSSE.ts index 3d1938de..2f3c003f 100644 --- a/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDeviceSSE.ts +++ b/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDeviceSSE.ts @@ -1,7 +1,7 @@ import { useState, useRef, useEffect, useCallback } from 'react'; -import { message } from 'antd'; +import { notification } from 'antd'; -// 空闲超时时间:30分钟(毫秒) +// 空闲超时时间:1分钟(测试用,正式环境改为30分钟) const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // GPS信息 @@ -182,12 +182,21 @@ export function useDeviceSSE(): UseDeviceSSEResult { console.log('[DeviceSSE] 连接已断开'); }, []); - // 因空闲断开连接 + // 因空闲断开连接并显示提醒 const disconnectDueToIdle = useCallback(() => { - console.log('[DeviceSSE] 页面空闲超过30分钟,断开SSE连接以节省资源'); + console.log('[DeviceSSE] 页面空闲超过1分钟,断开SSE连接'); setIsIdleDisconnected(true); + // 禁止自动重连 + shouldReconnectRef.current = false; + // 断开连接 disconnectSSE(); - message.warning('页面空闲超过30分钟,已断开实时数据连接,请刷新页面重新连接'); + // 显示提醒,使用统一的 key 避免重复 + notification.warning({ + message: '页面长时间未操作', + description: '已断开实时数据连接,请刷新页面重新连接', + duration: 0, + key: 'page-idle-warning', + }); }, [disconnectSSE]); // 重置空闲定时器 @@ -454,4 +463,4 @@ export function useDeviceSSE(): UseDeviceSSEResult { }; } -export default useDeviceSSE; \ No newline at end of file +export default useDeviceSSE; diff --git a/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDroneSSE.ts b/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDroneSSE.ts index faf2e2c3..3b1de545 100644 --- a/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDroneSSE.ts +++ b/zlt-web/react-web-next/src/pages/device/mapDashboard/components/MapContainer/3d/hooks/useDroneSSE.ts @@ -1,7 +1,7 @@ import { useState, useRef, useEffect, useCallback } from 'react'; -import { message } from 'antd'; +import { notification } from 'antd'; -// 空闲超时时间:30分钟(毫秒) +// 空闲超时时间:1分钟(测试用,正式环境改为30分钟) const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 无人机遥测数据接口定义(从useDeviceSSE.ts复制) @@ -185,12 +185,21 @@ export const useDroneSSE = (): UseDroneSSEReturn => { reconnectAttemptsRef.current = 0; }, []); - // 因空闲断开连接 + // 因空闲断开连接并显示提醒 const disconnectDueToIdle = useCallback(() => { - console.log('[DroneSSE] 页面空闲超过30分钟,断开SSE连接以节省资源'); + console.log('[DroneSSE] 页面空闲超过1分钟,断开SSE连接'); setIsIdleDisconnected(true); + // 禁止自动重连 + shouldReconnectRef.current = false; + // 断开连接 disconnectDroneSSE(); - message.warning('页面空闲超过30分钟,已断开无人机数据连接,请刷新页面重新连接'); + // 显示提醒,使用统一的 key 避免重复 + notification.warning({ + message: '页面长时间未操作', + description: '已断开实时数据连接,请刷新页面重新连接', + duration: 0, + key: 'page-idle-warning', + }); }, [disconnectDroneSSE]); // 重置空闲定时器 @@ -488,4 +497,4 @@ export const useDroneSSE = (): UseDroneSSEReturn => { disconnectDroneSSE, resetDroneData, }; -}; \ No newline at end of file +};