-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (65 loc) · 2.19 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const axios = require('axios');
let clientTransactionId = 1;
exports.getStatus = async (config) => {
let weatherProperties = [
'temperature',
'dewPoint',
'windSpeed',
'windDirection',
'windGust',
'cloudCover',
'humidity',
'skyBrightness',
'rainRate',
'skyQuality',
'skyTemperature',
'starFwhm',
'pressure'
];
return axios.all(weatherProperties.map(prop => getStat(config, prop.toLowerCase())))
.then((results) => {
let weather = {};
results.forEach((result, i) => {
let weatherProperty = weatherProperties[i];
weather[weatherProperty] = null;
let baseErrorMessage = `\t\tError occurred in "${weatherProperty.toLowerCase()}" call to weather equipment`;
if (!result) {
console.error(baseErrorMessage);
} else if (result.status !== 200) {
console.error(`${baseErrorMessage}: Error status ${result.status} - ${result.data}`);
} else if (result.data.ErrorNumber !== 0) {
console.error(`${baseErrorMessage}: Error ${result.data.ErrorNumber} - ${result.data.ErrorMessage}`);
} else {
weather[weatherProperty] = roundToPrecision(result.data.Value, config.precision || 6);
}
});
return weather;
})
.catch((error) => {
if (error.message) {
error.message = `Error occurred in "getStatus" call to weather equipment`;
}
console.error(error);
throw error;
});
};
let getStat = (config, stat) => {
let url = new URL(`${config.baseUrl}/${config.deviceNumber}/${stat}`);
let searchParams = new URLSearchParams();
searchParams.append('clienttransactionid', clientTransactionId);
if (config.clientId) {
searchParams.append('clientid', config.clientId);
}
url.search = searchParams;
let response = axios.get(url.toString())
.catch((error) => {
// TODO: Return something more meaningful for logging puropses?
return error.response;
});
clientTransactionId++;
return response;
};
// We're going to round our numbers up (in the positive direction) to the nearest <precision>th place
let roundToPrecision = (num, precision) => {
return +Number.parseFloat(num).toPrecision(precision);
};