diff --git a/config.schema.json b/config.schema.json index a9f260f..9a82046 100644 --- a/config.schema.json +++ b/config.schema.json @@ -173,6 +173,12 @@ "type": "string", "required": false }, + "mqttReconnectInterval": { + "title": "Reconnect interval (sec)", + "description": "60", + "type": "number", + "required": false + }, "discordWebhook": { "title": "Discord WebHook", "description": "URL to Discord WebHook", @@ -246,6 +252,7 @@ "expandable": true, "expanded": false, "items": [ + "devices[].mqttReconnectInterval", "devices[].mqttBroker", "devices[].mqttPort", "devices[].mqttUsername", @@ -295,6 +302,7 @@ "expandable": true, "expanded": false, "items": [ + "devices[].mqttReconnectInterval", "devices[].mqttBroker", "devices[].mqttPort", "devices[].mqttUsername", diff --git a/package.json b/package.json index a709ea4..ff61462 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "homebridge-http-sensors-switches", "displayName": "Http Sensors and Switches", "type": "module", - "version": "1.1.5", + "version": "1.1.6", "private": false, "description": "Http JSON for Sensors and Url for Switches", "author": "Kresimir Kokanovic", diff --git a/src/platformSensorServices.ts b/src/platformSensorServices.ts index b193d6f..58dfe41 100644 --- a/src/platformSensorServices.ts +++ b/src/platformSensorServices.ts @@ -29,6 +29,7 @@ export class platformSensors { public humidityName: string = ''; public airPressureName: string = ''; + public mqttReconnectInterval: string = ''; public mqttBroker: string = ''; public mqttPort: string = ''; public mqttTemperature: string = ''; @@ -60,6 +61,7 @@ export class platformSensors { this.airPressureName = this.accessory.context.device.airPressureName; this.updateInterval = accessory.context.device.updateInterval || 300000; // Default update interval is 300 seconds + this.mqttReconnectInterval = this.accessory.context.device.mqttReconnectInterval || 60; // 60 sec default this.mqttBroker = accessory.context.device.mqttBroker; this.mqttPort = accessory.context.device.mqttPort; this.mqttTemperature = accessory.context.device.mqttTemperature; @@ -191,6 +193,7 @@ export class platformSensors { username: this.mqttUsername, password: this.mqttPassword, rejectUnauthorized: false, + reconnectPeriod: Number(this.mqttReconnectInterval)*1000, }; if (this.mqttTemperature) { @@ -246,7 +249,8 @@ export class platformSensors { // Handle errors this.mqttClient.on('error', (err) => { this.platform.log.warn(this.deviceName,': Connection error:', err); - this.mqttClient.end(); + this.platform.log.warn(this.deviceName, ': Reconnecting in: ', this.mqttReconnectInterval, ' seconds.'); + //this.mqttClient.end(); }); } diff --git a/src/platformSwitchServices.ts b/src/platformSwitchServices.ts index de074da..571b7ac 100644 --- a/src/platformSwitchServices.ts +++ b/src/platformSwitchServices.ts @@ -32,6 +32,7 @@ export class platformSwitch { public statusOnCheck: string = ''; public statusOffCheck: string = ''; + public mqttReconnectInterval: string = ''; public mqttBroker: string = ''; public mqttPort: string = ''; public mqttSwitch: string = ''; @@ -68,11 +69,12 @@ export class platformSwitch { this.urlON = this.accessory.context.device.urlON; this.urlOFF = this.accessory.context.device.urlOFF; - this.mqttBroker = accessory.context.device.mqttBroker; - this.mqttPort = accessory.context.device.mqttPort; - this.mqttSwitch = accessory.context.device.mqttSwitch; - this.mqttUsername = accessory.context.device.mqttUsername; - this.mqttPassword = accessory.context.device.mqttPassword; + this.mqttReconnectInterval = this.accessory.context.device.mqttReconnectInterval || 60; // 60 sec default + this.mqttBroker = this.accessory.context.device.mqttBroker; + this.mqttPort = this.accessory.context.device.mqttPort; + this.mqttSwitch = this.accessory.context.device.mqttSwitch; + this.mqttUsername = this.accessory.context.device.mqttUsername; + this.mqttPassword = this.accessory.context.device.mqttPassword; this.discordWebhook = this.accessory.context.device.discordWebhook; this.discordUsername = this.accessory.context.device.discordUsername || 'StergoSmart'; @@ -253,6 +255,7 @@ export class platformSwitch { username: this.mqttUsername, password: this.mqttPassword, rejectUnauthorized: false, + reconnectPeriod: Number(this.mqttReconnectInterval)*1000, }; if (this.mqttSwitch) { @@ -309,7 +312,8 @@ export class platformSwitch { // Handle errors this.mqttClient.on('error', (err) => { this.platform.log.warn(this.deviceName, ': Connection error:', err); - this.mqttClient.end(); + this.platform.log.warn(this.deviceName, ': Reconnecting in: ', this.mqttReconnectInterval, ' seconds.'); + //this.mqttClient.end(); }); }