sean.zhou
2023-04-25 694b9483c7a551626244cbc222c602ea9ff74094
src/main/java/com/dji/sample/component/mqtt/config/MqttConfiguration.java
@@ -5,6 +5,7 @@
import com.dji.sample.component.mqtt.model.MqttClientOptions;
import com.dji.sample.component.mqtt.model.MqttProtocolEnum;
import com.dji.sample.component.mqtt.model.MqttUseEnum;
import com.dji.sample.control.model.dto.MqttBrokerDTO;
import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -70,6 +71,32 @@
        return addr.toString();
    }
    /**
     * Get the connection parameters of the mqtt client of the drc link.
     * @param clientId
     * @param username
     * @param age   The validity period of the token. unit: s
     * @param map   Custom data added in token.
     * @return
     */
    public static MqttBrokerDTO getMqttBrokerWithDrc(String clientId, String username, Long age, Map<String, ?> map) {
        if (!mqtt.containsKey(MqttUseEnum.DRC)) {
            throw new RuntimeException("Please configure the drc link parameters of mqtt in the backend configuration file first.");
        }
        Algorithm algorithm = JwtUtil.algorithm;
        String token = JwtUtil.createToken(map, age, algorithm, null, null);
        return MqttBrokerDTO.builder()
                .address(getMqttAddress(mqtt.get(MqttUseEnum.DRC)))
                .username(username)
                .clientId(clientId)
                .expireTime(System.currentTimeMillis() / 1000 + age)
                .password(token)
                .build();
    }
    @Bean
    public MqttConnectOptions mqttConnectOptions() {
        MqttClientOptions customizeOptions = getBasicClientOptions();