Skip to content

Commit

Permalink
Optimize Websocket disconnect cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Jan 3, 2025
1 parent 4dbae51 commit 8834df5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ https://forum.iobroker.net/topic/27532/homematic-ip-cloud-access-point-adapter
### **WORK IN PROGRESS**
-->
## Changelog
### __WORK IN PROGRESS__
* (@Apollon77) Optimize Websocket disconnect cases

### 1.26.3 (2024-12-29)
* (@GermanBluefox) Updated packages

Expand Down
16 changes: 11 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HmIpCloudAccesspointAdapter extends Adapter {

_unload(callback) {
this._unloaded = true;
this.expectWsError && clearTimeout(this.expectWsError);
this.reInitTimeout && clearTimeout(this.reInitTimeout);
this.reInitDataTimeout && clearTimeout(this.reInitDataTimeout);
this._api.dispose();
Expand Down Expand Up @@ -770,18 +771,23 @@ class HmIpCloudAccesspointAdapter extends Adapter {
}, 5000); // set null when connection is stable
}

_closed(code, reason) {
_closed(code, reason, forced = false) {
if (this.wsConnectionStableTimeout || !this.wsConnected) {
this.wsConnectionErrorCounter++;
} else {
this.wsConnectionErrorCounter = 0;
}
reason = reason ? reason.toString() : '';
this.log.warn(`ws connection closed (${this.wsConnectionErrorCounter}) - code: ${code} - reason: ${reason}`);
if (!forced) {
this.log.warn(`ws connection closed (${this.wsConnectionErrorCounter}) - code: ${code} - reason: ${reason}`);

Check failure on line 782 in main.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Replace ``ws·connection·closed·(${this.wsConnectionErrorCounter})·-·code:·${code}·-·reason:·${reason}`` with `⏎················`ws·connection·closed·(${this.wsConnectionErrorCounter})·-·code:·${code}·-·reason:·${reason}`,⏎············`
}
this.wsConnected = false;
if (this.wsConnectionErrorCounter > 6 && !this._unloaded) {
this.expectWsError && clearTimeout(this.expectWsError);
// When no error happens within 5 seconds, we refresh our self
this.expectWsError = setTimeout(() => this._closed(code, reason, true), 5000);
if ((forced || this.wsConnectionErrorCounter > 6) && !this._unloaded) {
this._api.dispose();
this.log.error(`error updating Homematic ip: ${code} - ${reason}`);
this.log.error(`close on websocket connection: ${code} - ${reason}`);
this.log.error('Try reconnect in 30s');
this.reInitTimeout && clearTimeout(this.reInitTimeout);
this.reInitTimeout = setTimeout(async () => {
Expand All @@ -799,7 +805,7 @@ class HmIpCloudAccesspointAdapter extends Adapter {
}
if (reason.includes('ECONNREFUSED') && !this._unloaded) {
this._api.dispose();
this.log.error(`error updating homematic ip: ${reason}`);
this.log.error(`error on websocket connection: ${reason}`);
this.log.error('Try reconnect in 30s');
this.reInitTimeout && clearTimeout(this.reInitTimeout);
this.reInitTimeout = setTimeout(() => {
Expand Down

0 comments on commit 8834df5

Please sign in to comment.