-
Notifications
You must be signed in to change notification settings - Fork 1
/
Airly.mjs
56 lines (48 loc) · 1.91 KB
/
Airly.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import _ from "lodash";
import * as db from "./db.mjs";
async function fetchWithTimeout(resource, options = {}) {
const {timeout = 1500} = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
}
export async function getAirlyData(logToDatabase, airlyApiKey, latitude, longitude) {
try {
const airlyResponse = await fetchWithTimeout("https://airapi.airly.eu/v2/measurements/point?" + "lat=" + latitude.toString() + "&lng=" + longitude.toString(), {
method: 'GET',
headers: {
'apikey': airlyApiKey
}
})
const airlyData = await airlyResponse.json();
try {
let fromDateTime = new Date(airlyData.current.fromDateTime);
let tillDateTime = new Date(airlyData.current.fromDateTime);
let values = airlyData.current.values;
const date = new Date();
const temperature = _.find(values, {name: 'TEMPERATURE'}).value;
const humidity = _.find(values, {name: 'HUMIDITY'}).value;
const pressure = _.find(values, {name: 'PRESSURE'}).value;
const pm25 = _.find(values, {name: 'PM25'}).value;
const pm10 = _.find(values, {name: 'PM10'}).value;
const pm1 = _.find(values, {name: 'PM1'}).value;
let data = {date, temperature, humidity, pressure, pm25, pm10, pm1};
if (logToDatabase) {
try {
await db.Airly.create(data);
} catch (e) {
console.log("Database insert error");
}
}
} catch (e) {
console.log("Airly parsing error");
}
} catch (e) {
console.log("Airly Timeout");
}
}