|
@@ -3,6 +3,7 @@
|
|
|
#include <QJsonDocument>
|
|
|
#include <QJsonObject>
|
|
|
#include <QMessageBox>
|
|
|
+#include <QPushButton>
|
|
|
|
|
|
#include "../global.h"
|
|
|
#include "../logger.h"
|
|
@@ -10,9 +11,31 @@
|
|
|
MqttClient::MqttClient(QObject *parent) : QObject(parent), _mqClient(new QMqttClient(this)) {
|
|
|
// 连接信号和槽
|
|
|
connect(_mqClient, &QMqttClient::stateChanged, this, &MqttClient::onStateChanged);
|
|
|
+ connect(_mqClient, &QMqttClient::disconnected, this, [this]() {
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ this,
|
|
|
+ [this]() {
|
|
|
+ QMessageBox msgBox;
|
|
|
+ msgBox.setWindowTitle("MQtt 错误");
|
|
|
+ msgBox.setText("mqtt连接失败, 请检查网络连接或MQTT服务器状态");
|
|
|
+ QPushButton *yesButton = msgBox.addButton("重连", QMessageBox::YesRole);
|
|
|
+ QPushButton *noButton = msgBox.addButton("取消", QMessageBox::NoRole);
|
|
|
+ yesButton->setStyleSheet(
|
|
|
+ "QPushButton { background-color:rgb(5, 58, 156); color: white; padding: 5px 15px; "
|
|
|
+ "border-radius: 4px; }");
|
|
|
+ noButton->setStyleSheet(
|
|
|
+ "QPushButton { background-color: #f44336; color: white; padding: 5px 15px; "
|
|
|
+ "border-radius: 4px; }");
|
|
|
+ msgBox.exec();
|
|
|
+ if (msgBox.clickedButton() == yesButton) {
|
|
|
+ _mqClient->connectToHost();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::ConnectionType::QueuedConnection);
|
|
|
+ });
|
|
|
connect(_mqClient, &QMqttClient::messageReceived, this, &MqttClient::onMessageReceived);
|
|
|
connect(_mqClient, &QMqttClient::errorChanged, this, &MqttClient::onError);
|
|
|
- connect(_mqClient, &QMqttClient::connected, this, &MqttClient::onConnected);
|
|
|
+ // connect(_mqClient, &QMqttClient::connected, this, &MqttClient::onConnected);
|
|
|
}
|
|
|
|
|
|
void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, const QString &username,
|
|
@@ -33,7 +56,7 @@ void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, cons
|
|
|
}
|
|
|
void MqttClient::onConnected() {
|
|
|
Logger::getInstance().debug("MQTT conncted");
|
|
|
- subscribeToTopics(m_subscribeTopics);
|
|
|
+ // subscribeToTopics(m_subscribeTopics);
|
|
|
}
|
|
|
|
|
|
void MqttClient::subscribeToTopics(const QStringList &topics) {
|
|
@@ -58,7 +81,7 @@ void MqttClient::sendMessage(const QString &topic, const QByteArray &message, qu
|
|
|
auto pub = _mqClient->publish(QMqttTopicName(topic), message, qos, isRetainedMsg);
|
|
|
if (pub == -1)
|
|
|
Logger::getInstance().error(
|
|
|
- QString("MQTT client sent message to topic: %1, msg: %2, qos: %3, isRetainedMsg: %4")
|
|
|
+ QString("Error MQTT client sent message to topic: %1, msg: %2, qos: %3, isRetainedMsg: %4")
|
|
|
.arg(topic, QString(message), QString::number(qos), isRetainedMsg ? "true" : "false"));
|
|
|
else
|
|
|
Logger::getInstance().info(
|
|
@@ -81,9 +104,10 @@ void MqttClient::onMessageReceived(const QByteArray &message, const QMqttTopicNa
|
|
|
|
|
|
void MqttClient::onStateChanged(QMqttClient::ClientState state) {
|
|
|
switch (state) {
|
|
|
- case QMqttClient::ClientState::Disconnected:
|
|
|
+ case QMqttClient::ClientState::Disconnected: {
|
|
|
Logger::getInstance().info("mqtt disconnected");
|
|
|
break;
|
|
|
+ }
|
|
|
case QMqttClient::ClientState::Connecting:
|
|
|
Logger::getInstance().info("mqtt connecting");
|
|
|
break;
|