Skip to content

Commit

Permalink
add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
TA2k committed Mar 21, 2024
1 parent 200e151 commit 0904c09
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 324 deletions.
29 changes: 24 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Json2iob = require('json2iob');
const crypto = require('crypto');
const express = require('express');
const bodyParser = require('body-parser');
const axiosRetry = require('axios-retry').default;

class Tedee extends utils.Adapter {
/**
Expand All @@ -30,11 +31,29 @@ class Tedee extends utils.Adapter {
this.userAgent = 'ioBroker v' + this.version;
this.apiVersion = 'v1.0';
this.json2iob = new Json2iob(this);

this.requestClient = axios.create({
withCredentials: true,
headers: { 'user-agent': this.userAgent },
timeout: 3 * 60 * 1000, //3min client timeout
});
axiosRetry(this.requestClient, {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
onRetry: (retryCount, err, cfg) => {
if (err.response) {
this.log.warn(`Retrying request to ${cfg.url} after ${err.response.status} ${err.response.statusText}`);
} else {
this.log.warn(`Retrying request to ${cfg.url} after ${err.code}`);
}
},
retryCondition: (error) => {
return (
error.code !== 'ECONNABORTED' &&
(!error.response || (error.response.status >= 405 && error.response.status <= 599))
);
},
});
this.updateInterval = null;
this.session = {};
this.states = {
Expand Down Expand Up @@ -375,7 +394,7 @@ class Tedee extends utils.Adapter {
method: 'POST',
url: url,
headers: {
acceot: '*/*',
accept: '*/*',
api_token: this.hashedAPIKey(),
mode: mode || '',
},
Expand All @@ -387,6 +406,10 @@ class Tedee extends utils.Adapter {
this.log.error(error);
error.response && this.log.error(JSON.stringify(error.response.data));
});
this.refreshTimeout && clearTimeout(this.refreshTimeout);
this.refreshTimeout = setTimeout(() => {
this.updateDevices();
}, 10 * 1000);
} else {
if (id.split('.')[3] === 'state') {
const deviceId = id.split('.')[2];
Expand All @@ -402,10 +425,6 @@ class Tedee extends utils.Adapter {
}
}
}
this.refreshTimeout && clearTimeout(this.refreshTimeout);
this.refreshTimeout = setTimeout(() => {
this.updateDevices();
}, 10 * 1000);
}
}
}
Expand Down
Loading

0 comments on commit 0904c09

Please sign in to comment.