diff options
author | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2018-10-24 16:07:05 +0200 |
---|---|---|
committer | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2018-10-25 06:05:24 +0000 |
commit | d59d4b183c3484a978f197ad27e2491138775c92 (patch) | |
tree | 212d9a4d6e7a5264ab5ff14f18ecb531257eda9b /tradeshow/iot-sensortag/mqttdataproviderpool.cpp | |
parent | c0917669684e051f79f6afb557bb491828346a47 (diff) |
SensorTag: Prefer loading MQTT configuration from file
To avoid accidently pushing credentials, they should not be patched into
the header, but rather be placed in to a file co-located to the
executable.
Change-Id: I03950107f39a88b0f1c7deb49ff16d25ddf9df4d
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'tradeshow/iot-sensortag/mqttdataproviderpool.cpp')
-rw-r--r-- | tradeshow/iot-sensortag/mqttdataproviderpool.cpp | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/tradeshow/iot-sensortag/mqttdataproviderpool.cpp b/tradeshow/iot-sensortag/mqttdataproviderpool.cpp index 24ce47c..55cd0f7 100644 --- a/tradeshow/iot-sensortag/mqttdataproviderpool.cpp +++ b/tradeshow/iot-sensortag/mqttdataproviderpool.cpp @@ -52,6 +52,8 @@ #include "mqttdataprovider.h" #include <QtCore/QDebug> +#include <QtCore/QFile> +#include <QtCore/QJsonDocument> MqttDataProviderPool::MqttDataProviderPool(QObject *parent) : DataProviderPool(parent) @@ -66,10 +68,10 @@ void MqttDataProviderPool::startScanning() emit providersUpdated(); emit dataProvidersChanged(); - m_client->setHostname(QLatin1String(MQTT_BROKER)); - m_client->setPort(MQTT_PORT); - m_client->setUsername(QByteArray(MQTT_USERNAME)); - m_client->setPassword(QByteArray(MQTT_PASSWORD)); + m_client->setHostname(MqttCredentials::getBroker()); + m_client->setPort((quint16)MqttCredentials::getPort()); + m_client->setUsername(MqttCredentials::getUsername()); + m_client->setPassword(MqttCredentials::getPassword()); connect(m_client, &QMqttClient::connected, [this]() { auto sub = m_client->subscribe(QLatin1String("sensors/active")); @@ -121,3 +123,62 @@ void MqttDataProviderPool::deviceUpdate(const QMqttMessage &msg) emit dataProvidersChanged(); } } + +namespace { + static QString gBroker; + static int gPort{0}; + static QString gUser; + static QString gPassword; + bool resolveCredentials() { + static bool resolved = false; + if (resolved) + return true; + + QFile f(QLatin1String("broker.json")); + if (!f.open(QIODevice::ReadOnly)) { + qDebug() << "Could not find or open broker.json"; + return false; + } + const QByteArray data = f.readAll(); + if (data.isEmpty()) { + qDebug() << "broker.json file empty"; + return false; + } + QJsonDocument doc = QJsonDocument::fromJson(data); + if (doc.isNull()) { + qDebug() << "broker.json does not contain valid data"; + return false; + } + gBroker = doc["broker"].toString(); + gPort = doc["port"].toInt(); + gUser = doc["user"].toString(); + gPassword = doc["password"].toString(); + qDebug() << "broker.json parsed. Using broker:" << gBroker << ":" << gPort; + resolved = true; + return true; + } +} + +QString MqttCredentials::getBroker() +{ + resolveCredentials(); + return gBroker; +} + +int MqttCredentials::getPort() +{ + resolveCredentials(); + return gPort; +} + +QString MqttCredentials::getUsername() +{ + resolveCredentials(); + return gUser; +} + +QString MqttCredentials::getPassword() +{ + resolveCredentials(); + return gPassword; +} |