-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectionDriver.js
115 lines (103 loc) · 4.15 KB
/
collectionDriver.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var ObjectID = require('mongodb').ObjectID;
var _ = require('lodash');
var moment = require('moment');
CollectionDriver = function(db) {
this.db = db;
}
CollectionDriver.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else callback(null, the_collection);
});
};
CollectionDriver.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else {
the_collection.find().toArray(function(error, results) {
if( error ) callback(error);
else callback(null, results);
});
}
});
};
CollectionDriver.prototype.findSensor = function(collectionName, sensorName, callback) {
this.getCollection(collectionName, function(error, the_collection){
if (error) callback(error);
else {
the_collection.find({'name':sensorName}).toArray(function(error, results){
callback(null, results);
});
}
});
};
CollectionDriver.prototype.get = function(collectionName, id, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(id)) callback({error: "invalid id"});
else {
the_collection.findOne({'_id':ObjectID(id)}, function(error, docs){
returnDocs(error, docs, callback);
});
}
}
});
};
CollectionDriver.prototype.distinct = function(collectionName, field, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else {
the_collection.distinct(field, function(error, docs){
returnDocs(error, docs, callback);
});
}
});
};
CollectionDriver.prototype.findDates = function(collectionName, startDate, endDate, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if(error) callback(error);
else {
var startDateObjectId = objectIdWithTimestamp(startDate);
var endDateObjectId = objectIdWithTimestamp(endDate);
var minDate, maxDate;
the_collection.find({'_id': { '$gt': startDateObjectId, '$lt': endDateObjectId}, 'motion' : 'End'}, {'name':1, 'time':1})
.toArray(function(error, results){
_(results).forEach(function(result){
result.endTimestamp = ObjectID(result._id).getTimestamp();
result.startTimestamp = moment(result.endTimestamp).subtract(result.time, 'seconds');
delete result._id;
if (minDate === undefined) {
minDate = result.startTimestamp;
} else {
if (moment(result.startTimestamp).isBefore(minDate)) {
minDate = result.startTimestamp;
}
}
if (maxDate === undefined) {
maxDate = result.endTimestamp;
} else {
if (moment(result.endTimestamp).isAfter(maxDate)) {
maxDate = result.endTimestamp;
}
}
});
callback(null, {'values': results, 'maxTimestamp': maxDate, 'minTimestamp': minDate});
});
}
});
};
var returnDocs = function(error, doc, callback) {
if (error) callback(error);
else callback(null, doc);
}
var objectIdWithTimestamp = function(timestamp) {
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
var hexSeconds = Math.floor(timestamp/1000).toString(16);
var constructedObjectId = ObjectID(hexSeconds + "0000000000000000");
return constructedObjectId;
}
exports.CollectionDriver = CollectionDriver;