-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp-mongo.js
245 lines (217 loc) · 6.86 KB
/
tp-mongo.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var mongoose = require('mongoose');
/** @description
*
* @type {Model}
*/
var devices = mongoose.model('devices', {
dev_id: String,
longitude: Number,
latitude: Number,
version: String,
location: String,
date: Date,
alive: Boolean
});
/** @description
*
* @type {Model}
*/
var sensorData = mongoose.model('sensorData', {
dev_id: String,
green_detect_cnt: Number,
red_detect_cnt: Number,
green_bi_detect_cnt: Number,
rf_signal_cnt: Number,
date: Date
});
/** @description
*
* @type {Model}
*/
var monthlySensorData = mongoose.model('monthSensorData', {
dev_id: String,
year: Number,
month: Number,
green_detect_cnt: Number,
red_detect_cnt: Number,
green_bi_detect_cnt: Number,
rf_signal_cnt: Number
});
/** @description
*
* @type {Model}
*/
var yearlySensorData = mongoose.model('yearSensorData', {
dev_id: String,
year: Number,
green_detect_cnt: Number,
red_detect_cnt: Number,
green_bi_detect_cnt: Number,
rf_signal_cnt: Number
});
/** @description
*
* @type {Model}
*/
var accessLog = mongoose.model('accessLog', {
date: Date,
ip: String
});
/** @description
*
* @param url
*/
exports.setConnect = function(url) {
mongoose.connect(url, { useNewUrlParser: true }, function(err) {
if(err) console.error(err);
else console.log('mongodb connected.');
});
}
/** @description
*
*/
exports.setDisconnect = function() {
mongoose.disconnect(function() {
console.log("mongo db disconnected.");
})
}
/** @description
*
* @param device
*/
exports.addDevice = function(device) {
devices.updateOne({dev_id: device.dev_id}, device, {upsert: true, setDefaultsOnInsert: true}, function(err) {
console.log(device.dev_id + ' updated');
console.log("lat : " + device.latitude + ", lon : " + device.longitude);
});
}
/** @description
*
* @param func
*/
exports.getAllDevices = function(func) {
devices.find(func).sort({"dev_id":1});
}
/** @description
*
* @param func
*/
exports.getAllSensorData = function(func) {
var date = new Date().toISOString().slice(0,10)
sensorData.find({date:{$gte: new Date(date)}}, {}, func).sort({"date":-1});
}
/** @description
*
* @param page
* @param size
* @param func
*/
exports.getSensorData = function(page, size, func) {
var query = {}
query.skip = size * (page - 1);
query.limit = size;
sensorData.find({date:{$gte: new Date(date)}}, {}, query, func).sort({"date":-1});
}
/** @description
*
* @param id
* @param date
* @param func
*/
exports.getDailySensorData = function(id, date, func) {
sensorData.find({dev_id: id, date:{$gte: new Date(date)}}, {"date":true, "green_detect_cnt":true, "red_detect_cnt":true, "green_bi_detect_cnt":true, "rf_signal_cnt":true}, func).sort({"date":1});
}
/** description
*
* @param id
* @param year
* @param month
* @param func
*/
exports.getMonthlySensorData = function(id, year, month, func) {
monthlySensorData.find({dev_id: id, year:year}, {"month":true, "green_detect_cnt":true, "red_detect_cnt":true, "green_bi_detect_cnt":true, "rf_signal_cnt":true}, func).sort({"month":1});
}
/** @description
*
* @param id
* @param year
* @param func
*/
exports.getYearlySensorData = function(id, year, func) {
yearlySensorData.find({dev_id: id}, {"year":true, "green_detect_cnt":true, "red_detect_cnt":true, "green_bi_detect_cnt":true, "rf_signal_cnt":true}, func).sort({"year":1});
}
/** @description
*
* @param data
*/
exports.addSensorData = function(data) {
let s = new sensorData();
s.dev_id = data.dev_id;
s.green_detect_cnt = data.green_detect_cnt;
s.red_detect_cnt = data.red_detect_cnt;
s.green_bi_detect_cnt = data.green_bi_detect_cnt;
s.rf_signal_cnt = data.rf_signal_cnt;
s.date = data.date;
s.save(function (err, result) {
console.log(result.dev_id + ' data added.');
});
let fromDate = new Date(); fromDate.setDate(1);
let toDate = new Date(fromDate); toDate.setMonth(toDate.getMonth()+1);
sensorData.find({dev_id:data.dev_id, "date":{$gte: new Date(fromDate.toISOString().slice(0,10)), $lt: new Date(toDate.toISOString().slice(0,10))}}, {}, function(err, e) {
let green_detect_cnt = 0;
let red_detect_cnt = 0;
let green_bi_detect_cnt = 0;
let rf_signal_cnt = 0;
e.forEach(function(ee) {
green_detect_cnt += ee.green_detect_cnt;
red_detect_cnt += ee.red_detect_cnt;
green_bi_detect_cnt += ee.green_bi_detect_cnt;
rf_signal_cnt += ee.rf_signal_cnt;
});
let month = new monthlySensorData();
month.dev_id = data.dev_id;
month.year = parseInt(fromDate.toISOString().slice(0, 4));
month.month = parseInt(fromDate.toISOString().slice(5, 7));
month.green_detect_cnt = green_detect_cnt/e.length;
month.red_detect_cnt = red_detect_cnt/e.length;
month.green_bi_detect_cnt = green_bi_detect_cnt/e.length;
month.rf_signal_cnt = rf_signal_cnt/e.length;
monthlySensorData.updateOne({dev_id: month.dev_id, year:month.year, month:month.month}, month, {upsert: true, setDefaultsOnInsert: true}, function(err) {
console.log(month.dev_id + ' month data updated');
});
monthlySensorData.find({dev_id:data.dev_id, "year": fromDate.toISOString().slice(0, 4)}, {}, function(err, y) {
let green_detect_cnt = 0;
let red_detect_cnt = 0;
let green_bi_detect_cnt = 0;
let rf_signal_cnt = 0;
y.forEach(function(yy) {
green_detect_cnt += yy.green_detect_cnt;
red_detect_cnt += yy.red_detect_cnt;
green_bi_detect_cnt += yy.green_bi_detect_cnt;
rf_signal_cnt += yy.rf_signal_cnt;
});
let year = new yearlySensorData();
year.dev_id = data.dev_id;
year.year = fromDate.toISOString().slice(0, 4);
year.green_detect_cnt = green_detect_cnt/y.length;
year.red_detect_cnt = red_detect_cnt/y.length;
year.green_bi_detect_cnt = green_bi_detect_cnt/y.length;
year.rf_signal_cnt = rf_signal_cnt/y.length;
yearlySensorData.updateOne({dev_id: year.dev_id, year:year.year}, year, {upsert: true, setDefaultsOnInsert: true}, function(err) {
console.log(month.dev_id + ' year data updated');
});
});
});
}
/** @description
*
* @param ip
*/
exports.addAccessLog = function(ip) {
var client = new accessLog();
client.ip = ip;
client.date = new Date();
client.save(function(err) {
console.log('accessd - ' + ip);
})
}