Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mqtt custom topic #620

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions include/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class mqtt : public QObject
~mqtt();

public slots:
void start(QString host, int port, QString username, QString password, bool is_ssl, bool ignore_ssl_errors);
void start(QString host, int port, QString username, QString password, bool is_ssl, bool ignore_ssl_errors, QString customTopic);

void stop();

Expand All @@ -36,8 +36,11 @@ private slots:

private:

/// Logger instance
int _jsonPort;
Logger* _log;
// HyperHDR MQTT topic & reponse path
QString HYPERHDRAPI;
QString HYPERHDRAPI_RESPONSE;

int _jsonPort;
Logger* _log;
QMQTT::Client* _clientInstance;
};
8 changes: 8 additions & 0 deletions sources/base/schema/schema-mqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
}
},
"propertyOrder" : 7
},
"custom_topic" :
{
"type": "string",
"title":"edt_conf_mqtt_custom_topic_title",
"required" : true,
"default" : "",
"propertyOrder" : 8
}
},
"additionalProperties" : false
Expand Down
25 changes: 18 additions & 7 deletions sources/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <QUrl>
#include <QHostInfo>

QString HYPERHDRAPI = QStringLiteral("HyperHDR/JsonAPI");
QString HYPERHDRAPI_RESPONSE = QStringLiteral("HyperHDR/JsonAPI/response");
// default param %1 is 'HyperHDR', do not edit templates here
const static QString TEMPLATE_HYPERHDRAPI = QStringLiteral("%1/JsonAPI");
const static QString TEMPLATE_HYPERHDRAPI_RESPONSE = QStringLiteral("%1/JsonAPI/response");

mqtt::mqtt(QObject* _parent)
: QObject(_parent)
Expand All @@ -24,16 +25,24 @@ mqtt::~mqtt()
stop();
}

void mqtt::start(QString host, int port, QString username, QString password, bool is_ssl, bool ignore_ssl_errors)
void mqtt::start(QString host, int port, QString username, QString password, bool is_ssl, bool ignore_ssl_errors, QString customTopic)
{

if (_clientInstance != nullptr)
return;

Debug(_log, "Starting the MQTT connection. Address: %s:%i. Protocol: %s. Authentication: %s, Ignore errors: %s",
QSTRING_CSTR(host), port, (is_ssl) ? "SSL": "NO SSL", (!username.isEmpty() || !password.isEmpty()) ? "YES" : "NO", (ignore_ssl_errors) ? "YES" : "NO");
HYPERHDRAPI = QString(TEMPLATE_HYPERHDRAPI).arg(customTopic);
HYPERHDRAPI_RESPONSE = QString(TEMPLATE_HYPERHDRAPI_RESPONSE).arg(customTopic);

Debug(_log, "Starting the MQTT connection. Address: %s:%i. Protocol: %s. Authentication: %s, Ignore errors: %s, MQTT topic: %s, MQTT response: %s",
QSTRING_CSTR(host), port, (is_ssl) ? "SSL": "NO SSL", (!username.isEmpty() || !password.isEmpty()) ? "YES" : "NO", (ignore_ssl_errors) ? "YES" : "NO",
QSTRING_CSTR(HYPERHDRAPI), QSTRING_CSTR(HYPERHDRAPI_RESPONSE));

QHostAddress adr(host);
QHostInfo info = QHostInfo::fromName(host);
if (!info.addresses().isEmpty()) {
adr = info.addresses().first();
}


if (is_ssl)
Expand Down Expand Up @@ -140,11 +149,13 @@ void mqtt::handleSettingsUpdate(settings::type type, const QJsonDocument& config
QString password = obj["password"].toString();
bool is_ssl = obj["is_ssl"].toBool();
bool ignore_ssl_errors = obj["ignore_ssl_errors"].toBool();

QString customTopic = obj["custom_topic"].toString().trimmed();
if (customTopic.isEmpty())
customTopic = "HyperHDR";

stop();
if (enabled)
start(host, port, username, password, is_ssl, ignore_ssl_errors);
start(host, port, username, password, is_ssl, ignore_ssl_errors, customTopic);
}
else if (type == settings::type::WEBSERVER)
{
Expand Down
4 changes: 3 additions & 1 deletion www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1221,5 +1221,7 @@
"warning_lut_not_found_3" : "Download & install LUT with one click",
"warning_errors_detected_t" : "Some errors found in the log",
"warning_errors_detected_1" : "The last one:",
"warning_errors_detected_2" : "Some errors are transient (e.g. with the auto-reconnect LED enabled) and can be ignored, others may indicate serious system configuration or operation errors.<br/>Check the logs and look for errors (in red):"
"warning_errors_detected_2" : "Some errors are transient (e.g. with the auto-reconnect LED enabled) and can be ignored, others may indicate serious system configuration or operation errors.<br/>Check the logs and look for errors (in red):",
"edt_conf_mqtt_custom_topic_title" : "Custom MQTT topic",
"edt_conf_mqtt_custom_topic_expl" : "Custom MQTT topic (default is: HyperHDR)"
}