From 3f43e40ed1a1e9f66b3ad2862da9dbc4b710c712 Mon Sep 17 00:00:00 2001
From: nishiwei <13126973358@163.com>
Date: Fri, 23 Oct 2026 14:09:47 +0800
Subject: [PATCH] 智云3.0机场1适配内容提交

---
 ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java |  110 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 95 insertions(+), 15 deletions(-)

diff --git a/ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java b/ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java
index 1d4fa0a..58c93c6 100644
--- a/ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java
+++ b/ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java
@@ -20,6 +20,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 
 @Slf4j
@@ -51,32 +52,99 @@
             //String key = "online:" + device.getDeviceSn();
             //System.out.println("在线状态:"+RedisOpsUtils.checkExist(key));
             // 更新机场设备状态:"3"-"设备已离线",
-            if(!_deviceService.checkDeviceOnline(device.getDeviceSn())){
+            if (!_deviceService.checkDeviceOnline(device.getDeviceSn())) {
                 _deviceService.updateWorkType(device.getDeviceSn(), 3L);
                 RedisOpsUtils.set(RedisConst.OSD_STATUS_PREFIX + device.getDeviceSn(), 3);
                 RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + device.getDeviceSn());
                 RedisOpsUtils.del(RedisConst.COVER_STATE_PREFIX + device.getDeviceSn());
-            }
-            else {
-                StartLiveStreaming(device.getDeviceSn());
-            }
-            if(!_deviceService.checkDeviceOnline(device.getChildDeviceSn())){
-                _deviceService.updateWorkType(device.getChildDeviceSn(), 6L);
-                RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + device.getChildDeviceSn());
-                RedisOpsUtils.del(RedisConst.COVER_STATE_PREFIX + device.getChildDeviceSn());
-            }
-            else{
-                StartLiveStreaming(device.getChildDeviceSn());
+                clearLiveRetry(device.getDeviceSn());
+            } else {
+                tryStartLiveStreaming(device.getDeviceSn());
             }
 
+            // 机场1:未绑定飞行器时 childDeviceSn 为空,跳过,避免无效的在线判断和起流
+            String childDeviceSn = device.getChildDeviceSn();
+            if (childDeviceSn == null || childDeviceSn.isEmpty()) {
+                continue;
+            }
+            if (!_deviceService.checkDeviceOnline(childDeviceSn)) {
+                _deviceService.updateWorkType(childDeviceSn, 6L);
+                RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + childDeviceSn);
+                RedisOpsUtils.del(RedisConst.COVER_STATE_PREFIX + childDeviceSn);
+                clearLiveRetry(childDeviceSn);
+            } else if (isDronePoweredOff(childDeviceSn)) {
+                // 飞行器舱内关机(6)或开机中(7)时相机尚未就绪,起流必然失败(如 513006),直接跳过
+                clearLiveRetry(childDeviceSn);
+            } else {
+                tryStartLiveStreaming(childDeviceSn);
+            }
         }
+    }
+
+    /**
+     * 按退避策略尝试开启直播。
+     * 原实现每 2 秒对未就绪设备执行一次“停-开”,开机阶段会刷出大量 513006/613011 日志;
+     * 这里失败后按 2s、4s、8s、16s、30s 递增间隔重试。
+     */
+    private void tryStartLiveStreaming(String sn) {
+        if (!canRetryLive(sn)) {
+            return;
+        }
+        markLiveRetry(sn, StartLiveStreaming(sn));
+    }
+
+    /**
+     * 飞行器是否处于关机或开机中状态。
+     * 6-舱内关机(机场 OSD 写入),7-开机中(下发 drone_open 时写入)。
+     */
+    private boolean isDronePoweredOff(String childDeviceSn) {
+        Object status = RedisOpsUtils.get(RedisConst.OSD_STATUS_PREFIX + childDeviceSn);
+        if (status == null) {
+            return false;
+        }
+        String value = String.valueOf(status);
+        return "6".equals(value) || "7".equals(value);
+    }
+
+    /**
+     * 连续失败次数与下次允许重试的时间戳。
+     */
+    private static class LiveRetryState {
+        private int failures;
+        private long nextRetryTime;
+    }
+
+    private static final long LIVE_RETRY_BASE_DELAY_MS = 2000L;
+    private static final long LIVE_RETRY_MAX_DELAY_MS = 30000L;
+
+    private final Map<String, LiveRetryState> liveRetryStates = new ConcurrentHashMap<>();
+
+    private boolean canRetryLive(String sn) {
+        LiveRetryState state = liveRetryStates.get(sn);
+        return state == null || System.currentTimeMillis() >= state.nextRetryTime;
+    }
+
+    private void markLiveRetry(String sn, boolean success) {
+        if (success) {
+            liveRetryStates.remove(sn);
+            return;
+        }
+        LiveRetryState state = liveRetryStates.computeIfAbsent(sn, k -> new LiveRetryState());
+        state.failures++;
+        long delay = Math.min(LIVE_RETRY_BASE_DELAY_MS << Math.min(state.failures - 1, 4), LIVE_RETRY_MAX_DELAY_MS);
+        state.nextRetryTime = System.currentTimeMillis() + delay;
+        log.info("SN: {}-直播启动失败(第 {} 次),{} 秒后重试", sn, state.failures, delay / 1000);
+    }
+
+    private void clearLiveRetry(String sn) {
+        liveRetryStates.remove(sn);
     }
 
     /**
      * 定时任务,每1秒读一次redis的机场设备的状态,并发送到websocket客户端
      * */
     //@Scheduled(cron = "0/1 * * * * ?")
-    @Scheduled(initialDelay = 2, fixedRate = 1, timeUnit = TimeUnit.SECONDS)
+    @Scheduled(initialDelay = 2, fixedRate = 3, timeUnit = TimeUnit.SECONDS)
     public void sendDeviceStatus(){
         // 测试 机场oss读取文件
 //        URL url = _ossService.getObjectUrl("kmz","/2025/11/25/新建航点航线(1)_20251125161322A001.kmz");
@@ -126,7 +194,12 @@
         return jsonString;
     }
 
-    public void StartLiveStreaming(String sn) {
+    /**
+     * 开启直播(后台定时任务调用)。
+     *
+     * @return true-已存在可用地址或起流成功;false-起流失败
+     */
+    public boolean StartLiveStreaming(String sn) {
         //调用开始直播
         ResponseResult rlive = null;
         String resultLive = "";
@@ -136,12 +209,13 @@
             // 判断param是否为空,不为空则返回正确的直播参数,为null则未获取直播参数,不为空则继续操作
             if (param == null) {
                 log.info("SN: {}-未获取到直播参数param为null,无法开启直播!", sn);
-                return;
+                return false;
             }
             // 如果存在直播则读取直播地址返回
             if (RedisOpsUtils.checkExist(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn)) {
                 resultLive = String.valueOf(RedisOpsUtils.get(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn));
 //                log.info("SN: {}-redis中存在相机的播放地址信息,直接返回地址!", sn);
+                return true;
             } else {
 //                log.info("SN: {}-redis中未找到相机的播放地址信息!停止直播后再开启直播", sn);
                 _liveStreamHandler.StopLiveStreaming(sn);
@@ -159,11 +233,17 @@
 //                    if (isWeb) {
 //                        log.info("SN: {}-更新webrtc地址成功!", sn);
 //                    }
+                    // 定时任务是后台自动起流,前端并没有发订阅,必须主动把地址推给已订阅该设备的页面;
+                    // 否则飞行器起飞后相机就绪、后台起流成功,页面却收不到地址,只能刷新后才出画面
+                    _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive, "");
+                    return true;
                 }
+                log.info("SN: {}-开启直播失败!返回值:{}", sn, rlive);
             }
         } catch (Exception e) {
             log.info("订时任务推流-SN: {}-开启直播失败!原因:{}", sn, e.getMessage());
             e.printStackTrace();
         }
+        return false;
     }
 }

--
Gitblit v1.9.3