Skip to content

Commit

Permalink
some error handling & load inactive devices
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualmarc committed Feb 9, 2024
1 parent 84b5a45 commit 42475f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Get sensor data from the Airthings Cloud (via Airthings Hub).

* (virtualmarc) fixed rssi

### 1.1.3

* (virtualmarc) some error handling
* (virtualmarc) load inactive devices

## License

MIT License
Expand Down
4 changes: 4 additions & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"1.1.2": {
"en": "fixed rssi",
"de": "Signalstärke korrigiert"
},
"1.1.3": {
"en": "some error handling & load inactive devices",
"de": "Fehlerbehandlung & inaktive Geräte laden"
}
},
"title": "Airthings Cloud",
Expand Down
38 changes: 25 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class AirthingsCloud extends utils.Adapter {
}

private async syncDevices(): Promise<void> {
const resp = await axios.get(`${API_BASE}/v1/devices`, {
const resp = await axios.get(`${API_BASE}/v1/devices?showInactive=true`, {
headers: {
Authorization: `Bearer ${await this.getToken()}`
}
Expand Down Expand Up @@ -349,15 +349,19 @@ class AirthingsCloud extends utils.Adapter {
const devices = await this.getDevicesAsync();

for (const device of devices) {
const type = (await this.getStateAsync(`${device._id}.type`))?.val as string;
const active = (await this.getStateAsync(`${device._id}.segment.active`))?.val;
try {
const type = (await this.getStateAsync(`${device._id}.type`))?.val as string;
const active = (await this.getStateAsync(`${device._id}.segment.active`))?.val;

if (active && !EXCLUDED_TYPES.includes(type ?? 'UNKNOWN')) {
this.log.debug(`Update device samples: ${device._id}`);
if (active && !EXCLUDED_TYPES.includes(type ?? 'UNKNOWN')) {
this.log.debug(`Update device samples: ${device._id}`);

await this.updateDeviceSamples(device._id);
} else {
this.log.debug(`Device ${device._id} is excluded`);
await this.updateDeviceSamples(device._id);
} else {
this.log.debug(`Device ${device._id} is excluded`);
}
} catch (ex) {
this.log.error(`Failed to update device ${device._id} ` + ex);
}
}
}
Expand Down Expand Up @@ -437,9 +441,13 @@ class AirthingsCloud extends utils.Adapter {
private updateTimer(): void {
this.log.debug('Update samples');

this.updateSamples().then(() => {
this.log.debug('Sample update finished');
});
try {
this.updateSamples().then(() => {
this.log.debug('Sample update finished');
});
} catch (ex) {
this.log.error('Failed to update samples ' + ex);
}
}

/**
Expand All @@ -453,9 +461,13 @@ class AirthingsCloud extends utils.Adapter {
return;
}

await this.syncDevices();
try {
await this.syncDevices();

await this.updateSamples();
await this.updateSamples();
} catch (ex) {
this.log.error('Error on initial sync ' + ex);
}

this.updateTimerId = this.setInterval(() => this.updateTimer(), this.config.update_interval * 60_000);
}
Expand Down

0 comments on commit 42475f0

Please sign in to comment.