summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tradeshow/iot-sensortag/SensorTagDemo.pro4
-rw-r--r--tradeshow/iot-sensortag/mqttdataproviderpool.cpp69
-rw-r--r--tradeshow/iot-sensortag/mqttdataproviderpool.h13
-rw-r--r--tradeshow/iot-sensortag/mqttupdate.cpp8
4 files changed, 79 insertions, 15 deletions
diff --git a/tradeshow/iot-sensortag/SensorTagDemo.pro b/tradeshow/iot-sensortag/SensorTagDemo.pro
index 6be190e..7393afc 100644
--- a/tradeshow/iot-sensortag/SensorTagDemo.pro
+++ b/tradeshow/iot-sensortag/SensorTagDemo.pro
@@ -14,7 +14,7 @@ CONFIG += c++11
DEFINES += QT_NO_FOREACH
# Specify UI layout to use: UI_SMALL or UI_WATCH
-DEFINES += UI_WATCH
+DEFINES += UI_SMALL
# To overcome the bug QTBUG-58648, uncomment this define
# Needed at least for RPi3 and iMX
@@ -28,7 +28,7 @@ win32|linux|android:!qnx {
# For using MQTT upload enable this config.
# This enables both, host and client mode
-# CONFIG += UPDATE_TO_MQTT_BROKER
+CONFIG += UPDATE_TO_MQTT_BROKER
# For using Azure cloud connectivity enable
# this config. This enabled both, host and
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;
+}
diff --git a/tradeshow/iot-sensortag/mqttdataproviderpool.h b/tradeshow/iot-sensortag/mqttdataproviderpool.h
index 1ba33ff..3004757 100644
--- a/tradeshow/iot-sensortag/mqttdataproviderpool.h
+++ b/tradeshow/iot-sensortag/mqttdataproviderpool.h
@@ -55,15 +55,18 @@
class MqttDataProvider;
-#define MQTT_BROKER ""
-#define MQTT_PORT 1883
-#define MQTT_USERNAME ""
-#define MQTT_PASSWORD ""
+namespace MqttCredentials
+{
+ QString getBroker();
+ int getPort();
+ QString getUsername();
+ QString getPassword();
+}
class MqttDataProviderPool : public DataProviderPool
{
public:
- explicit MqttDataProviderPool(QObject *parent = 0);
+ explicit MqttDataProviderPool(QObject *parent = nullptr);
void startScanning() override;
diff --git a/tradeshow/iot-sensortag/mqttupdate.cpp b/tradeshow/iot-sensortag/mqttupdate.cpp
index 45ea238..87a8416 100644
--- a/tradeshow/iot-sensortag/mqttupdate.cpp
+++ b/tradeshow/iot-sensortag/mqttupdate.cpp
@@ -357,10 +357,10 @@ MqttEventHandler::MqttEventHandler(const QString &name, QObject *parent)
connect(&m_pingTimer, &QTimer::timeout, this, &MqttEventHandler::sendAlive);
m_client = new QMqttClient;
- 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(MqttCredentials::getPort());
+ m_client->setUsername(MqttCredentials::getUsername());
+ m_client->setPassword(MqttCredentials::getPassword());
m_client->setWillMessage(QString::fromLocal8Bit("%1>Offline").arg(m_deviceName).toLocal8Bit());
m_client->setWillQoS(1);