Skip to content

Commit

Permalink
improve error handling in case connection to device is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
DutchmanNL committed Oct 31, 2023
1 parent 64183bd commit 7c239aa
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class Esphome extends utils.Adapter {
this.log.info(`ESPHome client ${host} connected`);
// Clear possible present warn messages for device from previous connection
delete warnMessages[host];
this.connectError(host, false);
} catch (e) {
this.log.error(`connection error ${e}`);
}
Expand All @@ -235,6 +236,7 @@ class Esphome extends utils.Adapter {
try {
if (this.deviceInfo[host].deviceName != null) {
this.setState(`${this.deviceInfo[host].deviceName}.info._online`, {val: false, ack: true});
this.connectError(host, true);
this.log.warn(`ESPHome client ${this.deviceInfo[host].deviceInfo.name} disconnected`);
} else {
this.log.warn(`ESPHome client ${host} disconnected`);
Expand All @@ -259,6 +261,7 @@ class Esphome extends utils.Adapter {

client[host].connection.on('data', (/** @type {object} */ data) => {
this.log.debug(`${host} client data ${data}`);
this.connectError(host, false);
});

// Handle device information when connected or information updated
Expand Down Expand Up @@ -489,7 +492,7 @@ class Esphome extends utils.Adapter {
optimisedError = `Client ${this.deviceInfo[host].ip} not reachable !`;
if (!warnMessages[host].connectError) {
this.log.error(optimisedError);
warnMessages[host].connectError = true;
this.connectError(host, true);
}
} else if (error.message.includes('Invalid password')) {
optimisedError = `Client ${host} incorrect password !`;
Expand Down Expand Up @@ -1198,6 +1201,19 @@ class Esphome extends utils.Adapter {
}
}

/**
* Function to handle memory status of connection issues
* @param {string} host host (ip) to which an connection error occured
* @param {boolean} status of cennectionError, true = no connection due to error
*/
connectError(host, status){
if (!warnMessages[host]) {
warnMessages[host] = {
connectError : status};
} else {
warnMessages[host].connectError = status;
}
}
}

if (require.main !== module) {
Expand Down

0 comments on commit 7c239aa

Please sign in to comment.