From 50975b443e654d29a9e5b1d936c69c420d4b01c5 Mon Sep 17 00:00:00 2001 From: Zoltan Borsos Date: Fri, 12 May 2023 17:10:49 +0200 Subject: [PATCH] Added option to specify custom clientId for the MqttClient --- app.js | 1 + lib/MqttClient.js | 6 ++++++ lib/res/default_config.json | 1 + 3 files changed, 8 insertions(+) diff --git a/app.js b/app.js index 5f36022..8afbd70 100644 --- a/app.js +++ b/app.js @@ -24,6 +24,7 @@ if (conf.get("mqtt")) { // eslint-disable-next-line no-unused-vars const mqttClient = new MqttClient({ brokerURL: conf.get("mqtt").broker_url, + clientId: conf.get("mqtt").clientId, caPath: conf.get("mqtt").caPath, identifier: conf.get("mqtt").identifier, topicPrefix: conf.get("mqtt").topicPrefix, diff --git a/lib/MqttClient.js b/lib/MqttClient.js index 8b63ef0..56a366f 100644 --- a/lib/MqttClient.js +++ b/lib/MqttClient.js @@ -11,6 +11,7 @@ class MqttClient { * * @param {object}options * @param {string} options.brokerURL + * @param {string} options.clientId * @param {string} options.caPath * @param {string} options.identifier * @param {string} options.topicPrefix @@ -27,6 +28,7 @@ class MqttClient { */ constructor(options) { this.brokerURL = options.brokerURL; + this.clientId = options.clientId ?? ""; this.caPath = options.caPath ?? ""; this.identifier = options.identifier ?? "rockrobo"; this.topicPrefix = options.topicPrefix ?? "valetudo"; @@ -64,6 +66,10 @@ class MqttClient { connect() { if (!this.client || (this.client && !this.client.connected && !this.client.reconnecting)) { const options = {}; + if (this.clientId) { + options.clientId = this.clientId; + } + if (this.caPath) { options.ca = fs.readFileSync(this.caPath); } diff --git a/lib/res/default_config.json b/lib/res/default_config.json index 19e7999..57024c2 100644 --- a/lib/res/default_config.json +++ b/lib/res/default_config.json @@ -16,6 +16,7 @@ "topicPrefix": "valetudo", "autoconfPrefix": "homeassistant", "broker_url": "mqtt://user:pass@foobar.example", + "clientId": "", "caPath": "", "mapDataTopic": "valetudo/robot/MapData/map-data", "minMillisecondsBetweenMapUpdates": 10000,