-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews.js
94 lines (73 loc) · 1.91 KB
/
views.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var _ = require('lodash');
var moment = require('moment');
var models = require('./models');
// Gonna move to require.js later garage48
var clientHomepage = function (req, res) {
res.sendfile(__dirname + '/clientHomepage.html');
};
var clientRealtimeHtml = function (req, res) {
res.sendfile(__dirname + '/clientRealTime.html');
};
var clientHistoryHtml = function (req, res) {
res.sendfile(__dirname + '/clientHistory.html');
};
var endpointBase = function (modelClass, req, res) {
var fromTs, toTs;
if (req.query.from_ts) {
fromTs = moment.unix(req.query.from_ts).toDate();
} else {
fromTs = moment().subtract('minutes', 5).toDate();
}
if (req.query.to_ts) {
toTs = moment.unix(req.query.to_ts).toDate();
} else {
toTs = moment().toDate();
}
modelClass
.find()
.where('ts').gt(fromTs).lt(toTs)
.select('val ts')
.lean()
.sort('ts')
.exec(function (err, data) {
if (err) {
res.send(400);
return;
}
res.send(data);
});
};
var heartbeatData = _.partial(endpointBase, models.Heartbeat);
var temperatureData = _.partial(endpointBase, models.Temperature);
var inputHeartbeat = function (req) {
var ts = moment(req.data.ts);
var value = req.data.value;
console.log('Incoming heartbeat', value);
var beat = new models.Heartbeat({
ts: ts.toDate(),
val: value
});
beat.save();
req.io.broadcast('heartbeat', { ts: ts.valueOf(), value: value });
};
var inputBPM = function (req) {
var ts = moment(req.data.ts);
console.log(req.data);
var value = req.data.value;
console.log('Incoming bpm', value);
var bpm = new models.BPM({
ts: ts.toDate(),
val: value
});
bpm.save();
req.io.broadcast('bpm', { ts: ts.valueOf(), value: value });
};
module.exports = {
clientHomepage: clientHomepage,
clientRealtimeHtml: clientRealtimeHtml,
clientHistoryHtml: clientHistoryHtml,
heartbeatData: heartbeatData,
temperatureData: temperatureData,
inputHeartbeat: inputHeartbeat,
inputBPM: inputBPM
};