liusuyi
2025-12-24 49c807f8c2e80cc012ed755acbe1308c5de0c65c
ard-sample/src/main/java/com/ard/sample/manage/service/impl/DeviceServiceImpl.java
@@ -1,10 +1,15 @@
package com.ard.sample.manage.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ard.common.annotation.DataScope;
import com.ard.common.annotation.DataScopePlus;
import com.ard.common.core.domain.model.LoginUser;
import com.ard.common.utils.SecurityUtils;
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.enums.MessageType;
import com.ard.sample.component.websocket.handler.DockWebSocketHandler;
import com.ard.sample.component.websocket.handler.LiveStreamHandler;
import com.ard.system.authorize.service.AuthorizeService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -37,12 +42,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@@ -55,6 +62,7 @@
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.ard.sample.component.mqtt.model.TopicConst.*;
@@ -68,6 +76,15 @@
@Slf4j
@Transactional
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DockWebSocketHandler _dockWebSocketHandler;
    @Autowired
    private LiveStreamHandler _liveStreamHandler;
    @Autowired
    private IArdCameraService _cameraService;
    @Autowired
    private IMessageSenderService messageSender;
@@ -351,12 +368,31 @@
        // subDevice.setPayloadsList(payloadService.getDevicePayloadEntitiesByDeviceSn(gateway.getChildDeviceSn()));
    }
    //region 单独写一个方法做权限验证,由于有好多定时任务也在实时获取设备列表,原方法不适用再添加权限注解
    @Override
    public List<DeviceDTO> getDockList() {
    public List<DeviceDTO> getDockList(String nickname) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        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(), loginUser.getUserId().toString());
            _liveStreamHandler.StartLiveStreaming(devices.getChildDeviceSn(), loginUser.getUserId().toString());
            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(), ardCamera1.getState());
                    }
                }
            }
        }
        devicesList.stream()
                .filter(gateway -> DeviceDomainEnum.DOCK.getVal() == gateway.getDomain() ||
@@ -368,27 +404,21 @@
    @Override
    public void spliceDeviceTopoPer(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);
        // payloads
        // subDevice.setPayloadsList(payloadService.getDevicePayloadEntitiesByDeviceSn(gateway.getChildDeviceSn()));
    }
    //@DataScope(deptAlias = "d")
    @Override
    public List<DeviceDTO> getDockListByParams(DeviceQueryParam param){
        // 在mapper中添加查询方法做权限验证
        return mapper.selectListPer(
                        new LambdaQueryWrapper<DeviceEntity>()
                                .eq(StringUtils.hasText(param.getDeviceSn()),
@@ -406,12 +436,98 @@
                                })
                                .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
    // 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) {
@@ -506,10 +622,10 @@
    @Override
    @ServiceActivator(inputChannel = ChannelName.INBOUND_OSD)
    public void handleOSD(Message<?> message) {
        String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
        byte[] payload = (byte[]) message.getPayload();
        CommonTopicReceiver receiver;
        try {
            String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
            byte[] payload = (byte[]) message.getPayload();
            CommonTopicReceiver receiver;
            String from = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                    topic.indexOf(OSD_SUF));
@@ -547,7 +663,12 @@
            tsaService.handleOSD(receiver, device, webSessions, wsMessage);
        } catch (IOException e) {
        } catch (NullPointerException e) {
            log.error("处理OSD消息NPE,headers={}, payload={}",
                    message.getHeaders(), message.getPayload(), e); // 打印完整栈
        }
        catch (IOException e) {
            log.error("处理OSD消息的IO异常:{}",e.getMessage(), e);
            e.printStackTrace();
        }
    }
@@ -619,6 +740,7 @@
            // 验证授权数量
            validateAuthorization(devicesList.size(), 1);
        } catch (Exception e) {
            log.error("保存设备时验证授权异常,请联系管理员!错误信息:" + e.getMessage());
            return Optional.empty();
        }
        // 调用验证方法
@@ -754,6 +876,9 @@
                .firmwareVersion(entity.getFirmwareVersion())
                .deptId(entity.getDeptId())
                .workType(entity.getWorkType())
                .webrtcUrl(entity.getWebrtcUrl())
                .cameraWebrtcUrl(entity.getCameraWebrtcUrl())
                .cameraState(entity.getCameraState())
                .workspaceName(entity.getDeptId() != null
                        ? Optional.ofNullable(sysDeptService.selectDeptById(entity.getDeptId()))
                        .map(SysDept::getDeptName)
@@ -804,6 +929,27 @@
        int update = mapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }
    @Override
    public Boolean updateWebrtcUrl(String sn, String webrtcUrl){
        LambdaUpdateWrapper<DeviceEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DeviceEntity::getDeviceSn, sn)
                .set(DeviceEntity::getWebrtcUrl, webrtcUrl);
        int update = mapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }
    public Boolean updateCameraWebrtcUrl(String sn, String CameraWebrtcUrl, String state){
        LambdaUpdateWrapper<DeviceEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DeviceEntity::getDeviceSn, sn)
                .set(DeviceEntity::getCameraWebrtcUrl, CameraWebrtcUrl)
                .set(DeviceEntity::getCameraState, state);
        int update = mapper.update(null, lambdaUpdateWrapper);
        return update > 0;
    }
    @Override
    public Boolean bindDevice(DeviceDTO device) {
@@ -880,6 +1026,9 @@
        BindDeviceReceiver dock = null;
        BindDeviceReceiver drone = null;
        for (BindDeviceReceiver device : devices) {
            if(device.getDeviceModelKey().isEmpty()){
                continue;
            }
            int val = Integer.parseInt(device.getDeviceModelKey().split("-")[0]);
            if (val == DeviceDomainEnum.DOCK.getVal()) {
                dock = device;
@@ -1039,34 +1188,41 @@
                        .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();
    }
    /**
     * 属性设置
     * */
    @Override
    public void devicePropertySet(String dockSn, DeviceSetPropertyEnum propertyEnum, JsonNode param) {
    public ResponseResult devicePropertySet(String dockSn, DeviceSetPropertyEnum propertyEnum, JsonNode param) {
        boolean dockOnline = this.checkDeviceOnline(dockSn);
        if (!dockOnline) {
            throw new RuntimeException("Dock is offline.");
            return ResponseResult.error(CommonErrorEnum.DEVICE_OFFLINE.getErrorCode(),"机场" + CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
            //throw new RuntimeException(CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
        }
        DeviceDTO deviceDTO = (DeviceDTO) RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_PREFIX + dockSn);
        boolean deviceOnline = this.checkDeviceOnline(deviceDTO.getChildDeviceSn());
        if (!deviceOnline) {
            throw new RuntimeException("Device is offline.");
            return ResponseResult.error(2222,"无人机" + CommonErrorEnum.DEVICE_OFFLINE.getErrorMsg());
            //throw new RuntimeException("Device is offline.");
            // CommonErrorEnum.DEVICE_OFFLINE.getErrorCode()
        }
        // Make sure the data is valid.
        BasicDeviceProperty basicDeviceProperty = objectMapper.convertValue(param, propertyEnum.getClazz());
        boolean valid = basicDeviceProperty.valid();
        if (!valid) {
            throw new IllegalArgumentException(CommonErrorEnum.ILLEGAL_ARGUMENT.getErrorMsg());
            return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT.getErrorMsg());
            // throw new IllegalArgumentException(CommonErrorEnum.ILLEGAL_ARGUMENT.getErrorMsg());
        }
        String topic = THING_MODEL_PRE + PRODUCT + dockSn + PROPERTY_SUF + SET_SUF;
@@ -1074,8 +1230,20 @@
                (OsdSubDeviceReceiver) RedisOpsUtils.get(RedisConst.OSD_PREFIX + deviceDTO.getChildDeviceSn());
        if (!param.isObject()) {
            this.deviceOnePropertySet(topic, propertyEnum, Map.entry(propertyEnum.getProperty(), param));
            return;
            return ResponseResult.success();
//            return;
        }
        if(param.size()==1){
            this.deviceOnePropertySetSilent(topic,propertyEnum,param);
            return ResponseResult.success();
//            return;
        }
//        if(Objects.equals(param.fieldNames().next(), "silent_mode"))
//        {
//            this.deviceOnePropertySet(topic, propertyEnum, Map.entry(propertyEnum.getProperty(), param.get("silent_mode")));
//            return;
//        }
        // If there are multiple parameters, set them separately.
        for (Iterator<Map.Entry<String, JsonNode>> filed = param.fields(); filed.hasNext(); ) {
            Map.Entry<String, JsonNode> node = filed.next();
@@ -1083,12 +1251,34 @@
            if (!isPublish) {
                continue;
            }
            this.deviceOnePropertySet(topic, propertyEnum, Map.entry(propertyEnum.getProperty(), node));
        }
        return ResponseResult.success();
    }
    /**
     * 为静音模式单独列出来一个方法,只有一个值的时候都可以调用,不限于静音模式
     * */
    @Override
    public void deviceOnePropertySetSilent(String topic, DeviceSetPropertyEnum propertyEnum,
                                     JsonNode value) {
//        if (Objects.isNull(value) || Objects.isNull(value.getValue())) {
//            throw new IllegalArgumentException(CommonErrorEnum.ILLEGAL_ARGUMENT.getErrorMsg());
//        }
        Map reply = messageSender.publishWithReply(
                Map.class, topic,
                CommonTopicResponse.builder()
                        .bid(UUID.randomUUID().toString())
                        .tid(UUID.randomUUID().toString())
                        .timestamp(System.currentTimeMillis())
                        .data(value)
                        .build(),
                2);
    }
        @Override
    public void deviceOnePropertySet(String topic, DeviceSetPropertyEnum propertyEnum,
                                     Map.Entry<String, Object> value) {
        if (Objects.isNull(value) || Objects.isNull(value.getValue())) {
@@ -1105,20 +1295,29 @@
                        .build(),
                2);
        while (true) {
            reply = (Map<String, Object>) reply.get(value.getKey());
            if (value.getValue() instanceof JsonNode) {
                break;
            }
            value = (Map.Entry) value.getValue();
        }
        SetReply setReply = objectMapper.convertValue(reply, SetReply.class);
        if (SetReplyStatusResultEnum.SUCCESS.getVal() != setReply.getResult()) {
            throw new RuntimeException("Failed to set " + value.getKey() + "; Error Code: " + setReply.getResult());
        }
// 接收返回信息在serviceReply中,订阅thing/product/7CTDLCE00A1UPE/property/set_reply上报的信息,此处不处理
//        while (true) {
//            reply = (Map<String, Object>) reply.get(value.getKey());
//            if (value.getValue() instanceof JsonNode) {
//                break;
//            }
//            value = (Map.Entry) value.getValue();
//        }
//        SetReply setReply = objectMapper.convertValue(reply, SetReply.class);
//        if (SetReplyStatusResultEnum.SUCCESS.getVal() != setReply.getResult()) {
//            throw new RuntimeException("Failed to set " + value.getKey() + "; Error Code: " + setReply.getResult());
//        }
    }
//    public JsonNode getJsonNode(JsonNode jsonNode) {
//        ObjectNode parentNode = objectMapper.createObjectNode();
//        parentNode.put("bid", UUID.randomUUID().toString());
//        parentNode.set("data", jsonNode);
//        parentNode.put("tid", UUID.randomUUID().toString());
//        parentNode.put("timestamp", System.currentTimeMillis());
//        return parentNode;
//    }
    @Override
    public Boolean checkDeviceOnline(String sn) {
@@ -1150,6 +1349,9 @@
                .domain(dto.getDomain())
                .firmwareVersion(dto.getFirmwareVersion())
                .workType(dto.getWorkType())
                .webrtcUrl(dto.getWebrtcUrl())
                .cameraWebrtcUrl(dto.getCameraWebrtcUrl())
                .cameraState(dto.getCameraState())
                .compatibleStatus(dto.getFirmwareStatus() == null ? null :
                        DeviceFirmwareStatusEnum.CONSISTENT_UPGRADE != DeviceFirmwareStatusEnum.find(dto.getFirmwareStatus()))
                .build();
@@ -1209,7 +1411,7 @@
                .sn(device.getDeviceSn())
                .deviceCallsign(device.getNickname())
                .isDeviceBindOrganization(device.getBoundStatus())
                .organizationId(device.getDeptId().toString())
                .organizationId(String.valueOf(device.getDeptId()))
                .organizationName(device.getWorkspaceName())
                .build();
    }
@@ -1291,9 +1493,48 @@
        messageSender.publish(topic, builder.build());
    }
//    @DataScopePlus(deptAlias="d")
//    @Override
//    public List<DeviceEntity> selectList(Wrapper<DeviceEntity> queryWrapper) {
//        return mapper.selectList(queryWrapper);
//    }
    /**
     * 定时任务,每秒钟从redis中读取信息发送机场摄像头(舱内舱外)、飞行器摄像头模式(广角、变焦、红外)
     * */
    @Override
    @Scheduled(initialDelay = 5, fixedRate = 1, timeUnit = TimeUnit.SECONDS)
    public void sendCameraInfo() {
        // 此方法修改为定时任务,每秒钟从redis中读取信息发送
        // 发送机场摄像头位置信息
        Map<Object, Object> map = RedisOpsUtils.getAllData(RedisConst.COVER_STATE_PREFIX+"*");
        map.forEach((k, v) -> {
           _dockWebSocketHandler.broadcastMessageToAllClients(v.toString());
        });
    }
    // 机场直播摄像头信息存redis中,舱内、舱外
    @Override
    public void setRedisCameraPosition(String sn, Integer cameraPosition) {
        String liveStatus = "";
        Optional<DeviceDTO> device = getDeviceBySn(sn);
        if(device.get().getDomain().equals(DeviceDomainEnum.DOCK.getVal())){
            CommonTopicReceiver<Map<String,Integer>> stateReceiver = new CommonTopicReceiver<>();
            stateReceiver.setTid(UUID.randomUUID().toString());
            stateReceiver.setBid(UUID.randomUUID().toString());
            stateReceiver.setType(MessageType.LIVESTATUS.getType());
            stateReceiver.setGateway(sn);
            stateReceiver.setTimestamp(System.currentTimeMillis());
            Map<String,Integer> live_status = new HashMap<>();
            live_status.put("camera_position", cameraPosition);
            stateReceiver.setData(live_status);
            try{
                liveStatus=objectMapper.writeValueAsString(stateReceiver);
            }
            catch (Exception e){
                log.error("机场直播设备状态反馈转换异常:{}",e);
            }
        }
        log.info("机场直播设备状态反馈存入redis中,key值:{}中:{}",RedisConst.COVER_STATE_PREFIX,liveStatus);
        RedisOpsUtils.set(RedisConst.COVER_STATE_PREFIX + sn, liveStatus);
    }
    public void setChildRedisCamera(String sn, String context){
        // 保存子设备信息到redis中,便于前端获取
        RedisOpsUtils.set(RedisConst.COVER_STATE_PREFIX + sn, context);
    }
}