-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
389 lines (368 loc) · 17.8 KB
/
web.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// This file is part of ghini.web
// http://github.com/Ghini/ghini.web
//
// ghini.web is free software: you can redistribute ghini.web and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// ghini.web is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
// License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with ghini.web. If not, see <http://www.gnu.org/licenses/>.
//
// this is the remote server
var http = require('http');
http.globalAgent.maxSockets = 10;
var config = require('./config');
var express = require("express");
var app = express();
var port = Number(process.env.PORT || config.port); // servicing on port
var dburl = process.env.DATABASE_URL || config.database_url;
console.log(dburl);
var mongodb = require('mongodb');
var dbclient = mongodb.MongoClient;
const mongoose = require('mongoose');
String.prototype.formatU = function() {
var str = this.toString();
if (!arguments.length)
return str;
var args = typeof arguments[0];
args = (("string" == args || "number" == args) ? arguments : arguments[0]);
for (var arg in args)
str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
return str;
};
var sizethree = function (i) {
var result = '000' + (i || 0);
return (result).substr(result.length-3);
}
app.set('views', __dirname + '/views');
app.set('view engine', "pug");
app.engine('pug', require('pug').__express);
app.use(express.static(__dirname + '/public'));
var favicon = require('serve-favicon');
app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.get("/", function(req, res){
res.render("map");
console.log(dburl);
});
app.get("/panels/:garden_id/:language?", (req, res) => {
var language = req.params.language || "en";
if (!language.match("^[a-z][a-z]$")) {
language = "en";
}
var connection = mongoose.createConnection(dburl);
connection.on('connected', () => {
var schema = new mongoose.Schema({});
var Panel = connection.model('infopanels', schema);
var result = [];
Panel.
aggregate([{$match: {garden_id: parseInt(req.params.garden_id)}}]).
cursor().
exec().
on('data', (doc) => {
doc.url = ("http://www.ghini.me/raw/g_{0}_{1}_{2}.mp3").formatU(
sizethree(doc.garden_id), language, sizethree(doc.id_within_garden));
result.push(doc);
}).
on('error', (err) => {
console.log("err:", err);
}).
on('end', () => {
res.render('panel', {doc: result});
});
});
});
app.get("/gardens", (req, res) => {
var connection = mongoose.createConnection(dburl);
connection.on('connected', () => {
var schema = new mongoose.Schema({});
var Garden = connection.model('garden', schema);
var result = [];
Garden.
aggregate([ {$sort: {name: 1}},
{$lookup: {from: "infopanels", foreignField: "garden", localField: "name", as: "infopanels"}},
{$project: {name: 1, lat: 1, lon: 1, contact: 1, id: 1,
title: "$name",
infopanels: {$size: "$infopanels"}}} ]).
cursor().
exec().
on('data', (doc) => {result.push(doc)}).
on('error', (err) => {
console.log("err:", err);
}).
on('end', () => {
res.render('garden', {doc: result});
});
});
});
// make the application listen to the port
// we pass the ExpressJS server to Socket.io. In effect, our real time
// communication will still happen on the same port.
var io = require('socket.io').listen(app.listen(port, function() {
console.log("Listening on " + port);
}));
io.sockets.on('connection', function (socket) {
// Upon a successful connection, we send the list of gardens, with the
// plants count per garden. gardens are objects as any other object, they
// have associated the colour red and the icon home, and the zoom at which
// they appear is 2. then we issue the command 'map-set-view' to the world.
// As instructed by the final user (perusing garden icons or menu item),
// the clients issue the 'select-garden' command. Upon receiving this
// command from a client, the server issues the command 'map-set-view'
// (garden position and extent), followed by as many 'add-object'
// commands as needed to send the lists of objects relative to the
// selected garden. These include plants, photos, infopanels.
dbclient.connect(dburl, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
// Get the collection of all gardens, each with the amount of plants;
// We store our find criteria in a cursor.
// Having a cursor does not mean we performed any database access, yet.
var cursor = db.collection('gardens').aggregate(
{$sort: {name: 1}},
{$lookup: {from: "plants", foreignField: "garden", localField: "name", as: "plants"}},
{$lookup: {from: "photos", foreignField: "garden", localField: "name", as: "photos"}},
{$lookup: {from: "infopanels", foreignField: "garden", localField: "name", as: "infopanels"}},
{$project: {layer_name: {$literal: "gardens"},
layer_zoom: {$literal: 1},
name: 1, lat: 1, lon: 1, contact: 1,
title: "$name",
plants: {$size: "$plants"},
photos: {$size: "$photos"},
infopanels: {$size: "$infopanels"},
draggable: {$literal: false},
color: {$literal: "red"},
icon: {$literal: "home"}}},
{});
// Lets iterate on the result.
// this will access the database, so we act in a callback.
cursor.each(function (err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc);
} else {
socket.emit('add-object', doc);
}
});
cursor = db.collection('plants').aggregate(
{$sort: {code: 1}},
{$group: {_id: {species: "$species", garden: "$garden"},
count: {$sum: 1}, plants: {$push: {_id: "$_id", code: "$code"}}}},
{$group: {_id: {species: "$_id.species"},
gardens: {$push: {name: "$_id.garden", plant_count: "$count", plants: "$plants"}}}},
{$lookup: {from: "taxa", localField: "_id.species", foreignField: "name", as: "taxon"}},
{$unwind: {path: "$taxon"}},
{$project: {_id: "$taxon._id",
phonetic: "$taxon.phonetic",
species_name: "$taxon.name",
family_name: "$taxon.family",
gardens: 1, taxon: 1,
layer_name: {$literal: "__taxa"}}},
{$sort: {family_name: 1, species_name: 1}},
{});
// Lets iterate on the result.
// this will access the database, so we act in a callback.
cursor.each(function (err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc);
} else {
socket.emit('add-object', doc);
}
});
}});
socket.on('select-garden', function (args) {
// require that client wipes all data relative to the current garden
socket.emit('map-remove-objects', 'plants');
socket.emit('map-remove-objects', 'photos');
socket.emit('map-remove-objects', 'infopanels');
var defaults = {
zoom: 2,
lat: 32.0,
lon: 8.0
};
console.log("args: ", args);
if ('map' in args) {
Object.assign(args, args.map);
} else {
Object.assign(args, defaults);
}
if (!('garden' in args)) {
socket.emit('map-set-view', args);
return;
}
dbclient.connect(dburl, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
// first of all, tell the client to set the view on the
// garden; the garden document contains lat, lon, and zoom.
var cursor = db.collection('gardens').findOne({aliases: args['garden']}, function(err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc, "garden not found", args);
console.log('map-set-view', args);
socket.emit('map-set-view', args);
} else {
if ('map' in args) {
Object.assign(doc, args.map);
}
console.log('map-set-view', doc);
socket.emit('map-set-view', doc);
// Get the plants relative to this garden.
// We store our find criteria in a cursor.
// Having a cursor does not mean we performed any database access, yet.
cursor = db.collection('plants').aggregate(
{$match: {garden: doc['name']}},
{$lookup: {from: "taxa", localField: "species", foreignField: "name", as: "taxon"}},
{$unwind: {path: "$taxon"}},
{$project: {layer_name: {$literal: "plants"},
layer_zoom: "$zoom",
lat: 1, lon: 1, species: 1, taxon: 1, code: 1,
title: "$code",
species_name: "$taxon.name",
vernacular: "$taxon.vernacular",
phonetic: "$taxon.phonetic",
family: "$taxon.family",
draggable: {$literal: false},
color: {$literal: "green"},
icon: {$literal: "tree-evergreen"}}},
{}
);
// Lets iterate on the result.
// this will access the database, so we act in a callback.
cursor.each(function (err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc);
} else {
switch(doc.family){
case 'Arecaceae':
doc.icon = 'palm-tree';
break;
case 'Cannaceae':
case 'Costaceae':
case 'Heliconiaceae':
case 'Lowiaceae':
case 'Marantaceae':
case 'Musaceae':
case 'Strelitziaceae':
case 'Zingiberaceae':
doc.icon = 'banana-tree';
break;
case 'Araucariaceae':
case 'Cupressaceae':
case 'Cycadaceae':
case 'Ephedraceae':
case 'Ginkgoaceae':
case 'Gnetaceae':
case 'Pinaceae':
case 'Podocarpaceae':
case 'Sciadopityaceae':
case 'Taxaceae':
case 'Welwitschiaceae':
case 'Zamiaceae ':
doc.icon = 'tree-conifer';
break;
case 'Anemiaceae':
case 'Apleniaceae':
case 'Aspleniaceae':
case 'Athyriaceae':
case 'Blechnaceae':
case 'Cibotiaceae':
case 'Culcitaceae':
case 'Cyatheaceae':
case 'Cystodiaceae':
case 'Cystopteridaceae':
case 'Davalliaceae':
case 'Dennstaedtiaceae':
case 'Dicksoniaceae':
case 'Diplaziopsidaceae':
case 'Dipteridaceae':
case 'Dryopteridacae':
case 'Dryopteridaceae':
case 'Equisetaceae':
case 'Gleicheniaceae':
case 'Hymenophyllaceae':
case 'Hypodematiaceae':
case 'Isoëtaceae':
case 'Lindsaeaceae':
case 'Lomariopsidaceae':
case 'Lonchitidaceae':
case 'Loxsomataceae':
case 'Lycopodiaceae':
case 'Lygodiaceae':
case 'Marattiaceae':
case 'Marsileaceae':
case 'Matoniaceae':
case 'Metaxyaceae':
case 'Nephrolepidaceae':
case 'Oleandraceae':
case 'Onocleaceae':
case 'Ophioglossaceae':
case 'Osmundaceae':
case 'Plagiogyriaceae':
case 'Polypodiaceae':
case 'Psilotaceae':
case 'Pteridaceae':
case 'Rhachidosoraceae':
case 'Saccolomataceae':
case 'Salviniaceae':
case 'Schizaeaceae':
case 'Selaginellaceae':
case 'Tectariaceae':
case 'Thelypteridaceae':
case 'Thyrsopteridaceae':
case 'Woodsiaceae':
// doc.icon = 'fern'; // there is no such icon yet; issue #33
break;
case 'Asteraceae':
doc.icon = 'compositae';
break;
}
socket.emit('add-object', doc);
}
});
// same for the photos
cursor = db.collection('photos').aggregate(
{$match: {garden: doc['name']}},
{$project: {layer_name: {$literal: "photos"},
layer_zoom: "$zoom",
lat: 1, lon: 1, title: 1, name: 1,
draggable: {$literal: false},
color: {$literal: "cadetblue"},
icon: {$literal: "camera"}}},
{}
);
cursor.each(function (err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc);
} else {
socket.emit('add-object', doc);
}
});
// same for the infopanels
cursor = db.collection('infopanels').aggregate(
{$match: {garden: doc['name']}},
{$project: {layer_name: {$literal: "infopanels"},
layer_zoom: "$zoom",
lat: 1, lon: 1, title: 1, text: 1,
draggable: {$literal: false},
color: {$literal: "purple"},
icon: {$literal: "info-sign"}}},
{}
);
cursor.each(function (err, doc) {
if (err || !doc) {
console.log("err:", err, "; doc:", doc);
} else {
socket.emit('add-object', doc);
}
});
}
});
}});
});
});