-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4afd77
commit 24aa978
Showing
7 changed files
with
751 additions
and
622 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import eslint from '@eslint/js'; | ||
import stylistic from '@stylistic/eslint-plugin'; | ||
import tseslint from 'typescript-eslint'; | ||
|
||
export default tseslint.config( | ||
eslint.configs.recommended, | ||
...tseslint.configs.recommended, | ||
eslint.configs.recommended, | ||
stylistic.configs.customize({ | ||
arrowParens: false, | ||
blockSpacing: true, | ||
braceStyle: 'stroustrup', | ||
commaDangle: 'never', | ||
flat: true, | ||
indent: 4, | ||
jsx: false, | ||
quoteProps: 'consistent-as-needed', | ||
quotes: 'single', | ||
semi: true | ||
}), | ||
...tseslint.configs.strict, | ||
...tseslint.configs.stylistic | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,66 @@ | ||
import axios from "axios"; | ||
import { AccessToken, ClientCredentials } from "simple-oauth2"; | ||
import axios from 'axios'; | ||
import { AccessToken, ClientCredentials } from 'simple-oauth2'; | ||
|
||
export class AirthingsApi { | ||
private accessToken?: AccessToken; | ||
private accessToken?: AccessToken; | ||
|
||
private readonly client?: ClientCredentials; | ||
private readonly tokenScope: string; | ||
private readonly client?: ClientCredentials; | ||
private readonly tokenScope: string; | ||
|
||
constructor(tokenScope: string, clientId?: string, clientSecret?: string) { | ||
this.tokenScope = tokenScope; | ||
constructor(tokenScope: string, clientId?: string, clientSecret?: string) { | ||
this.tokenScope = tokenScope; | ||
|
||
if (clientId == null || clientSecret == null) { | ||
return; | ||
} | ||
|
||
const config = { | ||
client: { | ||
id: clientId, | ||
secret: clientSecret | ||
}, | ||
auth: { | ||
tokenHost: "https://accounts.airthings.com", | ||
tokenPath: "https://accounts-api.airthings.com/v1/token" | ||
} | ||
}; | ||
if (clientId == null || clientSecret == null) { | ||
return; | ||
} | ||
|
||
this.client = new ClientCredentials(config); | ||
} | ||
const config = { | ||
client: { | ||
id: clientId, | ||
secret: clientSecret | ||
}, | ||
auth: { | ||
tokenHost: 'https://accounts.airthings.com', | ||
tokenPath: 'https://accounts-api.airthings.com/v1/token' | ||
} | ||
}; | ||
|
||
public async getLatestSamples(id: string) { | ||
if (this.client == null) { | ||
throw new Error("Airthings API Client not initialized due to invalid configuration..."); | ||
this.client = new ClientCredentials(config); | ||
} | ||
|
||
if (this.accessToken == null || this.accessToken?.expired(300)) { | ||
const tokenParams = { | ||
scope: this.tokenScope | ||
}; | ||
this.accessToken = await this.client.getToken(tokenParams); | ||
} | ||
public async getLatestSamples(id: string) { | ||
if (this.client == null) { | ||
throw new Error('Airthings API Client not initialized due to invalid configuration...'); | ||
} | ||
|
||
const requestConfig = { | ||
headers: { "Authorization": `${this.accessToken.token.access_token}` } | ||
}; | ||
if (this.accessToken == null || this.accessToken?.expired(300)) { | ||
const tokenParams = { | ||
scope: this.tokenScope | ||
}; | ||
this.accessToken = await this.client.getToken(tokenParams); | ||
} | ||
|
||
const response = await axios.get<AirthingsApiDeviceSample>(`https://ext-api.airthings.com/v1/devices/${id}/latest-samples`, requestConfig); | ||
return response.data; | ||
} | ||
const requestConfig = { | ||
headers: { Authorization: `${this.accessToken.token.access_token}` } | ||
}; | ||
|
||
const response = await axios.get<AirthingsApiDeviceSample>(`https://ext-api.airthings.com/v1/devices/${id}/latest-samples`, requestConfig); | ||
return response.data; | ||
} | ||
} | ||
|
||
export interface AirthingsApiDeviceSample { | ||
data: { | ||
battery?: number; | ||
co2?: number; | ||
humidity?: number; | ||
mold?: number; | ||
pm1?: number; | ||
pm25?: number; | ||
pressure?: number; | ||
radonShortTermAvg?: number; | ||
temp?: number; | ||
time?: number; | ||
voc?: number; | ||
} | ||
data: { | ||
battery?: number; | ||
co2?: number; | ||
humidity?: number; | ||
mold?: number; | ||
pm1?: number; | ||
pm25?: number; | ||
pressure?: number; | ||
radonShortTermAvg?: number; | ||
temp?: number; | ||
time?: number; | ||
voc?: number; | ||
}; | ||
} |
Oops, something went wrong.