package com.template.util;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.internal.wire.MqttReceivedMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
/**
 * @Author：liuxinyue
 * @Package：com.template.util
 * @Project：cloud-server
 * @name：MqttClient
 * @Date：2024/9/26 16:05
 */
@Log4j2
public class MyMqttClient {

    public static MqttClient mqttClient=null;

    private static MemoryPersistence memoryPersistence=null;

    private static MqttConnectOptions mqttConnectOptions=null;

    private static String ClientName="";

    private static String IP="";


    public static void main(String[] args) {
        start(ClientName);
    }


    public static void start(String clientId){
        //初始化连接设置对象
        mqttConnectOptions = new MqttConnectOptions();
        //设置是否清空session
        mqttConnectOptions.setCleanSession(true);
        //设置连接超时时间
        mqttConnectOptions.setConnectionTimeout(10);
        //设置持久化方式
        memoryPersistence = new MemoryPersistence();

        if(null!=clientId){
            try {
                mqttClient = new MqttClient("tcp://"+IP+":1883", clientId,memoryPersistence);
            } catch (MqttException e) {
                log.error(e);
            }
        }

        log.info("连接状态:"+mqttClient.isConnected());

        //设置连接和回调
        if(null!=mqttClient){
            if(!mqttClient.isConnected()){
                //创建回调函数对象
                Callback mqttReceiveCallBack = new Callback();
                //客户端添加回调函数
                mqttClient.setCallback(mqttReceiveCallBack);
                try {
                    log.info("连接");
                    mqttClient.connect(mqttConnectOptions);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        }else{
            log.info("mqttClient为空");
        }
        log.info("连接状态:"+mqttClient.isConnected());
    }

    //关闭连接
    public void closeConnect(){
        //关闭存储方式
        if(null!=memoryPersistence){
            try {
                memoryPersistence.close();
            } catch (MqttPersistenceException e) {
                e.printStackTrace();
            }
        }else{
            log.info("memoryPersistence为空");
        }

        //关闭连接
        if(null!=mqttClient){
            if(mqttClient.isConnected()){
                try {
                    mqttClient.disconnect();
                    mqttClient.close();
                } catch (MqttException e) {
                   e.printStackTrace();
                }
            }else{
                log.info("mqttClient未连接");
            }
        }else{
            log.info("mqttClient为空");
        }


    }

    //发布消息
    public static void publish(String topic, String message,int ops){
        if(null!=mqttClient && mqttClient.isConnected()){
            log.info("发布消息:"+mqttClient.isConnected());
            log.info("id:"+mqttClient.getClientId());
            MqttMessage mqttMessage = new MqttMessage();
            mqttMessage.setQos(ops);
            mqttMessage.setPayload(message.getBytes());

            MqttTopic topic1 = mqttClient.getTopic(topic);

            if(null!=topic1){
                try {
                    MqttDeliveryToken publish = topic1.publish(mqttMessage);
                    if(!publish.isComplete()){
                        log.info("消息发布成功");
                    }
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }

        }else{
            reConnect();
        }
    }

    //	重新连接
    public static void reConnect() {
        if(null != mqttClient) {
            if(!mqttClient.isConnected()) {
                if(null != mqttConnectOptions) {
                    try {
                        mqttClient.connect(mqttConnectOptions);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }else {
                    log.info("mqttConnectOptions是空的");
                }
            }else {
                log.info("mqttClient没有连接");
            }
        }else {
            start(ClientName);
        }
    }

    //订阅主题
    public static void subTopic(String topic){
        if(null!=mqttClient && mqttClient.isConnected()){
            try {
                mqttClient.subscribe(topic,1);
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }else{
            log.info("mqttClient是错误的");
        }
    }

    //清空主题
    public void cleanTopic(String topic){
        if(null!=mqttClient && mqttClient.isConnected()){
            try {
                mqttClient.unsubscribe(topic);
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }else{
            log.info("mqttClient是错误的");
        }
    }


}
