liusuyi
2026-06-01 a2e7e8ff9cfaa69b001d483710bddbda50d55c91
ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/service/impl/DeviceChannelServiceImpl.java
@@ -2,12 +2,16 @@
import com.ard.gb28181.api.domain.Device;
import com.ard.gb28181.api.domain.DeviceChannel;
import com.ard.gb28181.api.domain.GbCode;
import com.ard.gb28181.domain.GbChannel;
import com.ard.gb28181.service.IDeviceChannelService;
import com.ard.gb28181.service.IGbChannelService;
import com.ard.gb28181.service.IRedisCatchStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
@@ -22,13 +26,16 @@
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
    @Autowired
    private IGbChannelService gbChannelService;
    /**
     * 根据设备id清楚设备通道
     *
     * @param deviceId
     */
    @Override
    public void cleanChannelsForDevice(int deviceId) {
    public void cleanChannelsForDevice(String deviceId) {
        redisCatchStorage.cleanChannelsForDevice(deviceId);
    }
@@ -47,6 +54,77 @@
        List<DeviceChannel> deviceChannelsInRedis = redisCatchStorage.queryAllChannelsForRefresh(device.getDeviceId());
        redisCatchStorage.batchAdd(device.getDeviceId(), mergeWithNewChannels(deviceChannelsInRedis, channels, device.isOnLine()));
        // 自动为每个通道创建 GbChannel 记录(若不存在)
        autoCreateGbChannelForChannels(device, channels);
    }
    /**
     * 自动为设备下的每个通道创建 GbChannel 记录
     */
    private void autoCreateGbChannelForChannels(Device device, List<DeviceChannel> channels) {
        String parentDeviceId = device.getDeviceId();
        try {
            boolean hasRealSubChannel = false;
            for (DeviceChannel channel : channels) {
                String channelId = channel.getDeviceId();
                if (channelId == null) {
                    continue;
                }
                // 跳过行政区划(设备ID长度 <= 8)
                if (channelId.length() <= 8) {
                    continue;
                }
                // 跳过业务分组(215)和虚拟组织(216)
                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) {
                    }
                }
                // 判断是否有真实子通道(不是设备自身的默认通道)
                if (!channelId.equals(parentDeviceId)) {
                    hasRealSubChannel = true;
                }
                // 检查是否已存在
                GbChannel existing = gbChannelService.selectByGbDeviceIdAndGbChannelId(parentDeviceId, channelId);
                if (existing == null) {
                    GbChannel gbChannel = new GbChannel();
                    gbChannel.setGbDeviceId(parentDeviceId);
                    gbChannel.setGbChannelId(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");
                    gbChannelService.insertGbChannel(gbChannel);
                    log.info("[GbChannel自动创建] parentDeviceId={}, channelId={}, id={}",
                            parentDeviceId, channelId, gbChannel.getId());
                } else if (StringUtils.hasText(channel.getName())
                        && !channel.getName().equals(existing.getChannelName())) {
                    // 已存在但名称有变化,从目录同步更新名称
                    existing.setChannelName(channel.getName());
                    gbChannelService.updateGbChannel(existing);
                    log.info("[GbChannel更新名称] parentDeviceId={}, channelId={}, {} -> {}",
                            parentDeviceId, channelId, existing.getChannelName(), channel.getName());
                }
            }
            // 如果有真实子通道,删除注册时创建的默认通道(gb_channel_id = gb_device_id)
            if (hasRealSubChannel) {
                GbChannel defaultChannel = gbChannelService.selectByGbDeviceIdAndGbChannelId(parentDeviceId, parentDeviceId);
                if (defaultChannel != null) {
                    gbChannelService.deleteGbChannelById(defaultChannel.getId());
                    log.info("[GbChannel清理默认通道] parentDeviceId={}, 有真实子通道,删除默认通道 id={}", parentDeviceId, defaultChannel.getId());
                }
            }
        } catch (Exception e) {
            log.error("[GbChannel自动创建] 失败 parentDeviceId={}", parentDeviceId, e);
        }
    }
    /**