-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (35 loc) · 1.54 KB
/
server.js
File metadata and controls
40 lines (35 loc) · 1.54 KB
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
var config = module.require('./config'),
http = module.require('http'),
express = require('express'),
io = module.require('socket.io'),
util = module.require('util'),
sqlite3 = require('sqlite3').verbose(),
thermometers = module.require('thermometers');
var app = express(),
httpServer = http.createServer(app),
sockServer = io.listen(httpServer, {log: false}),
db = new sqlite3.Database(config.database);
// Setup thermolab and start logging
var thermoLab = new thermometers.ThermometerLab(config.thermometerLab.devicesPath);
var thermoLogger = new thermometers.ThermometerLogger(thermoLab, db);
// Setup web interface
app.use(express.static(__dirname + '/web'));
sockServer.on('connection', function(client) {
var onNewThermometer = function(t){
['tConnect', 'tData', 'tFail', 'tDisconnect'].forEach(function(evt){
t.on(evt, function(data) { client.emit(evt, data); });
});
};
var devs = thermoLab.getDevices();
for (var d in devs) {
onNewThermometer(devs[d]); // Relay all currently connected thermometers.
thermoLogger.getSensorLog(devs[d].deviceId, 60*60*1000, function(rows){ // Relay historical data for last hour
client.emit('tData', rows);
});
}
thermoLab.on('connect', onNewThermometer); // Relay all future connected thermometers.
client.once('disconnect', function(){ // Remove event listener when client disconnects.
thermoLab.removeListener('connect', onNewThermometer);
});
});
httpServer.listen(config.webPort);