mqttthread.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef MQTTTHREAD_H
  2. #define MQTTTHREAD_H
  3. #include <QString>
  4. #include <QStringList>
  5. #include <QThread>
  6. #include "./mqtt/mqttclient.h"
  7. class MqttClient;
  8. class MqttThread : public QThread {
  9. Q_OBJECT
  10. public:
  11. MqttThread(QObject *parent = nullptr);
  12. ~MqttThread();
  13. void stopThread();
  14. void sendMqttMessage(const QString &topic, const QByteArray &message, quint8 qos, bool isRetainedMsg);
  15. void setConnectionInfo(const QString &hostname, quint16 port, const QString &username, const QString &password,
  16. const QString &clientId, const QStringList &topicsToSubscribe);
  17. MqttClient *getMqttClient() const;
  18. signals:
  19. void mqttConnected();
  20. // 转发 MQTT 消息接收信号
  21. void projectMqttMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
  22. void sendMessageRequested(const QString &topic, const QByteArray &message, quint8 qos, bool isRetainedMsg);
  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