#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H

#include <QtMqtt/qmqttclient.h>
#include <QtMqtt/qmqttmessage.h>

#include <QObject>

class MqttClient : public QObject {
    Q_OBJECT

   public:
    explicit MqttClient(QObject *parent = nullptr);
    QMqttSubscription *subscribeToTopic(const QString &topic);
    void sendMessage(const QString &topic, const QByteArray &message, quint8 qos, bool isRetainedMsg);
    void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
    void connectToMqttBroker(const QString &hostname, quint16 port, const QString &username, const QString &password,
                             const QString &clientId);
    void subscribeToTopics(const QStringList &topics);
    void disconnectFromMqttBroker();
    bool isConnected() const;
   signals:
    void connected();
    void disConnected();
    void messageReceived(const QByteArray &message, const QMqttTopicName &topic);
    void proMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
   private slots:

    void onConnected();
    // void onDisconnected();
    void onStateChanged(QMqttClient::ClientState newState);
    void onError(QMqttClient::ClientError error);
    // void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);

   private:
    static MqttClient *instance;  // 静态成员变量，用于保存单例实例
    QMqttClient *_mqClient;
    QStringList m_subscribeTopics;
};
#endif  // MQTTCLIENT_H
