package com.xueyi.common.mqtt.configure;

import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * mqtt配置
 *
 * @author xueyi
 */
//@Configuration
//@EnableCaching
@Data
public class MqttConfig {
    @Value("${emqx.brokerUrl}")
    private String brokerUrl;

    @Value("${emqx.clientId}")
    private String clientId;

    @Value("${emqx.username}")
    private String username;

    @Value("${emqx.password}")
    private String password;


    @Value("${emqx.topics}")
    private String topics;

    @Value("${emqx.prefix}")
    private String prefix;

    @Value("${emqx.fetchLogTopic}")
    private String fetchLogTopic;

    @Bean
    public MqttClient mqttClient() throws Exception {
        MqttConnectOptions options = new MqttConnectOptions();
//        options.setCleanSession(true);
        options.setCleanSession(true);
        options.setConnectionTimeout(10); // 设置连接超时时间为10秒
        options.setMaxReconnectDelay(5000); // 设置最大重连延迟为5秒
        options.setAutomaticReconnect(true);
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        MqttClient client = new MqttClient(brokerUrl, clientId);
        client.connect(options);
        return client;
    }

}