//package com.yzt.airconditionersystem.config;
//
//import ch.qos.logback.core.net.server.Client;
//import com.yzt.airconditionersystem.bean.mqtt.MqttCallback;
//import org.eclipse.paho.client.mqttv3.MqttClient;
//import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.eclipse.paho.client.mqttv3.MqttException;
//import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.UUID;
//
///**
// * @author TzZach
// * @since 2022-05-10 21:10
// */
//@Configuration
//@ConditionalOnProperty(value = "spring.mqtt.enable", havingValue = "true")
//public class MqttConfig {
//
//    @Value("${spring.mqtt.username}")
//    private String username;
//
//    @Value("${spring.mqtt.password}")
//    private String password;
//
//    @Value("${spring.mqtt.url}")
//    private String url;
//
////    @Value("${spring.mqtt.producerClientId}")
////    private String producerClientId;
//
//    /**
//     * 生产者和消费者是单独连接服务器会使用到一个clientId（客户端id），如果是同一个clientid的话会出现Lost connection: 已断开连接; retrying...
//     */
////    @Value("${spring.mqtt.consumerClientId}")
////    private String consumerClientId;
//
////    @Value("${spring.mqtt.clientId}")
//    private String clientId = UUID.randomUUID().toString().replace("-", "");
//
//    /**
//     * 连接超时
//     */
//    @Value("${spring.mqtt.timeout}")
//    private int timeout;
//
//    /**
//     * 连接超时
//     */
//    @Value("${spring.mqtt.keepalive}")
//    private int keepalive;
//
//    /**
//     *入站通道名（消费者）订阅的bean名称
//     */
//    public static final String CHANNEL_NAME_IN = "mqttInboundChannel";
//
//    /**
//     * 出站通道名（生产者）发布的bean名称
//     */
//    public static final String CHANNEL_NAME_OUT = "mqttOutboundChannel";
//
//
//    @Autowired
//    private MqttCallback mqttCallback;
//
//    @Bean
//    public MqttClient mqttClient() {
//        try {
//            MqttClientPersistence persistence = mqttClientPersistence();
//            MqttClient mqttClient = new MqttClient(url, clientId, persistence);
//
//            mqttCallback.setMqttClient(mqttClient);
//            mqttClient.setCallback(mqttCallback);
//
//            mqttClient.connect(mqttConnectOptions());
//
//            return mqttClient;
//        } catch (MqttException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//    @Bean
//    public MqttConnectOptions mqttConnectOptions() {
//        MqttConnectOptions options = new MqttConnectOptions();
//        options.setUserName(username);
//        options.setPassword(password.toCharArray());
//        options.setCleanSession(true);
//        options.setAutomaticReconnect(true);
//        options.setConnectionTimeout(timeout);
//        options.setKeepAliveInterval(keepalive);
//        return options;
//    }
//
//    public MqttClientPersistence mqttClientPersistence() {
//        return new MemoryPersistence();
//    }
//
//
//}
