zhaowenyu
2025-12-03 b5b95f50e7905e9424628254b3cba7b92aa77cde
ard-sample/src/main/java/com/ard/sample/manage/service/impl/DeviceServiceImpl.java
@@ -1,6 +1,9 @@
package com.ard.sample.manage.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ard.device.camera.domain.ArdCamera;
import com.ard.device.camera.service.IArdCameraService;
import com.ard.device.channel.domain.ArdChannel;
import com.ard.sample.component.websocket.handler.LiveStreamHandler;
import com.ard.system.authorize.service.AuthorizeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -69,6 +72,9 @@
    @Autowired
    private LiveStreamHandler _liveStreamHandler;
    @Autowired
    private IArdCameraService _cameraService;
    @Autowired
    private IMessageSenderService messageSender;
@@ -354,15 +360,27 @@
    //region 单独写一个方法做权限验证,由于有好多定时任务也在实时获取设备列表,原方法不适用再添加权限注解
    @Override
    public List<DeviceDTO> getDockList() {
    public List<DeviceDTO> getDockList(String nickname) {
        List<DeviceDTO> devicesList = this.getDockListByParams(
                DeviceQueryParam.builder()
                        .domains(List.of(DeviceDomainEnum.GATEWAY.getVal(), DeviceDomainEnum.DOCK.getVal()))
                        .nickname(nickname)
                        .build());
        // 在獲取设备列表时,顺便获取直播地址
        for(DeviceDTO devices : devicesList){
            _liveStreamHandler.StartLiveStreaming(devices.getDeviceSn());
            _liveStreamHandler.StartLiveStreaming(devices.getChildDeviceSn());
            ArdCamera ardCamera = new ArdCamera();
            ardCamera.setDockId(devices.getId());
            List<ArdCamera> ardCameraList = _cameraService.selectArdCameraList(ardCamera);
            for(ArdCamera ardCamera1 : ardCameraList){
                for(ArdChannel ardChannel : ardCamera1.getChannelList()) {
                    if(ardChannel.getChanNo() == 1) {
                        // 修改列表中外接相机地址
                        updateCameraWebrtcUrl(devices.getDeviceSn(), ardChannel.getArdVtdu().getWebrtcUrl());
                    }
                }
            }
        }
        devicesList.stream()
@@ -407,6 +425,7 @@
                                })
                                .eq(param.getBoundStatus() != null, DeviceEntity::getBoundStatus,
                                        param.getBoundStatus())
                                .like(param.getNickname()!=null, DeviceEntity::getNickname, "%" + param.getNickname() + "%")
                                .orderBy(param.isOrderBy(),
                                        param.isAsc(), DeviceEntity::getId))
                .stream()
@@ -414,6 +433,90 @@
                .collect(Collectors.toList());
    }
    //endregion
    // region 多路直播列表,按nickname筛选
//    /**
//     * 多路直播列表,按nickname筛选
//     * */
//    @Override
//    public List<DeviceDTO> getDockCameraList(String nickname) {
//
//        List<DeviceDTO> devicesList = this.getDockListByParams(
//                DeviceQueryParam.builder()
//                        .domains(List.of(DeviceDomainEnum.GATEWAY.getVal(), DeviceDomainEnum.DOCK.getVal()))
//                        .nickname(nickname)
//                        .build());
//
//        // 在獲取设备列表时,顺便获取直播地址
//        for(DeviceDTO devices : devicesList){
//            _liveStreamHandler.StartLiveStreaming(devices.getDeviceSn());
//            _liveStreamHandler.StartLiveStreaming(devices.getChildDeviceSn());
//            ArdCamera ardCamera = new ArdCamera();
//            ardCamera.setDockId(devices.getId());
//            List<ArdCamera> ardCameraList = _cameraService.selectArdCameraList(ardCamera);
//            for(ArdCamera ardCamera1 : ardCameraList){
//                for(ArdChannel ardChannel : ardCamera1.getChannelList()) {
//                    if(ardChannel.getChanNo() == 1) {
//                        // 修改列表中外接相机地址
//                        updateCameraWebrtcUrl(devices.getDeviceSn(), ardChannel.getArdVtdu().getWebrtcUrl());
//                    }
//                }
//            }
//        }
//
//        devicesList.stream()
//                .filter(gateway -> DeviceDomainEnum.DOCK.getVal() == gateway.getDomain() ||
//                        RedisOpsUtils.checkExist(RedisConst.DEVICE_ONLINE_PREFIX + gateway.getDeviceSn()))
//                .forEach(this::spliceDeviceTopoPer);
//
//        return devicesList;
//    }
//    @Override
//    public void spliceDeviceTopoCamera(DeviceDTO gateway) {
//        gateway.setStatus(RedisOpsUtils.checkExist(RedisConst.DEVICE_ONLINE_PREFIX + gateway.getDeviceSn()));
//        // sub device
//        if (!StringUtils.hasText(gateway.getChildDeviceSn())) {
//            return;
//        }
//        DeviceDTO subDevice =
//                getDevicesByParams(DeviceQueryParam.builder().deviceSn(gateway.getChildDeviceSn()).build()).get(0);
//        subDevice.setStatus(RedisOpsUtils.checkExist(RedisConst.DEVICE_ONLINE_PREFIX + subDevice.getDeviceSn()));
//        gateway.setChildren(subDevice);
//    }
//    /**
//     * 按nickname筛选列表
//     * */
//    @Override
//    public List<DeviceDTO> getDockCameraListByParams(DeviceQueryParam param) {
//        return mapper.selectListPer(
//                        new LambdaQueryWrapper<DeviceEntity>()
//                                .eq(StringUtils.hasText(param.getDeviceSn()),
//                                        DeviceEntity::getDeviceSn, param.getDeviceSn())
//                                .eq(param.getDeviceType() != null,
//                                        DeviceEntity::getDeviceType, param.getDeviceType())
//                                .eq(param.getSubType() != null,
//                                        DeviceEntity::getSubType, param.getSubType())
//                                .eq(StringUtils.hasText(param.getChildSn()),
//                                        DeviceEntity::getChildSn, param.getChildSn())
//                                .and(!CollectionUtils.isEmpty(param.getDomains()), wrapper -> {
//                                    for (Integer domain : param.getDomains()) {
//                                        wrapper.eq(DeviceEntity::getDomain, domain).or();
//                                    }
//                                })
//                                .eq(param.getBoundStatus() != null, DeviceEntity::getBoundStatus,
//                                        param.getBoundStatus())
//                                .like(param.getNickname()!=null, DeviceEntity::getNickname, "%" + param.getNickname() + "%")
//                                .orderBy(param.isOrderBy(),
//                                        param.isAsc(), DeviceEntity::getId))
//                .stream()
//                .map(this::deviceEntityConvertToDTO)
//                .collect(Collectors.toList());
//    }
    // endregion
    @Override
    public Optional<TopologyDeviceDTO> getDeviceTopoForPilot(String sn) {
@@ -763,6 +866,7 @@
                .deptId(entity.getDeptId())
                .workType(entity.getWorkType())
                .webrtcUrl(entity.getWebrtcUrl())
                .cameraWebrtcUrl(entity.getCameraWebrtcUrl())
                .workspaceName(entity.getDeptId() != null
                        ? Optional.ofNullable(sysDeptService.selectDeptById(entity.getDeptId()))
                        .map(SysDept::getDeptName)
@@ -822,6 +926,16 @@
        int update = mapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }
    public Boolean updateCameraWebrtcUrl(String sn, String CameraWebrtcUrl){
        LambdaUpdateWrapper<DeviceEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DeviceEntity::getDeviceSn, sn)
                .set(DeviceEntity::getCameraWebrtcUrl, CameraWebrtcUrl);
        int update = mapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }
    @Override
    public Boolean bindDevice(DeviceDTO device) {
@@ -1060,14 +1174,14 @@
                        .data(Map.of(MapKeyConst.DEVICES, deviceOtaFirmwares))
                        .build());
        if (serviceReply.getResult() != ResponseResult.CODE_SUCCESS) {
            return ResponseResult.error(serviceReply.getResult(), "Firmware Error Code: " + serviceReply.getResult());
            String errorMsg = CommonErrorEnum.getMsgByCode(serviceReply.getResult());
            return ResponseResult.error(serviceReply.getResult(), errorMsg);
        }
        // Record the device state that needs to be updated.
        deviceOtaFirmwares.forEach(deviceOta -> RedisOpsUtils.setWithExpire(
                RedisConst.FIRMWARE_UPGRADING_PREFIX + deviceOta.getSn(),
                bid,
                RedisConst.DEVICE_ALIVE_SECOND * RedisConst.DEVICE_ALIVE_SECOND));
                bid,RedisConst.DEVICE_ALIVE_SECOND * RedisConst.DEVICE_ALIVE_SECOND));
        return ResponseResult.success();
    }
@@ -1075,7 +1189,7 @@
    public void devicePropertySet(String dockSn, DeviceSetPropertyEnum propertyEnum, JsonNode param) {
        boolean dockOnline = this.checkDeviceOnline(dockSn);
        if (!dockOnline) {
            throw new RuntimeException("Dock is offline.");
            throw new RuntimeException(CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
        }
        DeviceDTO deviceDTO = (DeviceDTO) RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_PREFIX + dockSn);
        boolean deviceOnline = this.checkDeviceOnline(deviceDTO.getChildDeviceSn());
@@ -1213,6 +1327,7 @@
                .firmwareVersion(dto.getFirmwareVersion())
                .workType(dto.getWorkType())
                .webrtcUrl(dto.getWebrtcUrl())
                .cameraWebrtcUrl(dto.getCameraWebrtcUrl())
                .compatibleStatus(dto.getFirmwareStatus() == null ? null :
                        DeviceFirmwareStatusEnum.CONSISTENT_UPGRADE != DeviceFirmwareStatusEnum.find(dto.getFirmwareStatus()))
                .build();