package com.usky.cdi.service.config.mqtt;

import lombok.Data;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;

@Data
public class MqttBaseConfig {

    private String username;

    private String password;

    private String hostUrl;

    private String msgTopic;

    //心跳间隔
    private int keepAliveInterval;

    //完成超时
    private int completionTimeout;


    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(new String[]{this.getHostUrl()});
        options.setUserName(this.getUsername());
        if (this.getPassword() != null) {
            options.setPassword(this.getPassword().toCharArray());
        }
        options.setKeepAliveInterval(this.getKeepAliveInterval());
        factory.setConnectionOptions(options);
        return factory;
    }

}
