From 4e30eed2733409303b7e11ec9bb20757e6b71c62 Mon Sep 17 00:00:00 2001 From: DEV2DEV_DE Date: Wed, 29 Nov 2023 16:00:53 +0100 Subject: [PATCH] Recurring refresh with timeout instead of interval --- main.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/main.js b/main.js index 623cc23..894febd 100644 --- a/main.js +++ b/main.js @@ -32,7 +32,7 @@ class Jablotron extends utils.Adapter { this.connected = false; this.sessionId = ''; - this.refreshInterval = undefined; + this.timeout = null this.states = []; axios.defaults.withCredentials = true; // force axios to use cookies @@ -77,16 +77,8 @@ class Jablotron extends utils.Adapter { // create interval for recurring tasks if (this.connected) { - this.log.debug('Setting up polling interval'); - this.refreshInterval = this.setInterval(() => { - try { - this.log.debug('Fetch data from jablonet.net'); - this.getExtendedData(headers, this.sessionId); - } catch (error) { - this.log.error('Error in polling interval: ' + error); - this.connected = false; - } - }, this.config.pollInterval * 1000); + this.log.debug('Setting up recurring refresh'); + this.recurringRefresh(); // subscribe to all state changes // this.subscribeStates('status.alarm'); } else { @@ -157,6 +149,18 @@ class Jablotron extends utils.Adapter { } } + /** + * Recursively refreshes data at a specified interval. + * @returns {void} + */ + async recurringRefresh() { + this.timeout = setTimeout(() => { + this.log.debug('Fetch data from jablonet.net'); + this.getExtendedData(headers, this.sessionId); + this.recurringRefresh(); + }, this.config.pollInterval * 1000); + } + /** * read all services related to the account * @param {object} headers @@ -346,7 +350,7 @@ class Jablotron extends utils.Adapter { */ onUnload(callback) { try { - this.clearInterval(this.refreshInterval); + this.clearTimeout(this.timeout); this.connected = false; callback(); } catch (e) {