| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Copyright (C) 2017 The Qt Company Ltd.
- // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
- #ifndef QML_MQTTCLIENT_H
- #define QML_MQTTCLIENT_H
- #include <QByteArray>
- #include <QTextCodec>
- #include <QtCore/QMap>
- #include <QtMqtt/QMqttClient>
- #include <QtMqtt/QMqttSubscription>
- // class QmlMqttClient;
- // class QmlMqttSubscription : public QObject
- // {
- // Q_OBJECT
- // Q_PROPERTY(QMqttTopicFilter topic MEMBER m_topic NOTIFY topicChanged)
- // public:
- // QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c);
- // ~QmlMqttSubscription();
- // Q_SIGNALS:
- // void topicChanged(QString);
- // void messageReceived(const QString &msg);
- // public slots:
- // void handleMessage(const QMqttMessage &qmsg);
- // private:
- // Q_DISABLE_COPY(QmlMqttSubscription)
- // QMqttSubscription *sub;
- // QmlMqttClient *client;
- // QMqttTopicFilter m_topic;
- // };
- class QmlMqttClient : public QObject
- {
- Q_OBJECT
- Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
- Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
- Q_PROPERTY(QMqttClient::ClientState state READ state WRITE setState NOTIFY stateChanged)
- public:
- QByteArray m_client_dataBuf; //数据缓存区
- int m_client_rxByte = 0; //接收字节数
- QmlMqttClient(QObject *parent = nullptr);
- Q_INVOKABLE void connectToHost();
- Q_INVOKABLE void disconnectFromHost();
- //Q_INVOKABLE QmlMqttSubscription *subscribe(const QString &topic);
- const QString hostname() const;
- void setHostname(const QString &newHostname);
- int port() const;
- void setPort(int newPort);
- const QMqttClient::ClientState state() const;
- void setState(const QMqttClient::ClientState &newState);
- bool ConnectStart(QString &remoteIp, unsigned short remotePort);
- bool Subscription(QString &m_topic);
- private:
- QTextCodec *tc; // 编码
- Q_DISABLE_COPY(QmlMqttClient)
- QMqttClient m_client;
- signals:
- void hostnameChanged();
- void portChanged();
- void stateChanged();
- void mqttclient_readSuccess(int recByte);
- private slots:
- void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
- //void mqttclient_disconnect();
- //void mqttclient_connected();
- };
- #endif // QML_MQTTCLIENT_H
|