From f0ed143646e3c280416ca05f64c1b59604289928 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Mon, 20 Oct 2025 10:52:48 +0800
Subject: [PATCH] websocket无人机控制者和视频流数据增加boxSn
---
ard-work/src/main/java/com/ard/work/component/websocket/handler/UavStreamManager.java | 19 +++------
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java | 4 +-
ard-work/src/main/java/com/ard/work/domain/receiver/BoxStreamUrlReceiver.java | 2 +
ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java | 21 ++++++----
ard-work/src/main/java/com/ard/work/component/websocket/handler/UavControlManager.java | 7 ++-
ard-work/src/main/java/com/ard/work/component/websocket/handler/BaseSubscriptionManager.java | 9 ++--
6 files changed, 32 insertions(+), 30 deletions(-)
diff --git a/ard-work/src/main/java/com/ard/work/component/websocket/handler/BaseSubscriptionManager.java b/ard-work/src/main/java/com/ard/work/component/websocket/handler/BaseSubscriptionManager.java
index d9ffafd..9482d18 100644
--- a/ard-work/src/main/java/com/ard/work/component/websocket/handler/BaseSubscriptionManager.java
+++ b/ard-work/src/main/java/com/ard/work/component/websocket/handler/BaseSubscriptionManager.java
@@ -23,10 +23,10 @@
/**
* 订阅指定设备的数据
*/
- public void subscribe(String boxSn, Long userId,Integer period) {
+ public void subscribe(String boxSn, Long userId, Integer period) {
subscriptions.computeIfAbsent(boxSn, k -> new HashSet<>()).add(userId);
if (subscriptions.get(boxSn).size() == 1) {
- startPushTask(boxSn,period);
+ startPushTask(boxSn, period);
}
}
@@ -46,7 +46,7 @@
/**
* 启动推送任务
*/
- private void startPushTask(String boxSn,Integer period) {
+ private void startPushTask(String boxSn, Integer period) {
// 如果已经存在任务,则不再创建
if (pushTasks.containsKey(boxSn)) {
return;
@@ -79,7 +79,6 @@
}
-
/**
* 向订阅者发送数据
*/
@@ -90,7 +89,7 @@
if (userIds != null) {
for (Long userId : userIds) {
try {
- log.debug("给用户{}定时推送",userId);
+ log.debug("给用户{}定时推送", userId);
WebSocketUtil.sendMessage(userId, JSON.toJSONString(baseMessage));
} catch (Exception e) {
log.error("Error sending message to session {}", userId, e);
diff --git a/ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java b/ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
index ca41681..b7e380a 100644
--- a/ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
+++ b/ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
@@ -192,8 +192,8 @@
}
/**
- * 处理无人机控制
- * 根据接收到的消息内容,决定无人机控制信息的订阅或退订
+ * 处理无人机控制者
+ * 根据接收到的消息内容,决定无人机控制者信息的订阅或退订
*
* @param payload 消息负载,包含控制信息
* @param userId 用户ID,用于标识用户
diff --git a/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavControlManager.java b/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavControlManager.java
index 863171a..4e56d46 100644
--- a/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavControlManager.java
+++ b/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavControlManager.java
@@ -12,7 +12,7 @@
import java.util.Map;
@Service
-public class UavControlManager extends BaseSubscriptionManager<Map<String, Long>> {
+public class UavControlManager extends BaseSubscriptionManager<Map<String, Object>> {
@Resource
private UavControlService uavControlService;
@@ -60,13 +60,14 @@
* @return 返回一个映射,包含无人机(fly)和载荷(payload)的当前控制者用户ID
*/
@Override
- protected Map<String, Long> getData(String boxSn) {
+ protected Map<String, Object> getData(String boxSn) {
// 获取当前控制飞行的用户ID
Long flyUserId = uavControlService.getCurrentController(boxSn, "fly");
// 获取当前控制载荷的用户ID
Long payloadUserId = uavControlService.getCurrentController(boxSn, "payload");
// 创建一个映射,用于存储飞行和载荷的当前控制者用户ID
- Map<String, Long> map=new HashMap<>();
+ Map<String, Object> map=new HashMap<>();
+ map.put("boxSn", boxSn);
// 将飞行当前控制者用户ID添加到映射中
map.put("flyController", flyUserId);
// 将载荷当前控制者用户ID添加到映射中
diff --git a/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavStreamManager.java b/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavStreamManager.java
index e24af6f..f0b0362 100644
--- a/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavStreamManager.java
+++ b/ard-work/src/main/java/com/ard/work/component/websocket/handler/UavStreamManager.java
@@ -42,14 +42,15 @@
public void subscribe(String boxSn, Long userId) {
STREAM_USER_MAP.computeIfAbsent(boxSn, k -> ConcurrentHashMap.newKeySet()).add(userId);
- String webrtcUrl = getWebrtcUrl(boxSn);
+ BoxStreamUrlReceiver streamReceiver = getWebrtcUrl(boxSn);
+ String webrtcUrl = streamReceiver.getRtc();
if (Objects.equals(webrtcUrl, "")) {
log.warn("{}{}{}", STREAM_LOG_PREFIX, STREAM_NOT_FOUND_MSG, boxSn);
BaseMessage<String> baseMessage = new BaseMessage<>("stream", STREAM_NOT_FOUND_MSG + boxSn);
sendMessageToUser(userId, JSON.toJSONString(baseMessage));
}
BOX_STREAM_PULL_MAP.put(boxSn, webrtcUrl);
- BaseMessage<String> baseMessage = new BaseMessage<>("stream", webrtcUrl);
+ BaseMessage<BoxStreamUrlReceiver> baseMessage = new BaseMessage<>("stream", streamReceiver);
sendMessageToUser(userId, JSON.toJSONString(baseMessage));
}
@@ -117,15 +118,9 @@
* @param boxSn 盒子编号
* @return String webrtcUrl
*/
- private String getWebrtcUrl(String boxSn) {
- try {
- BoxStreamUrlReceiver boxLiveUrl = tyService.getBoxLiveUrl(boxSn);
- if (boxLiveUrl != null) {
- return boxLiveUrl.getRtc();
- }
- } catch (Exception e) {
- log.error("获取媒体地址异常:{}", e.getMessage());
- }
- return "";
+ private BoxStreamUrlReceiver getWebrtcUrl(String boxSn) {
+ BoxStreamUrlReceiver boxLiveUrl = tyService.getBoxLiveUrl(boxSn);
+ boxLiveUrl.setBoxSn(boxSn);
+ return boxLiveUrl;
}
}
\ No newline at end of file
diff --git a/ard-work/src/main/java/com/ard/work/domain/receiver/BoxStreamUrlReceiver.java b/ard-work/src/main/java/com/ard/work/domain/receiver/BoxStreamUrlReceiver.java
index 58dd24d..b70b9d6 100644
--- a/ard-work/src/main/java/com/ard/work/domain/receiver/BoxStreamUrlReceiver.java
+++ b/ard-work/src/main/java/com/ard/work/domain/receiver/BoxStreamUrlReceiver.java
@@ -8,6 +8,8 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class BoxStreamUrlReceiver {
+ private String boxSn;
+
@JsonProperty("rtsp")
private String rtsp;
diff --git a/ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java b/ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java
index f891969..f872a47 100644
--- a/ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java
@@ -52,6 +52,7 @@
import com.ard.work.service.ITyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
+
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@@ -93,6 +94,7 @@
List<ArdServerInfo> ardServerInfos = ardServerService.selectArdServerInfoList(new ArdServerInfo());
return getBoxList(ardServerInfos);
}
+
/**
* 获取指定服务器云盒列表
*
@@ -103,6 +105,7 @@
TyHttpCommonReceiver<List<BoxInfoReceiver>> boxList = tyClient.getBoxList(serverId);
return boxList.getContent();
}
+
/**
* 获取直播地址
*
@@ -111,12 +114,13 @@
@Override
public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSn);
- if(ardDroneInfo== null){
- log.warn("未找到对应的飞行器信息,boxSn:{}",boxSn);
+ if (ardDroneInfo == null) {
+ log.warn("未找到对应的飞行器信息,boxSn:{}", boxSn);
return null;
}
- TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(ardDroneInfo.getServerId(),boxSn);
- return boxLiveUrl.getContent();
+ TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(ardDroneInfo.getServerId(),
+ boxSn);
+ return boxLiveUrl == null ? null : boxLiveUrl.getContent();
}
/**
@@ -140,7 +144,8 @@
@Override
public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxPhotosDTO.getBoxSn());
- TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(ardDroneInfo.getServerId(),boxPhotosDTO);
+ TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(ardDroneInfo.getServerId(),
+ boxPhotosDTO);
return boxPhotos.getContent();
}
@@ -150,7 +155,7 @@
@Override
public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSettingsDTO.getBoxSn());
- TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(ardDroneInfo.getServerId(),boxSettingsDTO);
+ TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(ardDroneInfo.getServerId(), boxSettingsDTO);
return result.getState() == 1;
}
@@ -673,8 +678,8 @@
break;
case 0x02:
// 处理注册消息
- HeartbeatReplyCmd heartbeatReplyCmd = (HeartbeatReplyCmd) tyCommand;
- logPrint("心跳消息", heartbeatReplyCmd.getTimestamp());
+ HeartbeatReplyCmd heartbeatReplyCmd = (HeartbeatReplyCmd) tyCommand;
+ logPrint("心跳消息", heartbeatReplyCmd.getTimestamp());
break;
case 0x03:
// 处理订阅消息
--
Gitblit v1.9.3