From d60347011ef235566d388136858890c21ff00822 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 11 Jun 2021 16:13:01 -0600 Subject: [PATCH] add option to allow self-signed certificate with MQTT also log the reason an MQTT connection failed --- defaultConfig.json | 3 ++- web/interfaces/mqttInterface.ts | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/defaultConfig.json b/defaultConfig.json index a11f5ccc..e33f6666 100755 --- a/defaultConfig.json +++ b/defaultConfig.json @@ -121,6 +121,7 @@ "port": 1883, "username": "", "password": "", + "selfSignedCertificate": false, "rootTopic": "@bind=(state.equipment.model).replace(' ','-').replace('/','').toLowerCase();", "retain": true, "qos": 0, @@ -251,4 +252,4 @@ } }, "appVersion": "0.0.1" -} \ No newline at end of file +} diff --git a/web/interfaces/mqttInterface.ts b/web/interfaces/mqttInterface.ts index 8c771f1d..92a93ce2 100644 --- a/web/interfaces/mqttInterface.ts +++ b/web/interfaces/mqttInterface.ts @@ -40,7 +40,6 @@ export class MqttInterfaceBindings extends BaseInterfaceBindings { private subscribed: boolean; // subscribed to events or not private sentInitialMessages = false; private init = () => { - let baseOpts = extend(true, { headers: {} }, this.cfg.options, this.context.options); if ((typeof baseOpts.hostname === 'undefined' || !baseOpts.hostname) && (typeof baseOpts.host === 'undefined' || !baseOpts.host || baseOpts.host === '*')) { logger.warn(`Interface: ${this.cfg.name} has not resolved to a valid host.`); @@ -52,6 +51,7 @@ export class MqttInterfaceBindings extends BaseInterfaceBindings { clientId: this.tokensReplacer(baseOpts.clientId, undefined, toks, { vars: {} } as any, {}), username: baseOpts.username, password: baseOpts.password, + rejectUnauthorized: !baseOpts.selfSignedCertificate, url } this.client = connect(url, opts); @@ -63,6 +63,9 @@ export class MqttInterfaceBindings extends BaseInterfaceBindings { } catch (err) { logger.error(err); } }); + this.client.on('error', (error) => { + logger.error(`MQTT error ${error}`) + }); } public async stopAsync() { try { @@ -458,4 +461,4 @@ export interface IMQTT { class MQTTMessage { topic: string; message: string; -} \ No newline at end of file +}