Skip to content

Commit

Permalink
mqtt reconnect interval config
Browse files Browse the repository at this point in the history
  • Loading branch information
kreso975 committed Oct 19, 2024
1 parent 9418713 commit d5f9bb6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -246,6 +252,7 @@
"expandable": true,
"expanded": false,
"items": [
"devices[].mqttReconnectInterval",
"devices[].mqttBroker",
"devices[].mqttPort",
"devices[].mqttUsername",
Expand Down Expand Up @@ -295,6 +302,7 @@
"expandable": true,
"expanded": false,
"items": [
"devices[].mqttReconnectInterval",
"devices[].mqttBroker",
"devices[].mqttPort",
"devices[].mqttUsername",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/platformSensorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -191,6 +193,7 @@ export class platformSensors {
username: this.mqttUsername,
password: this.mqttPassword,
rejectUnauthorized: false,
reconnectPeriod: Number(this.mqttReconnectInterval)*1000,
};

if (this.mqttTemperature) {
Expand Down Expand Up @@ -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();
});

}
Expand Down
16 changes: 10 additions & 6 deletions src/platformSwitchServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -253,6 +255,7 @@ export class platformSwitch {
username: this.mqttUsername,
password: this.mqttPassword,
rejectUnauthorized: false,
reconnectPeriod: Number(this.mqttReconnectInterval)*1000,
};

if (this.mqttSwitch) {
Expand Down Expand Up @@ -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();
});

}
Expand Down

0 comments on commit d5f9bb6

Please sign in to comment.