-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
48 lines (42 loc) · 1.06 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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const {
exec
} = require('child_process');
period = parseInt(process.env.PERIOD) || 500;
server.listen(8080);
let sendTemp = function(socket, data) {
//data.color = '#FF0000'
socket.emit('temperature', data);
}
let getCpuTemp = function(socket) {
'use strict';
exec('cat /sys/class/thermal/thermal_zone*/temp', (err, stdout) => {
if (err) throw err;
let data = {
t: parseFloat(stdout) / 1000
};
sendTemp(socket, data);
});
};
let getRandomTemp = function(socket) {
'use strict'
let data = {
t: (20 + Math.floor(Math.random() * 30))
};
sendTemp(socket, data);
};
io.on('connection', function(socket) {
'use strict';
console.log('a user connected');
let dataLoop = setInterval(function() {
//getCpuTemp(socket);
getRandomTemp(socket);
}, period);
socket.on('disconnect', function() {
console.log('a user disconnected');
clearInterval(dataLoop);
});
});