! *\brief called when a different MQTT connection is selected in the connection ComboBox. * connects to the MQTT broker according to the connection settings. */
| 2579 | * connects to the MQTT broker according to the connection settings. |
| 2580 | */ |
| 2581 | void ImportFileWidget::mqttConnectionChanged() { |
| 2582 | if (m_initialisingMQTT || ui.cbConnection->currentIndex() == -1) { |
| 2583 | ui.lLWT->hide(); |
| 2584 | ui.bLWT->hide(); |
| 2585 | ui.lMqttTopics->hide(); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | WAIT_CURSOR; |
| 2590 | CleanupNoArguments cleanup([] () { |
| 2591 | RESET_CURSOR; |
| 2592 | }); |
| 2593 | Q_EMIT error(QString()); |
| 2594 | |
| 2595 | // disconnected from the broker that was selected before |
| 2596 | disconnectMqttConnection(); |
| 2597 | |
| 2598 | delete m_client; |
| 2599 | m_client = new QMqttClient; |
| 2600 | connect(m_client, &QMqttClient::connected, this, &ImportFileWidget::onMqttConnect); |
| 2601 | connect(m_client, &QMqttClient::disconnected, this, &ImportFileWidget::onMqttDisconnect); |
| 2602 | connect(m_client, &QMqttClient::messageReceived, this, &ImportFileWidget::mqttMessageReceived); |
| 2603 | connect(m_client, &QMqttClient::errorChanged, this, &ImportFileWidget::mqttErrorChanged); |
| 2604 | |
| 2605 | // determine the connection settings for the new broker and initialize the mqtt client |
| 2606 | KConfig config(m_configPath, KConfig::SimpleConfig); |
| 2607 | KConfigGroup group = config.group(ui.cbConnection->currentText()); |
| 2608 | m_client->setHostname(group.readEntry("Host")); |
| 2609 | m_client->setPort(group.readEntry("Port").toUInt()); |
| 2610 | |
| 2611 | const bool useID = group.readEntry("UseID").toUInt(); |
| 2612 | if (useID) |
| 2613 | m_client->setClientId(group.readEntry("ClientID")); |
| 2614 | |
| 2615 | const bool useAuthentication = group.readEntry("UseAuthentication").toUInt(); |
| 2616 | if (useAuthentication) { |
| 2617 | m_client->setUsername(group.readEntry("UserName")); |
| 2618 | m_client->setPassword(group.readEntry("Password")); |
| 2619 | } |
| 2620 | |
| 2621 | // connect to the selected broker |
| 2622 | QDEBUG("Connect to " << m_client->hostname() << ":" << m_client->port()); |
| 2623 | if (!m_connectTimeoutTimer) { |
| 2624 | m_connectTimeoutTimer = new QTimer(this); |
| 2625 | m_connectTimeoutTimer->setInterval(6000); |
| 2626 | connect(m_connectTimeoutTimer, &QTimer::timeout, this, &ImportFileWidget::mqttConnectTimeout); |
| 2627 | } |
| 2628 | m_connectTimeoutTimer->start(); |
| 2629 | m_client->connectToHost(); |
| 2630 | } |
| 2631 | |
| 2632 | void ImportFileWidget::disconnectMqttConnection() { |
| 2633 | if (m_client && m_client->state() == QMqttClient::ClientState::Connected) { |
nothing calls this directly
no test coverage detected