-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-devices.js
38 lines (36 loc) · 1010 Bytes
/
list-devices.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
"use strict";
const config = require('./config');
const fetch = require('node-fetch');
const apiUrl = config.api.host+config.api.path;
console.log("using api:",apiUrl);
//fetch list of registered devices
fetch(apiUrl+"/devices")
//get payload from response
.then(response => response.json())
//list all devices
.then(result => {
if (!Array.isArray(result)) {
throw new Error("expected array of devices")
}
if (result.length == 0) {
throw new Error("no devices found")
}
return result;
})
.then(result => {
result.forEach(device => {
console.log("====",device.name,"====");
console.log(" id:",device.id);
console.log(" type:",device.type);
console.log(" sensors:");
device.sensors
.filter(sensor=> sensor.primary)
.map(sensor => {
console.log(" ",sensor.name,"in",sensor.unit,"(id:"+sensor.id+")");
});
console.log();
});
})
.catch(err=>{
console.log("Error:",err.message);
});