Skip to content

Commit

Permalink
Custom MQTT topic & response path
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Aug 9, 2023
1 parent 34d2184 commit bd2d88f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
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
21 changes: 14 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,14 +25,18 @@ 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);
Expand Down Expand Up @@ -144,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
5 changes: 4 additions & 1 deletion www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1221,5 +1221,8 @@
"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_title" : "Custom MQTT topic",

This comment has been minimized.

Copy link
@AstaRom

AstaRom Aug 10, 2023

Contributor

Duplicate line))

This comment has been minimized.

Copy link
@awawa-dev

awawa-dev Aug 10, 2023

Author Owner

Good catch!

"edt_conf_mqtt_custom_topic_expl" : "Custom MQTT topic (default is: HyperHDR)"
}

0 comments on commit bd2d88f

Please sign in to comment.