forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
49 lines (46 loc) · 1.92 KB
/
events.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
var User = require('../models/user');
var Openhab = require('../models/openhab');
var Event = require('../models/event');
var logger = require('../logger');
exports.eventsget = function(req, res) {
var perPage = 20,
page = req.query.page > 0 ? parseInt(req.query.page) : 0;
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {openhab: openhab};
if (req.query.source)
filter.source = req.query.source;
Event.find(filter)
.limit(perPage)
.skip(perPage * page)
.sort({when: 'desc'})
.lean()
.exec(function(error, events) {
Event.count().exec(function (err, count) {
res.render('events', { events: events, pages: count / perPage, page: page,
title: "Events", user: req.user, openhab: openhab, source: req.query.source,
errormessages:req.flash('error'), infomessages:req.flash('info') });
});
});
} else {
}
});
}
exports.eventsvaluesget = function(req, res) {
req.user.openhab(function(error, openhab) {
if (!error && openhab != null) {
var filter = {openhab: openhab, source: req.query.source};
Event.find(filter).sort({when: 'asc'}).select('when status -_id').exec(function(error, events) {
if (!error) {
var result = [];
var startTime = parseInt(events[0].when.getTime()/1000);
for (var i=0; i<events.length; i++) {
var event = events[i];
result.push([parseInt(event.when.getTime()/1000)-startTime, parseFloat(event.status)]);
}
res.send(JSON.stringify(result));
}
});
}
});
}