Skip to content

Commit

Permalink
attempt to patch issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenicholl committed Dec 1, 2022
1 parent fa1398b commit 3e347d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"displayName": "Homebridge WeatherFlow Tempest",
"name": "homebridge-weatherflow-tempest",
"version": "1.2.0",
"version": "1.2.1",
"description": "Exposes WeatherFlow Tempest Station data as Temperature Sensors, Light Sensors, Humidity Sensors and Fan Sensors (for Wind Speed).",
"license": "Apache-2.0",
"repository": {
Expand Down
22 changes: 21 additions & 1 deletion src/tempestApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ export class TempestApi {

}

private isResponseGood(response: AxiosResponse) {

try {
if (!response || !response.data) {
return false;
} else if (typeof response.data === 'string') {
return ('obs' in JSON.parse(response.data));
} else {
return ('obs' in response.data);
}
} catch(exception) {
this.log.error(exception as string);
return false;
}

}

public async getStationCurrentObservation(retry_count = 0) {

if (retry_count === this.max_retries) {
Expand All @@ -59,7 +76,7 @@ export class TempestApi {
}

const response: AxiosResponse | undefined = await this.getStationObservation();
if (!response || !response.data || !('obs' in response.data)) {
if (!response || !this.isResponseGood(response as AxiosResponse)) {
this.log.warn('Response missing "obs" data.');
if (this.data !== undefined) {
this.log.warn('Returning last cached response.');
Expand All @@ -69,6 +86,9 @@ export class TempestApi {
await this.delay(1000 * retry_count);
return await this.getStationCurrentObservation(retry_count + 1);
} else {
if (typeof response.data === 'string') {
response.data = JSON.parse(response.data);
}
this.data = response.data['obs'][0];
return this.data;
}
Expand Down

0 comments on commit 3e347d8

Please sign in to comment.