-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·47 lines (40 loc) · 1 KB
/
cli.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
#!/usr/bin/env node
var argv = require('yargs')
.usage('Usage: $0 [sensor ID]')
.option('c', {
alias: 'class',
type: 'boolean',
desc: 'Append the AQI class at the end in parenthesis'
})
.option('j', {
alias: 'json',
type: 'boolean',
desc: 'Output as JSON'
})
.demandCommand(1)
.argv;
const libAqi = require('./lib/aqi');
var sensorId = argv._[0];
libAqi.getSensor(sensorId).then(async function (sensor) {
var aqi = await libAqi.getAQI(sensor);
var output = aqi;
aqiClass = null;
if (argv.class) {
aqiClass = libAqi.getAQIClass(aqi);
output = `${aqi} (${aqiClass})`;
}
if (argv.json) {
var outputJson = {
aqi: aqi,
sensorId: sensorId,
};
if (aqiClass) {
outputJson.class = aqiClass;
}
console.log(JSON.stringify(outputJson, null, 2));
} else {
console.log(output);
}
}).catch(err => {
console.error(err);
});