liusuyi
2026-06-01 a2e7e8ff9cfaa69b001d483710bddbda50d55c91
ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/api/Gb28181ApiController.java
@@ -5,10 +5,7 @@
import com.ard.common.core.domain.R;
import com.ard.common.core.domain.RtpServerParam;
import com.ard.common.core.exception.ServiceException;
import com.ard.gb28181.api.domain.Device;
import com.ard.gb28181.api.domain.DeviceChannel;
import com.ard.gb28181.api.domain.GbChannelDTO;
import com.ard.gb28181.api.domain.GbDeviceDTO;
import com.ard.gb28181.api.domain.*;
import com.ard.gb28181.config.UserSetting;
import com.ard.gb28181.domain.GbChannel;
import com.ard.gb28181.domain.GbDevice;
@@ -24,12 +21,15 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import javax.sip.ResponseEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -728,6 +728,18 @@
    }
    /**
     * 根据主键ID获取GbChannel
     */
    @GetMapping("/getGbChannelById/{id}")
    public R<GbChannelDTO> getGbChannelById(@PathVariable Long id) {
        GbChannel gbChannel = gbChannelService.selectGbChannelById(id);
        if (gbChannel == null) {
            return R.fail("GbChannel不存在 id:" + id);
        }
        return R.ok(toGbChannelDTO(gbChannel));
    }
    /**
     * 根据国标设备ID和通道ID获取GbChannel
     */
    @GetMapping("/getGbChannel/{gbDeviceId}/{gbChannelId}")
@@ -773,12 +785,12 @@
        dto.setGbDeviceId(d.getGbDeviceId());
        dto.setDeviceName(d.getDeviceName());
        dto.setDeviceCode(d.getDeviceCode());
        dto.setStreamMode(d.getStreamMode());
        dto.setEnableMp4(d.getEnableMp4());
        dto.setStreamStatus(d.getStreamStatus());
        dto.setMediaServerId(d.getMediaServerId());
        dto.setStreamKey(d.getStreamKey());
        dto.setSnap(d.getSnap());
        dto.setIp(d.getIp());
        dto.setPort(d.getPort());
        dto.setManufacturer(d.getManufacturer());
        dto.setModel(d.getModel());
        dto.setFirmware(d.getFirmware());
        dto.setOnLine(d.getOnLine());
        return dto;
    }
@@ -815,10 +827,14 @@
                if (existingDevice == null) {
                    GbDevice gbDevice = new GbDevice();
                    gbDevice.setGbDeviceId(device.getDeviceId());
                    gbDevice.setDeviceName(device.getName() != null ? device.getName() : device.getDeviceId());
                    gbDevice.setDeviceName(StringUtils.hasText(device.getName()) ? device.getName() : device.getDeviceId());
                    gbDevice.setDeviceCode(device.getDeviceId());
                    gbDevice.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                    gbDevice.setEnableMp4("0");
                    gbDevice.setIp(device.getIp());
                    gbDevice.setPort(device.getPort());
                    gbDevice.setManufacturer(device.getManufacturer());
                    gbDevice.setModel(device.getModel());
                    gbDevice.setFirmware(device.getFirmware());
                    gbDevice.setOnLine(device.isOnLine());
                    gbDeviceService.insertGbDevice(gbDevice);
                    deviceCount++;
                    log.info("[迁移] 创建设备: {}", device.getDeviceId());
@@ -831,7 +847,7 @@
                    GbChannel gbChannel = new GbChannel();
                    gbChannel.setGbDeviceId(device.getDeviceId());
                    gbChannel.setGbChannelId(device.getDeviceId());
                    gbChannel.setChannelName(device.getName() != null ? device.getName() : device.getDeviceId());
                    gbChannel.setChannelName(StringUtils.hasText(device.getName()) ? device.getName() : device.getDeviceId());
                    gbChannel.setDeviceCode(device.getDeviceId());
                    gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                    gbChannel.setEnableMp4("0");
@@ -860,7 +876,7 @@
                            GbChannel gbChannel = new GbChannel();
                            gbChannel.setGbDeviceId(device.getDeviceId());
                            gbChannel.setGbChannelId(channelId);
                            gbChannel.setChannelName(channel.getName() != null ? channel.getName() : channelId);
                            gbChannel.setChannelName(StringUtils.hasText(channel.getName()) ? channel.getName() : channelId);
                            gbChannel.setDeviceCode(channelId);
                            gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                            gbChannel.setEnableMp4("0");
@@ -876,4 +892,98 @@
        return R.ok("迁移完成: 设备 " + deviceCount + " 条, 通道 " + channelCount + " 条");
    }
    /**
     * 从 Redis 同步通道数据到 MySQL,使 ard_gb_channel 与 Redis 一致
     */
    @Operation(summary = "从Redis同步通道到MySQL")
    @PostMapping("/syncGbChannelsFromRedis")
    public R<String> syncGbChannelsFromRedis() {
        List<Device> allDevices = deviceService.getAllRedisDevices();
        int createdCount = 0;
        int deletedCount = 0;
        int skippedCount = 0;
        for (Device device : allDevices) {
            try {
                String deviceId = device.getDeviceId();
                List<DeviceChannel> redisChannels = deviceService.getChannelsByDeviceId(deviceId);
                if (redisChannels == null) {
                    redisChannels = new ArrayList<>();
                }
                Set<String> redisChannelIds = new HashSet<>();
                boolean hasRealSubChannel = false;
                for (DeviceChannel ch : redisChannels) {
                    String channelId = ch.getDeviceId();
                    if (channelId == null || channelId.length() <= 8) {
                        continue;
                    }
                    if (channelId.length() == 20) {
                        try {
                            GbCode gbCode = GbCode.decode(channelId);
                            if (gbCode != null && ("215".equals(gbCode.getTypeCode()) || "216".equals(gbCode.getTypeCode()))) {
                                continue;
                            }
                        } catch (Exception ignored) {}
                    }
                    redisChannelIds.add(channelId);
                    if (!channelId.equals(deviceId)) {
                        hasRealSubChannel = true;
                    }
                    GbChannel existing = gbChannelService.selectByGbDeviceIdAndGbChannelId(deviceId, channelId);
                    if (existing == null) {
                        GbChannel gbChannel = new GbChannel();
                        gbChannel.setGbDeviceId(deviceId);
                        gbChannel.setGbChannelId(channelId);
                        gbChannel.setChannelName(StringUtils.hasText(ch.getName()) ? ch.getName() : channelId);
                        gbChannel.setDeviceCode(channelId);
                        gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                        gbChannel.setEnableMp4("0");
                        gbChannelService.insertGbChannel(gbChannel);
                        createdCount++;
                        log.info("[同步] 创建通道: deviceId={}, channelId={}", deviceId, channelId);
                    } else if (StringUtils.hasText(ch.getName())
                            && !ch.getName().equals(existing.getChannelName())) {
                        existing.setChannelName(ch.getName());
                        gbChannelService.updateGbChannel(existing);
                        log.info("[同步] 更新通道名称: deviceId={}, channelId={}, {} -> {}",
                                deviceId, channelId, existing.getChannelName(), ch.getName());
                    }
                }
                List<GbChannel> mysqlChannels = gbChannelService.selectGbChannelByGbDeviceId(deviceId);
                for (GbChannel mysqlCh : mysqlChannels) {
                    if (!redisChannelIds.contains(mysqlCh.getGbChannelId())) {
                        if ("1".equals(mysqlCh.getStreamStatus())) {
                            skippedCount++;
                            continue;
                        }
                        gbChannelService.deleteGbChannelById(mysqlCh.getId());
                        deletedCount++;
                    }
                }
                if (hasRealSubChannel) {
                    GbChannel defaultChannel = gbChannelService.selectByGbDeviceIdAndGbChannelId(deviceId, deviceId);
                    if (defaultChannel != null && !redisChannelIds.contains(deviceId)) {
                        if (!"1".equals(defaultChannel.getStreamStatus())) {
                            gbChannelService.deleteGbChannelById(defaultChannel.getId());
                            deletedCount++;
                        } else {
                            skippedCount++;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("[同步] 处理设备失败: {}", device.getDeviceId(), e);
            }
        }
        return R.ok(String.format("同步完成: 创建 %d 条, 删除 %d 条, 跳过 %d 条(推流中)",
                createdCount, deletedCount, skippedCount));
    }
}