-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
64 lines (54 loc) · 1.74 KB
/
server.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
// requires
var http = require('http'),
exec = require('child_process').exec,
stat = require('node-static'),
sensors = require('./sensors').sensors,
faye = require('faye');
// static file server to serve the /public folder
var fileServer = new stat.Server('./public');
// pubsub server to push out live data updates
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
// the main server
var server = http.createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
});
});
bayeux.attach(server);
server.listen(8080);
console.log('Server running on port 8080');
// get faye client to pubish messages to browser
var client = bayeux.getClient();
// loop forever over the sensors array, waiting between each iteration to avoid
// clogging the i2c bus and getting junk data
(function() {
var i = 0;
var get_data = function() {
exec(sensors[i].cmd, function(error, stdout, stderr) {
client.publish('/data', {
name: sensors[i].name,
value: sensors[i].parser(stdout),
units: sensors[i].units,
time: Date.now()
});
i = (i + 1) % sensors.length;
setTimeout(get_data, 20);
});
};
get_data();
})();
// old server code
// http.createServer(function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// var callbacks = 0;
// sensors.forEach(function(s) {
// callbacks += 1;
// exec(s.cmd, function (error, stdout, stderr) {
// res.write( s.label + s.parser(stdout) + s.units + '\n' );
// callbacks -= 1;
// if (callbacks === 0) {
// res.end();
// }
// });
// });
// }).listen(1337, '');