QMqttSubscriber不打印收到的消息

时间:2018-03-19 11:21:50

标签: c++ qt mqtt subscribe

我正在使用QT开发一个mqtt项目。发布者工作正常,打开和关闭由raspi3控制的锁,但我还需要订阅一个主题,并需要在订阅的主题响应时启动另一个操作。我做了第一个小例子,然后开始在更好的项目中实现它。 但不是在这个项目中。我错过了什么吗?

以下是我的代码中的相关摘要:

HMIController.cpp

QmlMqttClient *HMIController::createClient()
{

mqttClient = new QmlMqttClient;
mqttClient->handleSub();

return mqttClient;

}

qmlmqttclient.h

#ifndef QMLMQTTCLIENT_H
#define QMLMQTTCLIENT_H

#include <QtCore/QMap>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttSubscription>
#include <QtMqtt/QMqttMessage>

class QmlMqttClient;

class QmlMqttClient : public QMqttClient
{
Q_OBJECT

public:
QmlMqttClient(QObject *parent = nullptr);

QMqttSubscription *QmlMqttClient::subscribe(const QMqttTopicFilter &topic, quint8 qos = 0);
Q_INVOKABLE qint32 publish(const QMqttTopicName &topic, const QByteArray &message = QByteArray(),
                           quint8 qos = 0, bool retain = false);

public slots:
void handleOpen();
void handleClose();
void handleSub();

private:
Q_DISABLE_COPY(QmlMqttClient)

private slots:
void brokerDisconnected();

};

#endif // QMLMQTTCLIENT_H

qmlmqttclient.cpp

#include <QtMqtt/QMqttSubscription>
#include <QtMqtt/QMqttTopicFilter>
#include <QMqttTopicName>
#include <QtMqtt/QMqttClient>
#include <QMqttClient>
#include <QMqttSubscription>
#include <QDateTime>
#include <QDebug>
#include <QtQuick>

#include "qmlmqttclient.h"
#include "hmicontroller.h"

QmlMqttClient::QmlMqttClient( QObject *parent)
    : QMqttClient(parent)
{

    this->setHostname("192.168.137.82");
    this->setPort(1883);
    this->connectToHost();

    if (ClientState::Disconnected)
    {
        connect(this, &QmlMqttClient::disconnected, this, &QmlMqttClient::brokerDisconnected);
        qDebug() << "Broker disconnected";
    } else if (ClientState::Connecting)
    {
        qDebug() << "Connecting to MqttBroker ..." << this->clientId();
    }

connect(this, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
    qDebug() << QDateTime::currentDateTime().toString()
                + QLatin1String(" Received Topic: ")
                + topic.name()
                + QLatin1String(" Message: ")
                + message
                + QLatin1Char('\n');

});

}

qint32 QmlMqttClient::publish(const QMqttTopicName &topic, const QByteArray &message,
                              quint8 qos, bool retain)
{
    return QMqttClient::publish(topic, message, qos, retain);
}

void QmlMqttClient::handleOpen()
{
    QString msg = "AUF";
    const QMqttTopicName *topic = new QMqttTopicName("lock");
    publish(*topic, msg.toUtf8() ,0);
}

void QmlMqttClient::handleClose()
{
    QString msg = "ZU";
    const QMqttTopicName *topic = new QMqttTopicName("lock");
    publish(*topic, msg.toUtf8() ,0);
}

QMqttSubscription *QmlMqttClient::subscribe(const QMqttTopicFilter &topic, quint8 qos)
{
    qDebug() << "Subscribed" << this->clientId();
    return QMqttClient::subscribe(topic, qos);
}

//Called from hmicontroller
void QmlMqttClient::handleSub()
{
    const QMqttTopicFilter *topic = new QMqttTopicFilter("button");

    subscribe(*topic, quint8(0));

    qDebug() << "Subscribed";
}



void QmlMqttClient::brokerDisconnected()
{
    qDebug() << "Session closed";
}

非常感谢你的帮助,顺便说一句,我也是c ++和qt的新手。所以请原谅任何不那么好的书面代码。

1 个答案:

答案 0 :(得分:0)

所以,我发现了我的问题。订阅者没有订阅我指定的主题,因为加载此方法是为了快速。延迟它现在工作正常。