mqttthread.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. void mqttDisconnected();
  21. // 转发 MQTT 消息接收信号
  22. void projectMqttMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
  23. void sendMessageRequested(const QString &topic, const QByteArray &message, quint8 qos, bool isRetainedMsg);
  24. protected:
  25. void run() override;
  26. private:
  27. MqttClient *mqttClient = nullptr;
  28. QString m_hostname;
  29. quint16 m_port;
  30. QString m_username;
  31. QString m_password;
  32. QString m_clientId;
  33. QStringList m_topicsToSubscribe;
  34. bool m_stopFlag = false;
  35. };
  36. #endif // MQTTTHREAD_H