|
@@ -11,9 +11,31 @@
|
|
MqttClient::MqttClient(QObject *parent) : QObject(parent), _mqClient(new QMqttClient(this)) {
|
|
MqttClient::MqttClient(QObject *parent) : QObject(parent), _mqClient(new QMqttClient(this)) {
|
|
// 连接信号和槽
|
|
// 连接信号和槽
|
|
connect(_mqClient, &QMqttClient::stateChanged, this, &MqttClient::onStateChanged);
|
|
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::messageReceived, this, &MqttClient::onMessageReceived);
|
|
connect(_mqClient, &QMqttClient::errorChanged, this, &MqttClient::onError);
|
|
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,
|
|
void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, const QString &username,
|
|
@@ -34,7 +56,7 @@ void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, cons
|
|
}
|
|
}
|
|
void MqttClient::onConnected() {
|
|
void MqttClient::onConnected() {
|
|
Logger::getInstance().debug("MQTT conncted");
|
|
Logger::getInstance().debug("MQTT conncted");
|
|
- subscribeToTopics(m_subscribeTopics);
|
|
|
|
|
|
+ // subscribeToTopics(m_subscribeTopics);
|
|
}
|
|
}
|
|
|
|
|
|
void MqttClient::subscribeToTopics(const QStringList &topics) {
|
|
void MqttClient::subscribeToTopics(const QStringList &topics) {
|
|
@@ -83,23 +105,6 @@ void MqttClient::onMessageReceived(const QByteArray &message, const QMqttTopicNa
|
|
void MqttClient::onStateChanged(QMqttClient::ClientState state) {
|
|
void MqttClient::onStateChanged(QMqttClient::ClientState state) {
|
|
switch (state) {
|
|
switch (state) {
|
|
case QMqttClient::ClientState::Disconnected: {
|
|
case QMqttClient::ClientState::Disconnected: {
|
|
- 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) {
|
|
|
|
- // TODO
|
|
|
|
- _mqClient->connectToHost();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
Logger::getInstance().info("mqtt disconnected");
|
|
Logger::getInstance().info("mqtt disconnected");
|
|
break;
|
|
break;
|
|
}
|
|
}
|