-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttn-data-read.js
68 lines (60 loc) · 1.72 KB
/
ttn-data-read.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
//---------//
/* Modules */
//---------//
const https = require('https');
const {exec} = require('child_process');
//---------//
/* Globals */
//---------//
//fetchData stuff
const fetchOptions = {
host: 'student-diss.data.thethingsnetwork.org',
path: '/api/v2/query/demo',
headers: {
accept: 'application/json',
authorization: 'key ttn-account-v2.Cx4Re2sKJ897BRDBoTTcWlUFfN4deKyGHmgXIQtmPzI'
}
};
const fetchInterval = (10 * 1000); //10secs
var data = [];
//printData stuff
const printInterval = (11 * 1000); //11secs
//-------------//
/* Entry Point */
//-------------//
setInterval(fetchData, fetchInterval);
setInterval(printData, printInterval);
//-----------//
/* Functions */
//-----------//
function fetchData() {
console.log('fetching data... ' + fetchOptions.host + fetchOptions.path);
https.get(fetchOptions, (response) => {
let responseData = '';
response.on('data', (chunk) => {
responseData += chunk;
});
response.on('end', () => {
if (responseData != '') {
data = JSON.parse(responseData).map((element, index) => {
return [element.time, element.raw];
});
} else {
console.log('No data to retrieve')
}
});
}).on('error', (err) => {
console.error('ERR! fetchData()');
console.error(err);
});
};
function printData() {
for (let i = 0; i < data.length; i++) {
exec('echo '+data[i][1]+' | base64 --decode', (err, stdout, stderr) => {
if (err)
console.error(err);
else
console.log('['+data[i][0]+'] '+stdout.toString('ascii'));
});
}
};