mqttthread.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef MQTTTHREAD_H
  2. #define MQTTTHREAD_H
  3. #include <QThread>
  4. #include <QString>
  5. #include <QStringList>
  6. #include "./mqtt/mqttclient.h"
  7. class MqttClient;
  8. class MqttThread : public QThread
  9. {
  10. Q_OBJECT
  11. public:
  12. MqttThread(QObject *parent = nullptr);
  13. ~MqttThread();
  14. void stopThread();
  15. void sendMqttMessage(const QString &topic, const QByteArray& message);
  16. void setConnectionInfo(const QString& hostname, quint16 port, const QString& username, const QString& password, const QString& clientId, const QStringList& topicsToSubscribe);
  17. MqttClient* getMqttClient() const;
  18. signals:
  19. void mqttConnected();
  20. // 转发 MQTT 消息接收信号
  21. void messageAndTopicReceived(const QByteArray &message, const QMqttTopicName &topic);
  22. void sendMessageRequested(const QString& topic, const QByteArray& message);
  23. protected:
  24. void run() override;
  25. private:
  26. MqttClient* mqttClient = nullptr;
  27. QString m_hostname;
  28. quint16 m_port;
  29. QString m_username;
  30. QString m_password;
  31. QString m_clientId;
  32. QStringList m_topicsToSubscribe;
  33. bool m_stopFlag = false;
  34. };
  35. #endif // MQTTTHREAD_H