Skip to content

Commit

Permalink
Improve error handling for issues during data processing, fixes #237
Browse files Browse the repository at this point in the history
  • Loading branch information
DutchmanNL committed Oct 27, 2023
1 parent 940b98a commit dd26794
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const utils = require('@iobroker/adapter-core');
const request = require('request-promise-native');
const stateAttr = require(__dirname + '/lib/stateAttr.js');
const settings = { Username: "", Password: "", intervall: 30000 }, warnMessages = {};
let isConnected = false;
let timer = null;

const disableSentry = false; // Ensure to set to true during development !
Expand Down Expand Up @@ -42,7 +43,8 @@ class Discovergy extends utils.Adapter {
settings.Password = this.config.Password;
settings.intervall = (1000 * this.config.intervall);

await this.setState('info.connection', false, true);
this.setState('info.connection', false, true);
isConnected = false

this.log.info('Discovergy Adapter startet, trying to discover meters associated with your account');

Expand Down Expand Up @@ -72,6 +74,7 @@ class Discovergy extends utils.Adapter {

// We got a response API is
await this.setState('info.connection', true, true);
isConnected = true;

// Retrieve all meter objects from Discovergy API
/** @type {Record<string, any>[]} */
Expand Down Expand Up @@ -150,8 +153,8 @@ class Discovergy extends utils.Adapter {
if (!error && response.statusCode === 200) {
// we got a response

const result = body;
const data = JSON.parse(result);
this.log.debug(`[doDiscovergyMeter] Data : ${JSON.stringify(body)}`)
const data = JSON.parse(body);

for (const attributes in data) {

Expand Down Expand Up @@ -222,12 +225,19 @@ class Discovergy extends utils.Adapter {
}
}

this.setState('info.connection', true, true);
isConnected = true

} else { // error or non-200 status code
this.log.error('Error retrieving information for : ' + meterId);
this.log.error('[doDiscovergyMeter] Error retrieving information for : ' + meterId);
this.setState('info.connection', false, true);
isConnected = false
}
});
} catch (error) {
console.error(error);
this.log.error(`[doDiscovergyMeter] ${error}`);
this.setState('info.connection', false, true);
isConnected = false
}
}

Expand Down

0 comments on commit dd26794

Please sign in to comment.