QMqttSubscriber 不打印收到的消息
QMqttSubscriber not printing received messages
我正在使用 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 的新手。所以请原谅任何不太好的书面代码。
所以,我发现了我的问题。订阅者没有订阅我指定的主题,因为加载这个方法太快了。延迟它现在工作正常。
我正在使用 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 的新手。所以请原谅任何不太好的书面代码。
所以,我发现了我的问题。订阅者没有订阅我指定的主题,因为加载这个方法太快了。延迟它现在工作正常。