Skip to content

Commit

Permalink
Merge pull request #270 from Grizzelbee/rewrite
Browse files Browse the repository at this point in the history
Rewrite
  • Loading branch information
Grizzelbee authored Jul 13, 2022
2 parents db0d70e + 693ff2d commit 9c34201
Show file tree
Hide file tree
Showing 7 changed files with 2,454 additions and 82 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ Here is a list of what these raw values stand for:
## Changelog
### **WORK IN PROGRESS**

### V6.3.3 (2022-07-13) (Black Wings)
* (grizzelbee) Fix: [258](https://github.com/Grizzelbee/ioBroker.mielecloudservice/issues/258) Improved error handling in case of line outages
* (grizzelbee) Fix: [269](https://github.com/Grizzelbee/ioBroker.mielecloudservice/issues/269) Removed double decryption of passwords
* (grizzelbee) Chg: Dependencies got updated

### V6.3.2 (2022-06-02) (Black Wings)
* (grizzelbee) New: Added new config option "delayed processing" to prevent overload on less powerful hardware
* (grizzelbee) Fix: changed actions info message during polling to log level debug
Expand Down
14 changes: 13 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"common": {
"name": "mielecloudservice",
"version": "6.3.2",
"version": "6.3.3",
"news": {
"6.3.3": {
"en": "Improved error handling in case of line outages\nRemoved double decryption of passwords\nDependencies got updated",
"de": "Verbesserte Fehlerbehandlung bei Leitungsausfällen\n Doppelte Entschlüsselung von Passwörtern entfernt\n Abhängigkeiten aktualisiert",
"ru": "Улучшена обработка ошибок при отключении линии\nУдалена двойная расшифровка паролей\nОбновлены зависимости",
"pt": "Tratamento de erros aprimorado em caso de interrupções de linha\nRemovida a descriptografia dupla de senhas\nAs dependências foram atualizadas",
"nl": "Verbeterde foutafhandeling in het geval van lijnstoringen \n Dubbele decodering van wachtwoorden verwijderd \n Afhankelijkheden zijn bijgewerkt",
"fr": "Amélioration de la gestion des erreurs en cas de panne de ligne \n Suppression du double déchiffrement des mots de passe \n Les dépendances ont été mises à jour",
"it": "Migliorata la gestione degli errori in caso di interruzioni della linea\nRimossa la doppia decrittazione delle password\nAggiornate le dipendenze",
"es": "Manejo de errores mejorado en caso de cortes de línea \n Eliminado el descifrado doble de contraseñas \n Se actualizaron las dependencias",
"pl": "Ulepszona obsługa błędów w przypadku awarii linii\nUsunięto podwójne odszyfrowywanie haseł\nZaktualizowano zależności",
"zh-cn": "改进了线路中断时的错误处理\n删除了密码的双重解密\n更新了依赖项"
},
"6.3.2": {
"en": "New config option \"delayed processing\" to prevent overload on less powerful hardware\nChanged actions info message during polling to log level debug\nFixed german translation bug \"minutes\" -> \"protokoll\" (thanks to rekorboi)",
"de": "Neue Konfigurationsoption \"verzögerte Verarbeitung\", um eine Überlastung auf weniger leistungsstarker Hardware zu verhindern \nMeldung während des Pollings von Info auf Debug geändert\n Deutscher Übersetzungsfehler \"Minuten\" -> \"Protokoll\" behoben (danke an rekorboi)",
Expand Down
61 changes: 31 additions & 30 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ class Mielecloudservice extends utils.Adapter {
connectionErrorHandlingInProgress=true;
try{
switch (events.readyState) {
case 0: // CONNECTING
case 0: { // CONNECTING
adapter.log.info(`SSE is trying to reconnect but it seems this won't work. So trying myself by closing and reinitializing.`);
events.close();
adapter.initSSE();
while(events.readyState === 0) {
const randomDelay = Math.pow(events.sseErrors, 2) * 1000 + Math.floor(Math.random() * 1000);
timeouts.getEvents = setTimeout(() => {
adapter.initSSE();
adapter.log.info(`Still trying to connect...`);
}, randomDelay);
}
}
break;
case 1: // OPEN
adapter.log.info(`SSE connection is still open or open again. Doing nothing.`);
Expand Down Expand Up @@ -145,32 +152,16 @@ class Mielecloudservice extends utils.Adapter {
events.sseErrors++;
adapter.setState('info.connection', false, true);
adapter.log.debug('Received error message by SSE: ' + JSON.stringify(event));
if (events.sseErrors >= mieleConst.MAX_ERROR_THRESHOLD && !connectionErrorHandlingInProgress){
adapter.log.warn(`More than ${mieleConst.MAX_ERROR_THRESHOLD} connection errors occurred. Handling immediately.`);
adapter.doSSEErrorHandling(adapter, events);
} else {
const randomDelay = (events.sseErrors*1000) + Math.floor(Math.random()*1000);
adapter.log.warn(`An ${typeof event.message != 'undefined'?`error occurred (${event.message})`:'undefined error occurred'}. Handling it in ${(mieleConst.RECONNECT_TIMEOUT+randomDelay)/1000} seconds to give it a chance to solve itself.`);
if (Object.prototype.hasOwnProperty.call(timeouts, 'reconnectDelay')){
clearTimeout(timeouts.reconnectDelay);
}
timeouts.reconnectDelay = setTimeout(function (adapter, events) {
adapter.doSSEErrorHandling(adapter, events);
}, mieleConst.RECONNECT_TIMEOUT+randomDelay, adapter, events);
const randomDelay = ( Math.pow(events.sseErrors, 2) * 1000) + Math.floor(Math.random() * 1000);
if (Object.prototype.hasOwnProperty.call(timeouts, 'reconnectDelay')){
clearTimeout(timeouts.reconnectDelay);
}
});

/**
* code for watchdog
* -> check every 5 minutes whether pings are missing
* */
timeouts.watchdog = setInterval(() => {
const testValue = new Date();
if (Date.parse(testValue.toLocaleString()) - Date.parse(auth.ping.toLocaleString()) >= 60000) {
adapter.log.debug(`Watchdog detected ping failure. Last ping occurred over a minute ago. Trying to handle.`);
adapter.log.warn(`An ${typeof event.message != 'undefined'?`error (#${events.sseErrors}) occurred (${event.message})`:'undefined error occurred'}. Handling it in ${(randomDelay)/1000} seconds to give it a chance to solve itself.`);
timeouts.reconnectDelay = setTimeout(function (adapter, events) {
event.reconnectInterval = randomDelay;
adapter.doSSEErrorHandling(adapter, events);
}
}, mieleConst.WATCHDOG_TIMEOUT);
}, randomDelay, adapter, events);
});
}

/**
Expand Down Expand Up @@ -223,10 +214,6 @@ class Mielecloudservice extends utils.Adapter {
this.setState('info.connection', false, true);
// remember the link to the adapter instance
adapter = this;
// decrypt passwords
this.config.Client_secret = this.decrypt(this.config.Client_secret);
this.config.Miele_pwd = this.decrypt(this.config.Miele_pwd);
//
if (fakeRequests){
const fs=require('fs');
fs.readFile('test/testdata.devices.json', 'utf8', function(err, data) {
Expand Down Expand Up @@ -279,6 +266,20 @@ class Mielecloudservice extends utils.Adapter {
if (adapter.config.sse){
this.log.info(`Registering for all appliance events at Miele API.`);
adapter.initSSE();
/**
* code for watchdog
* -> check every 5 minutes whether pings are missing
* */
this.log.info(`Initializing SSE watchdog.`);
timeouts.watchdog = setInterval(() => {
const testValue = new Date();
if (( testValue.getTime() - auth.ping.getTime() ) >= mieleConst.WATCHDOG_TIMEOUT) {
adapter.log.info(`Watchdog detected ping failure. Last ping occurred over a minute ago. Trying to handle.`);
adapter.setState('info.connection', false, true);
events.close();
adapter.initSSE();
}
}, mieleConst.WATCHDOG_TIMEOUT);
} else {
this.log.info(`Requesting data from Miele API using time based polling every ${adapter.config.pollInterval * adapter.config.pollUnit} Seconds.`);
adapter.doDataPolling(adapter, auth);
Expand Down
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.mielecloudservice",
"version": "6.3.2",
"version": "6.3.3",
"description": "Get your Miele appliances (XGW3000 & WiFiConn@ct) connected",
"author": {
"name": "grizzelbee",
Expand Down Expand Up @@ -30,30 +30,30 @@
"axios": "^0.27.2",
"axios-oauth-client": "^1.5.0",
"eventsource": "^2.0.2",
"flatted": "^3.2.5"
"flatted": "^3.2.6"
},
"devDependencies": {
"@alcalzone/release-script": "^3.5.9",
"@iobroker/adapter-dev": "^1.0.0",
"@iobroker/testing": "^3.0.2",
"@snyk/protect": "^1.946.0",
"@snyk/protect": "^1.972.0",
"@types/chai": "^4.3.1",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.39",
"@types/node": "^18.0.3",
"@types/proxyquire": "^1.3.28",
"@types/sinon": "^10.0.11",
"@types/sinon": "^10.0.12",
"@types/sinon-chai": "^3.2.8",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.16.0",
"eslint": "^8.19.0",
"fs": "^0.0.2",
"gulp": "^4.0.2",
"mocha": "^10.0.0",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0",
"sinon-chai": "^3.7.0",
"typescript": "~4.7.2"
"typescript": "~4.7.4"
},
"main": "main.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion source/mieleConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports.MODE_NORMAL = 0;
module.exports.MODE_SABBATH = 1;
module.exports.RESTART_TIMEOUT = 30; // 30 Seconds
module.exports.WATCHDOG_TIMEOUT = 300000; // 5 Minutes in ms
module.exports.RECONNECT_TIMEOUT = 30000; // 30 Seconds in ms
module.exports.RECONNECT_TIMEOUT = 60000; // 60 Seconds in ms
module.exports.AUTH_CHECK_TIMEOUT = 12*3600*1000; // 12 hours in ms
module.exports.AUTH_CHECK_TIMEOUT_TEST = 30000; // 30 seconds in ms
module.exports.ALL_ACTIONS_DISABLED={'processAction':[],'light':[],'ambientLight':[],'startTime':[],'ventilationStep':[],'programId':[],'targetTemperature':[],'deviceName':false,'powerOn':false,'powerOff':false,'colors':[],'modes':[]};
Expand Down
2,356 changes: 2,355 additions & 1 deletion test/testdata.devices.json

Large diffs are not rendered by default.

0 comments on commit 9c34201

Please sign in to comment.