-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterSensors.js
More file actions
255 lines (218 loc) · 7.77 KB
/
FilterSensors.js
File metadata and controls
255 lines (218 loc) · 7.77 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
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
246
247
248
249
250
251
252
253
254
/**
* Sat April 5, 2014
*
* This .js node script runs on my raspberry pi and filters the collected data and serves it via rest
*
* There are three mongo collections:
* 1. sensors : this is scraped from http://udfcd.onerain.com. It is a record, with LL coords, of each
* 'device' (not each station). Each of these records from onerain actually includes the last value
* of the sensor, which isn't used here, because we're collecting the values directly via radio.
*
* 2. sites : this is also scraped form http://udfcd.onerain.com. It is a record, also with LL coords, of
* each 'station'. Each station can have multiple 'sensors'. We're getting the LL and name from this record.
*
* 3. receivedStations : this is a raw collection of data received via radio. Each record includes stationID
* (which is device_alias in the udfc data), and value, and timestamp
*
* 4. foundStations : this is one record for each station we've ever received, the first time we say it, and the last time
* it reported
*
* This .js does two things:
* 1. It runs every 5 minutes and filters un processed receivedStations into foundStations updating or inserting new records
* as needed
*
* 2. It serves the foundStations data via JSON REST using express
*
*/
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
var _ = require('underscore');
var Q = require('q');
var express = require('express');
var app = express();
var geo = require('geolib');
var home = {
latitude: 39.966464,
longitude: -105.159251
};
var milesPerMeter = 0.000621371;
// make promise methods out of basic mongo methods
var syncFind = function (db, colName, query, sort, limit) {
var def = Q.defer();
db.collection(colName).find(query, {limit: limit}).sort(sort).toArray(function (err, results) {
if (err) {
def.reject(err);
} else {
def.resolve(results);
}
});
return def.promise;
};
var syncFindOne = function (db, colName, query) {
var def = Q.defer();
db.collection(colName).findOne(query, function (err, result) {
if (err)
def.reject(err);
else
def.resolve(result);
});
return def.promise;
};
var syncUpdate = function (db, colName, selector, record) {
var def = Q.defer();
db.collection(colName).update(selector, record, {w: 1}, function (err, rec) {
if (err) {
def.reject(err);
} else {
def.resolve(rec);
}
});
};
var syncInsert = function (db, colName, record) {
var def = Q.defer();
db.collection(colName).insert(record, {w: 1}, function (err, rec) {
if (err) {
def.reject(err);
} else {
def.resolve(rec);
}
});
};
var syncConnect = function (url) {
var def = Q.defer();
MongoClient.connect('mongodb://localhost:27017/urbanDrainage', function (err, db) {
if (err) {
def.reject(err);
} else {
def.resolve(db);
}
});
return def.promise;
};
var computeDistanceMiles = function (p1, p2) {
return geo.getDistance(p1, p2) * milesPerMeter;
};
// basic REST HTTP
app.get('/hello.txt', function (req, res) {
res.send('Hello World');
});
app.get('/foundStations', function (req, res) {
res.setHeader('Content-Type', 'application/json');
var dbCon;
syncConnect('mongodb://localhost:27017/urbanDrainage')
.then(function (db) {
dbCon = db;
return syncFind(db, 'foundStations', {}, {lastReport: -1});
})
.then(function (foundStations) {
res.end(JSON.stringify(foundStations))
})
.finally(function(){
dbCon.close();
})
.fail(function (err) {
res.end(err.toString());
})
});
app.configure(function () {
app.use(express.static(__dirname));
});
var server = app.listen(3000, function () {
console.log('Listening on port %d', server.address().port);
});
var processOneStation = function (db) {
var def = Q.defer();
var unprocessedStation = null;
syncFindOne(db, 'receivedStations', {processed: {"$exists": false}})
.then(function (us) {
if (us) {
console.log("looking for device_alias of " + us.stationID.toString());
unprocessedStation = us;
return [us.stationID.toString(), syncFindOne(db, 'sensors', {"properties.device_alias": us.stationID.toString()})];
} else {
throw {
reason: "no more records",
code: 0
}
}
})
.spread(function (devAlias, stationDetails) {
if (stationDetails == null) {
throw {
reason: "Didn't find station for device alias " + devAlias,
code: 1
}
}
return [stationDetails, syncFindOne(db, 'foundStations', {stationID: stationDetails.properties.site_alias})];
})
.spread(function (stationDetails, foundStation) {
var stationPoint = {
latitude: stationDetails.geometry.coordinates[1],
longitude: stationDetails.geometry.coordinates[0]
};
var distanceFromHome = computeDistanceMiles(stationPoint, home);
if (foundStation == null) {
console.log("inserting new station for " + stationDetails.properties.name);
var newFoundStation = {
stationID: stationDetails.properties.site_alias,
firstReport: new Date(),
name: stationDetails.properties.name,
sensorType: stationDetails.properties.type,
lon: stationDetails.geometry.coordinates[0],
lat: stationDetails.geometry.coordinates[1],
lastReport: new Date(),
distanceFromHome: distanceFromHome
};
// insert a new foundStation
return syncInsert(db, 'foundStations', newFoundStation);
}
else {
console.log("updating existing station " + foundStation.name);
foundStation.lastReport = new Date();
// todo this is temporary
foundStation.distanceFromHome = distanceFromHome;
return syncUpdate(db, 'foundStations', {"_id": foundStation._id}, foundStation);
}
})
.fail(function (err) {
console.log("in main fail");
console.dir(err);
})
.finally(function () {
console.log("done");
if (unprocessedStation) {
// mark record as processed
console.log("marking as processed");
unprocessedStation.processed = true;
syncUpdate(db, 'receivedStations', {"_id": unprocessedStation._id}, unprocessedStation);
def.resolve();
} else {
def.reject();
}
})
.done();
return def.promise;
};
var processStations = function (db, prom) {
var def = Q.defer();
prom
.then(function () {
processStations(db, processOneStation(db));
})
.fail(function (err) {
console.log("we're done\n" + err);
def.resolve(); // because this is how we know we're done
})
.finally(function () {
})
.done();
return def.promise;
};
setInterval(function () {
console.log("\n\n\nSTARTING\n\n\n");
MongoClient.connect('mongodb://localhost:27017/urbanDrainage', function (err, db) {
processStations(db, processOneStation(db))
.then(function(){
db.close();
})
});
}, 5 * 60 * 1000);