liusuyi
2026-05-30 f4f4fc53260eb67483dce406a963628273786a61
ard-modules/ard-modules-gb28181/src/main/java/com/ard/gb28181/api/Gb28181ApiController.java
@@ -7,8 +7,14 @@
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.config.UserSetting;
import com.ard.gb28181.domain.GbChannel;
import com.ard.gb28181.domain.GbDevice;
import com.ard.gb28181.service.IDeviceService;
import com.ard.gb28181.service.IGbChannelService;
import com.ard.gb28181.service.IGbDeviceService;
import com.ard.gb28181.service.ISIPCommander;
import com.ard.gb28181.session.SipInviteSessionManager;
import com.ard.zlm.api.RemoteZlmService;
@@ -22,6 +28,7 @@
import org.springframework.web.context.request.async.DeferredResult;
import javax.sip.ResponseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -48,6 +55,12 @@
    @Autowired
    private RemoteZlmService remoteZlmService;
    @Autowired
    private IGbDeviceService gbDeviceService;
    @Autowired
    private IGbChannelService gbChannelService;
    /**
     * 根据设备id获取设备
@@ -686,4 +699,181 @@
        }
        frontEndCommand(deviceId, channelId, cmdCode, switchId, 0, 0);
    }
    // ==================== GbDevice / GbChannel 管理接口 ====================
    /**
     * 根据国标设备ID获取GbDevice
     */
    @GetMapping("/getGbDevice/{gbDeviceId}")
    public R<GbDeviceDTO> getGbDevice(@PathVariable String gbDeviceId) {
        GbDevice gbDevice = gbDeviceService.selectGbDeviceByGbDeviceId(gbDeviceId);
        if (gbDevice == null) {
            return R.fail("GbDevice不存在 gbDeviceId:" + gbDeviceId);
        }
        return R.ok(toGbDeviceDTO(gbDevice));
    }
    /**
     * 获取所有GbDevice列表
     */
    @GetMapping("/getAllGbDevices")
    public R<List<GbDeviceDTO>> getAllGbDevices() {
        List<GbDevice> list = gbDeviceService.selectGbDeviceList(new GbDevice());
        List<GbDeviceDTO> dtoList = new ArrayList<>();
        for (GbDevice d : list) {
            dtoList.add(toGbDeviceDTO(d));
        }
        return R.ok(dtoList);
    }
    /**
     * 根据国标设备ID和通道ID获取GbChannel
     */
    @GetMapping("/getGbChannel/{gbDeviceId}/{gbChannelId}")
    public R<GbChannelDTO> getGbChannel(@PathVariable String gbDeviceId, @PathVariable String gbChannelId) {
        GbChannel gbChannel = gbChannelService.selectByGbDeviceIdAndGbChannelId(gbDeviceId, gbChannelId);
        if (gbChannel == null) {
            return R.fail("GbChannel不存在 gbDeviceId:" + gbDeviceId + " gbChannelId:" + gbChannelId);
        }
        return R.ok(toGbChannelDTO(gbChannel));
    }
    /**
     * 根据国标设备ID获取所有通道
     */
    @GetMapping("/getGbChannelsByDeviceId/{gbDeviceId}")
    public R<List<GbChannelDTO>> getGbChannelsByDeviceId(@PathVariable String gbDeviceId) {
        List<GbChannel> list = gbChannelService.selectGbChannelByGbDeviceId(gbDeviceId);
        List<GbChannelDTO> dtoList = new ArrayList<>();
        for (GbChannel c : list) {
            dtoList.add(toGbChannelDTO(c));
        }
        return R.ok(dtoList);
    }
    /**
     * 更新GbChannel流状态
     */
    @PostMapping("/updateGbChannelStream")
    R<Boolean> updateGbChannelStream(@RequestBody GbChannelDTO dto) {
        GbChannel gbChannel = new GbChannel();
        gbChannel.setId(dto.getId());
        gbChannel.setStreamStatus(dto.getStreamStatus());
        gbChannel.setStreamKey(dto.getStreamKey());
        gbChannel.setMediaServerId(dto.getMediaServerId());
        gbChannel.setSnap(dto.getSnap());
        gbChannelService.updateGbChannelStream(gbChannel);
        return R.ok(true);
    }
    private GbDeviceDTO toGbDeviceDTO(GbDevice d) {
        GbDeviceDTO dto = new GbDeviceDTO();
        dto.setId(d.getId());
        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());
        return dto;
    }
    private GbChannelDTO toGbChannelDTO(GbChannel c) {
        GbChannelDTO dto = new GbChannelDTO();
        dto.setId(c.getId());
        dto.setGbDeviceId(c.getGbDeviceId());
        dto.setGbChannelId(c.getGbChannelId());
        dto.setChannelName(c.getChannelName());
        dto.setDeviceCode(c.getDeviceCode());
        dto.setStreamMode(c.getStreamMode());
        dto.setEnableMp4(c.getEnableMp4());
        dto.setStreamStatus(c.getStreamStatus());
        dto.setMediaServerId(c.getMediaServerId());
        dto.setStreamKey(c.getStreamKey());
        dto.setSnap(c.getSnap());
        return dto;
    }
    /**
     * 一键迁移:将 Redis 中已注册的国标设备同步到 MySQL(供首次执行 SQL 后使用)
     */
    @Operation(summary = "迁移Redis国标设备到MySQL")
    @PostMapping("/migrateGbDevices")
    public R<String> migrateGbDevices() {
        List<Device> allDevices = deviceService.getAllDevices();
        int deviceCount = 0;
        int channelCount = 0;
        for (Device device : allDevices) {
            try {
                // 创建设备记录
                GbDevice existingDevice = gbDeviceService.selectGbDeviceByGbDeviceId(device.getDeviceId());
                if (existingDevice == null) {
                    GbDevice gbDevice = new GbDevice();
                    gbDevice.setGbDeviceId(device.getDeviceId());
                    gbDevice.setDeviceName(device.getName() != null ? device.getName() : device.getDeviceId());
                    gbDevice.setDeviceCode(device.getDeviceId());
                    gbDevice.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                    gbDevice.setEnableMp4("0");
                    gbDeviceService.insertGbDevice(gbDevice);
                    deviceCount++;
                    log.info("[迁移] 创建设备: {}", device.getDeviceId());
                }
                // 创建默认通道(设备自身)
                GbChannel existingDefaultChannel = gbChannelService.selectByGbDeviceIdAndGbChannelId(
                        device.getDeviceId(), device.getDeviceId());
                if (existingDefaultChannel == null) {
                    GbChannel gbChannel = new GbChannel();
                    gbChannel.setGbDeviceId(device.getDeviceId());
                    gbChannel.setGbChannelId(device.getDeviceId());
                    gbChannel.setChannelName(device.getName() != null ? device.getName() : device.getDeviceId());
                    gbChannel.setDeviceCode(device.getDeviceId());
                    gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                    gbChannel.setEnableMp4("0");
                    gbChannelService.insertGbChannel(gbChannel);
                    channelCount++;
                }
                // 创建所有通道
                List<DeviceChannel> channels = deviceService.getChannelsByDeviceId(device.getDeviceId());
                if (channels != null) {
                    for (DeviceChannel channel : channels) {
                        String channelId = channel.getDeviceId();
                        if (channelId == null || channelId.length() <= 8) {
                            continue;
                        }
                        if (channelId.length() == 20) {
                            try {
                                com.ard.gb28181.api.domain.GbCode gbCode = com.ard.gb28181.api.domain.GbCode.decode(channelId);
                                if (gbCode != null && ("215".equals(gbCode.getTypeCode()) || "216".equals(gbCode.getTypeCode()))) {
                                    continue;
                                }
                            } catch (Exception ignored) {}
                        }
                        GbChannel existing = gbChannelService.selectByGbDeviceIdAndGbChannelId(device.getDeviceId(), channelId);
                        if (existing == null) {
                            GbChannel gbChannel = new GbChannel();
                            gbChannel.setGbDeviceId(device.getDeviceId());
                            gbChannel.setGbChannelId(channelId);
                            gbChannel.setChannelName(channel.getName() != null ? channel.getName() : channelId);
                            gbChannel.setDeviceCode(channelId);
                            gbChannel.setStreamMode(device.getStreamMode() != null ? device.getStreamMode() : "TCP-PASSIVE");
                            gbChannel.setEnableMp4("0");
                            gbChannelService.insertGbChannel(gbChannel);
                            channelCount++;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("[迁移] 处理设备失败: {}", device.getDeviceId(), e);
            }
        }
        return R.ok("迁移完成: 设备 " + deviceCount + " 条, 通道 " + channelCount + " 条");
    }
}