Skip to content

Commit

Permalink
#376 Handling when reconnect fails
Browse files Browse the repository at this point in the history
  • Loading branch information
theimo1221 committed Oct 16, 2022
1 parent be6b72a commit 067921b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 43 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ It is recommended that you add your own tests into the mix.
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->

### **WORK IN PROGRESS**

* (theimo1221) #376 Handling when reconnect fails

### 3.1.3 (2022-10-04)

* (theimo1221) Update Packages
Expand Down
101 changes: 68 additions & 33 deletions build/lib/ringApiClient.js

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

2 changes: 1 addition & 1 deletion build/lib/ringApiClient.js.map

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

52 changes: 43 additions & 9 deletions src/lib/ringApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class RingApiClient {
public refreshing = false;
private devices: { [id: string]: OwnRingDevice } = {};
private _refreshInterval: NodeJS.Timer | null = null;
private _retryTimeout: NodeJS.Timer | null = null;

get locations(): { [id: string]: OwnRingLocation } {
return this._locations;
Expand Down Expand Up @@ -77,7 +78,23 @@ export class RingApiClient {
this.refreshing = true;
this._api?.disconnect();
this._api = undefined;
await this.retrieveLocations();
if (!await this.retrieveLocations()) {
if (initial) {
this.adapter.terminate(`Failed to retrieve any locations for your ring Account.`);
} else {
if (this._retryTimeout !== null) {
clearTimeout(this._retryTimeout);
this._retryTimeout = null;
}
this.warn(`Couldn't load data from Ring Server on reconnect, will retry in 5 Minutes...`)
this._retryTimeout = setTimeout(this.refreshAll.bind(this), 5 * 60 * 1000);
}
} else {
if (this._retryTimeout !== null) {
clearTimeout(this._retryTimeout);
this._retryTimeout = null;
}
}
if (Object.keys(this._locations).length === 0 && initial) {
this.adapter.terminate(`We couldn't find any locations in your Ring Account`);
return;
Expand Down Expand Up @@ -115,17 +132,34 @@ export class RingApiClient {
clearInterval(this._refreshInterval);
this._refreshInterval = null;
}
if (this._retryTimeout !== null) {
clearTimeout(this._retryTimeout);
this._retryTimeout = null;
}
}

private async retrieveLocations(): Promise<void> {
private async retrieveLocations(): Promise<boolean> {
this.debug(`Retrieve Locations`);
const locs = await (await this.getApi()).getLocations().catch(this.handleApiError.bind(this));
this.debug(`Recieved ${locs?.length} Locations`);
this._locations = {};
for(const loc of locs as Location[]) {
const newLoc = new OwnRingLocation(loc, this.adapter, this);
this._locations[newLoc.fullId] = newLoc;
}
return new Promise<boolean>(async (res) => {
(await this.getApi()).getLocations()
.catch((reason) => {
this.handleApiError(reason)
res(false);
})
.then((locs) => {
if (typeof locs != "object" || (locs?.length ?? 0) == 0) {
this.debug("getLocations was successful, but received no array")
res(false);
}
this.debug(`Received ${locs?.length} Locations`);
this._locations = {};
for (const loc of locs as Location[]) {
const newLoc = new OwnRingLocation(loc, this.adapter, this);
this._locations[newLoc.fullId] = newLoc;
}
res(true);
});
});
}

private handleApiError(reason: any): void {
Expand Down

0 comments on commit 067921b

Please sign in to comment.