From f1369957f739d58f1ba8426910fa4fd25012a77f Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Tue, 16 Dec 2025 19:57:47 +0800
Subject: [PATCH] 2025-12-16 zwy 优化机场开盖关盖时内外舱摄像头标记

---
 ard-sample/src/main/java/com/ard/sample/manage/service/impl/DockOnlineServiceImpl.java |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 55 insertions(+), 4 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 cf637e0..bf578d3 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
@@ -2,19 +2,21 @@
 
 import com.ard.common.core.redis.RedisConst;
 import com.ard.common.core.redis.RedisOpsUtils;
+import com.ard.sample.common.model.ResponseResult;
 import com.ard.sample.component.websocket.enums.MessageType;
 import com.ard.sample.component.websocket.handler.DockWebSocketHandler;
+import com.ard.sample.component.websocket.handler.LiveStreamHandler;
 import com.ard.sample.manage.model.dto.DeviceDTO;
+import com.ard.sample.manage.model.dto.LiveDTO;
+import com.ard.sample.manage.model.dto.LiveTypeDTO;
 import com.ard.sample.manage.service.IDeviceService;
+import com.ard.sample.manage.service.ILiveStreamService;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import javax.annotation.Resource;
-import java.net.URL;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -33,6 +35,12 @@
     @Autowired
     protected ObjectMapper _objectMapper;
 
+    @Autowired
+    private LiveStreamHandler _liveStreamHandler;
+
+    @Autowired
+    private ILiveStreamService _liveStreamService;
+
     /**
      * 定时任务,每2秒取一次机场设备的在线状态,如已离线更新状态
      * */
@@ -49,11 +57,18 @@
                 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());
+            }
+
         }
     }
 
@@ -82,7 +97,7 @@
 
             // 发送给websocket客户端
             _dockWebSocketHandler.sendMessageToSubscribers(device.getDeviceSn(), MessageType.STATE.getType(), json, "");
-            log.info("发送设备状态{}到websocket客户端:{}", MessageType.STATE.getType(), json);
+//            log.info("发送设备状态{}到websocket客户端:{}", MessageType.STATE.getType(), json);
         }
     }
 
@@ -110,4 +125,40 @@
 
         return jsonString;
     }
+
+    public void StartLiveStreaming(String sn) {
+        //调用开始直播
+        ResponseResult rlive = null;
+        String resultLive = "";
+        try {
+            // 获取直播参数
+            LiveTypeDTO param = _liveStreamHandler.getLiveParam(sn);
+            // 如果存在直播则读取直播地址返回
+            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);
+            } else {
+//                log.info("SN: {}-redis中未找到相机的播放地址信息!停止直播后再开启直播", sn);
+                _liveStreamHandler.StopLiveStreaming(sn);
+                // 如果不存在直播,则开始直播,并返回直播地址
+                rlive = _liveStreamService.liveStart(param);
+                if (rlive.getCode() == 0) {
+                    // 直播成功后,在redis中存一个标记,将videoid保留,留给停止直播使用
+                    log.info("SN: {}-开启直播成功!", sn);
+                    rlive.setGateway(sn);
+                    rlive.setType(MessageType.LIVESTREAM.getType());
+                    resultLive = _objectMapper.writeValueAsString(rlive);
+                    RedisOpsUtils.set(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn, resultLive);
+                    LiveDTO live = (LiveDTO) rlive.getData();
+                    Boolean isWeb = _deviceService.updateWebrtcUrl(sn,live.getWebrtcUrl());
+//                    if (isWeb) {
+//                        log.info("SN: {}-更新webrtc地址成功!", sn);
+//                    }
+                }
+            }
+        } catch (Exception e) {
+            log.info("订时任务推流-SN: {}-开启直播失败!原因:{}", sn, e.getMessage());
+            e.printStackTrace();
+        }
+    }
 }

--
Gitblit v1.9.3