mqttthread.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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,
  15. bool isRetainedMsg);
  16. void setConnectionInfo(const QString &hostname, quint16 port, const QString &username,
  17. const QString &password, const QString &clientId,
  18. const QStringList &topicsToSubscribe);
  19. MqttClient *getMqttClient() const;
  20. signals:
  21. void mqttConnected();
  22. // 转发 MQTT 消息接收信号
  23. void projectMqttMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
  24. void sendMessageRequested(const QString &topic, const QByteArray &message, quint8 qos,
  25. bool isRetainedMsg);
  26. protected:
  27. void run() override;
  28. private:
  29. MqttClient *mqttClient = nullptr;
  30. QString m_hostname;
  31. quint16 m_port;
  32. QString m_username;
  33. QString m_password;
  34. QString m_clientId;
  35. QStringList m_topicsToSubscribe;
  36. bool m_stopFlag = false;
  37. };
  38. #endif // MQTTTHREAD_H