package xiaoqu.home.open.mqtt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("spring.mqtt")
public class MqttConfig {
	@Autowired
	private MqttPushClient mqttPushClient;
	private String username;
	private String password;
	private String hostUrl;
	private String clientId;
	private String defaultTopic;
	private int timeout;
	private int keepalive;

	public void setMqttPushClient(MqttPushClient mqttPushClient) {
		this.mqttPushClient = mqttPushClient;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setHostUrl(String hostUrl) {
		this.hostUrl = hostUrl;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public void setDefaultTopic(String defaultTopic) {
		this.defaultTopic = defaultTopic;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public void setKeepalive(int keepalive) {
		this.keepalive = keepalive;
	}

	public String getUsername() {
		return this.username;
	}

	public String getPassword() {
		return this.password;
	}

	public String getHostUrl() {
		return this.hostUrl;
	}

	public String getClientId() {
		return this.clientId;
	}

	public String getDefaultTopic() {
		return this.defaultTopic;
	}

	public int getTimeout() {
		return this.timeout;
	}

	public int getKeepalive() {
		return this.keepalive;
	}

	@Bean
	public MqttPushClient getMqttPushClient() {
		
		this.mqttPushClient.connect(this.hostUrl, this.clientId, this.username, this.password, this.timeout,this.keepalive);

//		this.mqttPushClient.subscribe(this.defaultTopic, 1);
//		this.mqttPushClient.subscribe("$SYS/brokers/+/clients/#", 1);
		return this.mqttPushClient;
	}
}
