-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.js
63 lines (52 loc) · 1.63 KB
/
monitor.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
const http = require('http');
const os = require('os');
const path = require('path');
const Tessel = require('tessel-io');
const J5 = require('johnny-five');
const board = J5.Board({
io: new Tessel()
});
const Express = require('express');
const SocketIO = require('socket.io');
const app = new Express();
const server = new http.Server(app);
const io = new SocketIO(server);
const assertPath = path.join(__dirname, 'app');
const vendorPath = path.join(__dirname, 'node_modules');
app.use(Express.static(assertPath));
app.use('/vendor', Express.static(vendorPath));
board.on('ready', () => {
const clients = new Set();
const monitor = new J5.Multi({
controller: 'BME280',
elevation: 2
});
let update = Date.now() - 5000;
monitor.on('change', () => {
const now = Date.now();
if(now - update >= 5000) {
update = now;
clients.forEach(client => {
client.emit('report', {
thermometer: monitor.thermometer.celsius,
barometer: monitor.barometer.pressure,
hygrometer: monitor.hygrometer.relativeHumidity,
altimeter: monitor.altimeter.meters
});
});
}
});
io.on('connection', socket => {
if(clients.size < 5) {
clients.add(socket);
socket.on('disconnect', () => clients.delete(socket));
}
});
const port = 80;
server.listen(port, () => {
console.log(`Server started, go to http://${os.hostname()}`);
process.on('SIGINT', () => {
server.close();
});
});
});