-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·51 lines (44 loc) · 1.35 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
#!/usr/bin/env node
const exec = require('child_process').exec;
const skygear = require('skygear');
const endPoint = process.env.SKYGEAR_ENDPOINT;
const apiKey = process.env.SKYGEAR_APIKEY;
const username = process.env.SKYGEAR_USERNAME;
const password = process.env.SKYGEAR_PASSWORD;
const interval = 60 * 1000;
console.log('Initializing health reporting');
console.log('Skygear endpoint: ', endPoint);
console.log('Skygear username: ', username);
const HealthModel = skygear.Record.extend('piHealth');
function sendHealth() {
exec(`vcgencmd measure_temp`, (error, stdout, stderr) => {
const cpuTemp = stdout.replace(/temp=(.*)\n/, function() {
return arguments[1];
});
console.log('Temp ', cpuTemp);
const h = new HealthModel({
cpuTemp: cpuTemp
});
skygear.privateDB.save(h);
});
}
skygear.config({
apiKey: apiKey,
endPoint: endPoint
}).then(() => {
console.log('Skygear initialized');
skygear.signupWithUsername(username, password).then(() => {
console.log('Sucessfully signup the devices');
sendHealth();
setInterval(sendHealth, interval);
}, (err) => {
console.log('Trying to login');
skygear.loginWithUsername(username, password).then(() => {
console.log('Sucessfully login');
sendHealth();
setInterval(sendHealth, interval);
}, (err) => {
console.log(err);
});
});
});