aix
2024-08-20 b0dd7ca693fcd13e539316cfc8c63150182a0315
src/main/java/com/dji/sample/manage/service/impl/DeviceServiceImpl.java
@@ -26,6 +26,9 @@
import com.dji.sample.manage.model.param.DeviceQueryParam;
import com.dji.sample.manage.model.receiver.*;
import com.dji.sample.manage.service.*;
import com.dji.sample.wayline.model.entity.WaylineJobEntity;
import com.dji.sample.wayline.model.param.WaylineJobQueryParam;
import com.dji.sample.wayline.service.IWaylineJobService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -97,6 +100,9 @@
    @Autowired
    private IDeviceRedisService deviceRedisService;
    @Autowired
    private IWaylineJobService waylineJobService;
    @Autowired
    @Qualifier("gatewayOSDServiceImpl")
@@ -279,10 +285,13 @@
    public List<DeviceDTO> getDevicesByParams(DeviceQueryParam param) {
        return mapper.selectList(
                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()),
@@ -295,6 +304,7 @@
                        .eq(StringUtils.hasText(param.getWorkspaceId()),
                                DeviceEntity::getWorkspaceId, param.getWorkspaceId())
                        .eq(param.getBoundStatus() != null, DeviceEntity::getBoundStatus, param.getBoundStatus())
                        .eq(param.getReserveId() != null, DeviceEntity::getReserveId, param.getReserveId())
                        .orderBy(param.isOrderBy(),
                                param.isAsc(), DeviceEntity::getId))
                .stream()
@@ -303,10 +313,11 @@
    }
    @Override
    public List<DeviceDTO> getDevicesTopoForWeb(String workspaceId) {
    public List<DeviceDTO> getDevicesTopoForWeb(String workspaceId, String reserveId) {
        List<DeviceDTO> devicesList = this.getDevicesByParams(
                DeviceQueryParam.builder()
                        .workspaceId(workspaceId)
                        .reserveId(reserveId)
                        .domains(List.of(DeviceDomainEnum.GATEWAY.getVal(), DeviceDomainEnum.DOCK.getVal()))
                        .build());
@@ -314,6 +325,17 @@
                .filter(gateway -> DeviceDomainEnum.DOCK.getVal() == gateway.getDomain() ||
                        deviceRedisService.checkDeviceOnline(gateway.getDeviceSn()))
                .forEach(this::spliceDeviceTopo);
        devicesList.forEach(deviceDTO -> {
            WaylineJobQueryParam waylineJobQueryParam = new WaylineJobQueryParam();
            waylineJobQueryParam.setDockSn(deviceDTO.getDeviceSn());
            WaylineJobEntity latestJob = waylineJobService.getLatestJob(workspaceId, waylineJobQueryParam);
            deviceDTO.setLatestWaylineJob(latestJob);
            deviceDTO.setOnlineStatus(true);
            OsdDockReceiver osdDockReceiver = (OsdDockReceiver) RedisOpsUtils.get("osd:" + deviceDTO.getDeviceSn());
            deviceDTO.setLatitude(osdDockReceiver!=null? osdDockReceiver.getLatitude():null);
            deviceDTO.setLongitude(osdDockReceiver!=null? osdDockReceiver.getLongitude():null);
        });
        return devicesList;
    }
@@ -435,7 +457,7 @@
            if (deviceOpt.isEmpty()) {
                deviceOpt = this.getDeviceBySn(from);
                if (deviceOpt.isEmpty()) {
                    log.error("Please restart the drone.");
                    log.error("请重新启动无人机.");
                    return;
                }
@@ -518,9 +540,37 @@
            }
            entity.setId(deviceEntity.getId());
            mapper.updateById(entity);
            return Optional.of(deviceEntity);
            fillNullField(entity, deviceEntity);
            return Optional.of(entity);
        }
        return mapper.insert(entity) > 0 ? Optional.of(entity) : Optional.empty();
    }
    private void fillNullField(DeviceEntity entity, DeviceEntity oldEntity) {
        if (Objects.isNull(entity) || Objects.isNull(oldEntity)) {
            return;
        }
        if (Objects.isNull(entity.getWorkspaceId())) {
            entity.setWorkspaceId(oldEntity.getWorkspaceId());
        }
        if (Objects.isNull(entity.getUserId())) {
            entity.setUserId(oldEntity.getUserId());
        }
        if (Objects.isNull(entity.getChildSn())) {
            entity.setChildSn(oldEntity.getChildSn());
        }
        if (Objects.isNull(entity.getBoundStatus())) {
            entity.setBoundStatus(oldEntity.getBoundStatus());
        }
        if (Objects.isNull(entity.getBoundTime())) {
            entity.setBoundTime(oldEntity.getBoundTime());
        }
        if (Objects.isNull(entity.getFirmwareVersion())) {
            entity.setFirmwareVersion(oldEntity.getFirmwareVersion());
        }
        if (Objects.isNull(entity.getDeviceIndex())) {
            entity.setDeviceIndex(oldEntity.getDeviceIndex());
        }
    }
    /**
@@ -650,7 +700,7 @@
            return;
        }
        if (entity.getFirmwareVersion().equals(firmwareReleaseNoteOpt.get().getProductVersion())) {
            deviceDTO.setFirmwareStatus(entity.getCompatibleStatus() ?
            deviceDTO.setFirmwareStatus(Objects.requireNonNullElse(entity.getCompatibleStatus(), true) ?
                    DeviceFirmwareStatusEnum.NOT_UPGRADE.getVal() :
                    DeviceFirmwareStatusEnum.CONSISTENT_UPGRADE.getVal());
            return;
@@ -880,7 +930,7 @@
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(deviceOtaFirmwares.get(0).getSn());
        if (deviceOpt.isEmpty()) {
            throw new RuntimeException("Device is offline.");
            throw new RuntimeException("设备离线");
        }
        DeviceDTO device = deviceOpt.get();
        String gatewaySn = DeviceDomainEnum.DOCK.getVal() == device.getDomain() ? device.getDeviceSn() : device.getParentSn();
@@ -891,7 +941,7 @@
        ServiceReply serviceReply = messageSender.publishServicesTopic(
                gatewaySn, FirmwareMethodEnum.OTA_CREATE.getMethod(), Map.of(MapKeyConst.DEVICES, deviceOtaFirmwares), bid);
        if (serviceReply.getResult() != ResponseResult.CODE_SUCCESS) {
            return ResponseResult.error(serviceReply.getResult(), "Firmware Error Code: " + serviceReply.getResult());
            return ResponseResult.error(serviceReply.getResult(), "硬件错误码 " + serviceReply.getResult());
        }
        // Record the device state that needs to be updated.
@@ -908,16 +958,17 @@
    private void checkOtaConditions(String dockSn) {
        Optional<OsdDockReceiver> deviceOpt = deviceRedisService.getDeviceOsd(dockSn, OsdDockReceiver.class);
        if (deviceOpt.isEmpty()) {
            throw new RuntimeException("Dock is offline.");
            throw new RuntimeException("机场离线");
        }
        boolean emergencyStopState = deviceOpt.get().getEmergencyStopState();
        if (emergencyStopState) {
            throw new RuntimeException("The emergency stop button of the dock is pressed and can't be upgraded.");
//            throw new RuntimeException("The emergency stop button of the dock is pressed and can't be upgraded.");
            throw new RuntimeException("机场急停按钮被按下,无法升级");
        }
        DockModeCodeEnum dockMode = this.getDockMode(dockSn);
        if (DockModeCodeEnum.IDLE != dockMode) {
            throw new RuntimeException("The current status of the dock can't be upgraded.");
            throw new RuntimeException("当前机场状态无法升级");
        }
    }
@@ -925,13 +976,13 @@
    public void devicePropertySet(String workspaceId, String dockSn, DeviceSetPropertyEnum propertyEnum, JsonNode param) {
        Optional<DeviceDTO> dockOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (dockOpt.isEmpty()) {
            throw new RuntimeException("Dock is offline.");
            throw new RuntimeException("机场离线");
        }
        String childSn = dockOpt.get().getChildDeviceSn();
        boolean deviceOnline = deviceRedisService.checkDeviceOnline(childSn);
        Optional<OsdSubDeviceReceiver> osdOpt = deviceRedisService.getDeviceOsd(childSn, OsdSubDeviceReceiver.class);
        if (!deviceOnline || osdOpt.isEmpty()) {
            throw new RuntimeException("Device is offline.");
            throw new RuntimeException("设备离线");
        }
        // Make sure the data is valid.
@@ -959,6 +1010,41 @@
    }
    @Override
    public int devicePropertySet(String workspaceId, String dockSn, JsonNode value) {
        Optional<DeviceDTO> dockOpt = deviceRedisService.getDeviceOnline(dockSn);
        if (dockOpt.isEmpty()) {
            throw new RuntimeException("机场离线");
        }
        String childSn = dockOpt.get().getChildDeviceSn();
        boolean deviceOnline = deviceRedisService.checkDeviceOnline(childSn);
        Optional<OsdSubDeviceReceiver> osdOpt = deviceRedisService.getDeviceOsd(childSn, OsdSubDeviceReceiver.class);
        if (!deviceOnline || osdOpt.isEmpty()) {
            throw new RuntimeException("设备离线");
        }
        String topic = THING_MODEL_PRE + PRODUCT + dockSn + PROPERTY_SUF + SET_SUF;
        Map reply = messageSender.publishWithReply(
                Map.class, topic,
                CommonTopicResponse.builder()
                        .bid(UUID.randomUUID().toString())
                        .tid(UUID.randomUUID().toString())
                        .timestamp(System.currentTimeMillis())
                        .data(value)
                        .build());
        SetReply setReply = objectMapper.convertValue(reply, SetReply.class);
        if (SetReplyStatusResultEnum.SUCCESS.getVal() != setReply.getResult()) {
            throw new RuntimeException("设置失败" + reply + "; 错误码: " + setReply.getResult());
        }
        return ResponseResult.success().getCode();
    }
    @Override
    public void deviceOnePropertySet(String topic, DeviceSetPropertyEnum propertyEnum, Map.Entry<String, Object> value) {
        if (Objects.isNull(value) || Objects.isNull(value.getValue())) {
            throw new IllegalArgumentException(CommonErrorEnum.ILLEGAL_ARGUMENT.getErrorMsg());
@@ -983,7 +1069,7 @@
        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());
            throw new RuntimeException("设置失败" + value.getKey() + "; 错误码: " + setReply.getResult());
        }
    }
@@ -1134,14 +1220,14 @@
    }
    /**
     * Handles messages in the state topic about basic drone data.
     *
     * Note: Only the data of the drone payload is handled here. You can handle other data from the drone
     * according to your business needs.
     * @param deviceBasic   basic drone data
     * 处理关于基本无人机数据的状态主题的消息。
     * 注:这里只处理无人机有效载荷的数据。你可以处理无人机的其他数据
     * *根据您的业务需要。
     * @param deviceBasic   无人机基本数据
     */
    @ServiceActivator(inputChannel = ChannelName.INBOUND_STATE_BASIC, outputChannel = ChannelName.INBOUND_STATE_PAYLOAD)
    public List<DevicePayloadReceiver> stateBasic(DeviceBasicReceiver deviceBasic) {
        log.info("无人机数据的状态主题的消息--DeviceBasicReceiver:{}",deviceBasic);
        Optional<DeviceDTO> deviceOpt = deviceRedisService.getDeviceOnline(deviceBasic.getDeviceSn());
        if (deviceOpt.isEmpty()) {
            return deviceBasic.getPayloads();
@@ -1151,6 +1237,7 @@
            return deviceBasic.getPayloads();
        }
        DeviceDTO dock = dockOpt.get();
        log.info("redis dockOpt:{}",dockOpt);
        if (!deviceBasic.getControlSource().equals(dock.getControlSource())) {
            dock.setControlSource(deviceBasic.getControlSource());
            deviceRedisService.setDeviceOnline(dock);
@@ -1166,4 +1253,4 @@
        }
        return deviceBasic.getPayloads();
    }
}
}