qml_mqttclient.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
  3. #ifndef QML_MQTTCLIENT_H
  4. #define QML_MQTTCLIENT_H
  5. #include <QByteArray>
  6. #include <QTextCodec>
  7. #include <QtCore/QMap>
  8. #include <QtMqtt/QMqttClient>
  9. #include <QtMqtt/QMqttSubscription>
  10. // class QmlMqttClient;
  11. // class QmlMqttSubscription : public QObject
  12. // {
  13. // Q_OBJECT
  14. // Q_PROPERTY(QMqttTopicFilter topic MEMBER m_topic NOTIFY topicChanged)
  15. // public:
  16. // QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c);
  17. // ~QmlMqttSubscription();
  18. // Q_SIGNALS:
  19. // void topicChanged(QString);
  20. // void messageReceived(const QString &msg);
  21. // public slots:
  22. // void handleMessage(const QMqttMessage &qmsg);
  23. // private:
  24. // Q_DISABLE_COPY(QmlMqttSubscription)
  25. // QMqttSubscription *sub;
  26. // QmlMqttClient *client;
  27. // QMqttTopicFilter m_topic;
  28. // };
  29. class QmlMqttClient : public QObject
  30. {
  31. Q_OBJECT
  32. Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
  33. Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
  34. Q_PROPERTY(QMqttClient::ClientState state READ state WRITE setState NOTIFY stateChanged)
  35. public:
  36. QByteArray m_client_dataBuf; //数据缓存区
  37. int m_client_rxByte = 0; //接收字节数
  38. QmlMqttClient(QObject *parent = nullptr);
  39. Q_INVOKABLE void connectToHost();
  40. Q_INVOKABLE void disconnectFromHost();
  41. //Q_INVOKABLE QmlMqttSubscription *subscribe(const QString &topic);
  42. const QString hostname() const;
  43. void setHostname(const QString &newHostname);
  44. int port() const;
  45. void setPort(int newPort);
  46. const QMqttClient::ClientState state() const;
  47. void setState(const QMqttClient::ClientState &newState);
  48. bool ConnectStart(QString &remoteIp, unsigned short remotePort);
  49. bool Subscription(QString &m_topic);
  50. private:
  51. QTextCodec *tc; // 编码
  52. Q_DISABLE_COPY(QmlMqttClient)
  53. QMqttClient m_client;
  54. signals:
  55. void hostnameChanged();
  56. void portChanged();
  57. void stateChanged();
  58. void mqttclient_readSuccess(int recByte);
  59. private slots:
  60. void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
  61. //void mqttclient_disconnect();
  62. //void mqttclient_connected();
  63. };
  64. #endif // QML_MQTTCLIENT_H