liusuyi
2025-12-27 fbced0c4dd4950a184f8471ef71995aa93dafd50
ard-device/src/main/java/com/ard/device/channel/service/impl/ArdChannelServiceImpl.java
@@ -1,6 +1,8 @@
package com.ard.device.channel.service.impl;
import com.ard.common.constant.CacheConstants;
import com.ard.common.constant.UserConstants;
import com.ard.common.core.redis.RedisCache;
import com.ard.common.utils.uuid.IdUtils;
import com.ard.device.camera.domain.ArdCamera;
import com.ard.device.camera.service.IArdCameraService;
@@ -11,8 +13,10 @@
import com.ard.vtdu.domain.ArdVtdu;
import com.ard.vtdu.service.IArdVtduService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@@ -26,14 +30,17 @@
@Slf4j
@Service
public class ArdChannelServiceImpl implements IArdChannelService {
    @Resource
    private IArdCameraService ardCameraService;
   // @Resource
    //private IArdCameraService ardCameraService;
    @Resource
    private ArdChannelMapper ardChannelMapper;
    @Resource
    private IArdVtduService ardVtduService;
    @Resource
    private MediamtxConfig mediamtxConfig;
    @Resource
    private RedisCache redisCache;
    /**
     * 查询通道管理
     *
@@ -105,12 +112,13 @@
     * 清空通道
     *
     * @author 刘苏义
     * @date   2024/8/10 11:18
     * @date 2024/8/10 11:18
     */
    @Override
    public int clearArdChannel(){
    public int clearArdChannel() {
        return ardChannelMapper.clearArdChannel();
    }
    /**
     * 删除通道管理信息
     *
@@ -121,6 +129,7 @@
    public int deleteArdChannelByDeviceId(String deviceId) {
        return ardChannelMapper.deleteArdChannelByDeviceId(deviceId);
    }
    /**
     * 校验相机通道是否唯一
     *
@@ -130,20 +139,19 @@
    @Override
    public boolean checkArdChannelUnique(ArdChannel channel) {
        ArdChannel info = ardChannelMapper.checkArdChannelUnique(channel);
        if (info != null && !Objects.equals(info.getId(), channel.getId()))
        {
        if (info != null && !Objects.equals(info.getId(), channel.getId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    @Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(fixedDelay = 5000)
    public void syncVtdu() {
        log.debug("定时同步Vtdu任务");
        List<ArdChannel> channels = this.selectArdChannelList(new ArdChannel());
        if (channels == null || channels.isEmpty()) {
            log.warn("没有获取到通道数据,退出同步");
            // log.warn("没有获取到通道数据,退出同步");
            return;
        }
        List<ArdVtdu> ardVtduList = ardVtduService.selectArdVtduList(new ArdVtdu());
@@ -156,10 +164,16 @@
            String name = channel.getDeviceId() + "_" + channel.getChanNo();  // 生成媒体表的 name
            String cameraId = channel.getDeviceId();  // cameraId 就是通道的 deviceId
            String rtspSourceUrl = getRtspSource(channel);
            String rtspUrl = "rtsp://" + mediamtxConfig.getPlayHost() + ":8554" + "/" + name;  // 拼接 webrtcUrl
            String rtmpUrl = "rtmp://" + mediamtxConfig.getPlayHost() + ":1935" + "/" + name;  // 拼接 webrtcUrl
            String rtspUrl =
                    "rtsp://" + mediamtxConfig.getPlayHost() + ":" + mediamtxConfig.getRtspPort() + "/" + name;  //
            // 拼接 webrtcUrl
            String rtmpUrl =
                    "rtmp://" + mediamtxConfig.getPlayHost() + ":" + mediamtxConfig.getRtmpPort() + "/" + name;  // 拼接
            // webrtcUrl
            String protocol = mediamtxConfig.isHttps() ? "https://" : "http://";
            String webrtcUrl = protocol + mediamtxConfig.getPlayHost() + ":8889" + "/" + name;  // 拼接 webrtcUrl
            String webrtcUrl =
                    protocol + mediamtxConfig.getPlayHost() + ":" + mediamtxConfig.getWebrtcPort() + "/" + name;
            // 拼接 webrtcUrl
            // 先检查媒体表是否已有此记录
            Optional<ArdVtdu> existingMedia = ardVtduList.stream()
@@ -196,6 +210,7 @@
        // 3. 删除操作:删除媒体表中多余的记录
        List<String> invalidNames = ardVtduList.stream()
                .filter(vtdu -> !validNames.contains(vtdu.getName()))
                .filter(vtdu -> !"publisher".equalsIgnoreCase(vtdu.getRtspSource())) // 不是机场流
                .map(ArdVtdu::getName)
                .collect(Collectors.toList());
@@ -211,7 +226,7 @@
    // 根据通道信息获取rtsp源地址
    private String getRtspSource(ArdChannel channel) {
        String rtspSource = "";
        ArdCamera ardCamera =ardCameraService.selectArdCameraById(channel.getDeviceId());
        ArdCamera ardCamera =  redisCache.getCacheObject(CacheConstants.CAMERA_LIST_KEY + channel.getDeviceId());
        if (ardCamera != null) {
            switch (ardCamera.getFactory()) {
                case "1":