-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavvia.js
646 lines (547 loc) · 26.4 KB
/
avvia.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* In questo script gestiremo tutte le chiamate post e get dell'applicazione (sia WEB che Android).
* Con le chiamate "get" mettiamo sulla schermata tutti i dati in formato JSON. Esempio: ci servono le rastrelliere in un altro
* script. Cosa facciamo? La fetch dell'url corrispondente a dove abbiamo la get.
* Con le chiamate POST, invece, passiamo i dati da un altro script al database. Vogliamo aggiungere rastrelliere? Facciamo una chiamata POST
* all'URL che ci permette di aggiungerle, ad esempio "/rastrelliere". Successivamente ad ogni richiesta POST si fa una redirect all'URL della home. */
var apice = "\'";
var errore_completo;
var query_insert = "";
/* Preparazione di nodejs per far funzionare il tutto. */
const {Client} = require('pg')
const express = require("express");
var bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const open = require('open');
const cluster = require("cluster");
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'BikeFleetMonitoring',
password: '6666',
port: 5432,
})
const app = express();
client.connect();
app.use(express.static(__dirname));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true, parameterLimit: 1000000}))
app.use(fileUpload({
createParentPath: true
}));
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
/* Stiamo in ascolto su "localhost:3000". */
app.listen(3000, () => {
console.log("Application started and Listening on port 3000");
});
/*** Richieste GET ***/
/* Ad URL "/home" rendirizziamo "home.html". Questo a sua volta chiamerà lo script "home.js" che farà tutte le operazioni
* sulla mappa. */
app.get("/home", (req, res) => {
res.render("home.html");
});
/* Ad URL "/rastrelliere" avremo il JSON di tutte le rastrelliere. */
app.get("/rastrelliere", async (req, res) => {
var response = await getRastrelliere().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le rastrelliere.' + '\n' + errore_completo);
} else {
res.json(response.rows);
}
});
/* Ad URL "/geofence" avremo il JSON di tutte le geofence. */
app.get("/geofence", async (req, res) => {
var response = await getGeofences().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le geofence.' + '\n' + errore_completo);
} else {
res.json(response.rows);
}
});
/* Ad URL "/geofenceVietate" avremo il JSON di tutte le geofence vietate. */
app.get("/geofenceVietate", async (req, res) => {
var response = await getGeofencesVietate().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le geofence vietate.' + '\n' + errore_completo);
} else {
res.json(response.rows);
}
});
/* Ad URL "/users" avremo il JSON di tutti gli user. */
app.get("/users", async (req, res) => {
var response = await getUsers().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare gli utenti.' + '\n' + errore_completo);
} else {
res.json(response.rows);
console.log('Utenti caricati con successo' + '\n');
}
});
/* Ad URL "/storico" avremo il JSON di tutte lo storico dei percorsi. */
app.get("/storico", async (req, res) => {
var response = await getStorico().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare lo storico dei tragitti.' + '\n' + errore_completo);
} else {
res.json(response.rows);
}
});
/* Ad URL "/bici_real_time" avremo il JSON di tutte le bici noleggiate in real time. */
app.get("/bici_real_time", async (req, res) => {
var response = await getBiciRealTime().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le bici noleggiate in real time.' + '\n' + errore_completo);
} else {
console.log('Caricate le bici noleggiate in real time.');
res.json(response.rows);
}
});
/* Ad URL "/lista_bici" avremo il JSON di tutte le bici riferite ad una determinata rastrelliera. */
app.get("/lista_bici", async (req, res) => {
var response = await getListaBici(req.query.id).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare la lista delle bici.' + '\n' + errore_completo);
} else {
res.json(response.rows);
console.log('Lista delle bici caricata con successo' + '\n');
}
});
/* Ad URL "/vis_pren" avremo il JSON del codice di prenotazione, la bicicletta e se il noleggio è iniziato o meno, dato lo username utente. */
app.get("/vis_pren", async (req, res) => {
var response = await getPrenotazione(apice + req.query.cod_u + apice).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare il codice della prenotazione.' + '\n' + errore_completo);
} else {
res.json(response.rows);
console.log('Codice della prenotazione caricato con successo' + '\n');
}
});
/* Ad URL "/checkDistance" avremo l'id della rastrelliera a meno di tot metri dall'utente. */
app.get("/checkDistance", async (req, res) => {
var response = await getRastrellieraVicino(req.query.lng, req.query.lat).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nel ritrovamento della rastrelliera vicino all\'utente!' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
});
/* Ad URL "/rastrelliera_corrispondente" avremo l'id della rastrelliera in cui è contenuta una determinata bici. Serve per il noleggio
* quando va a sbloccare la bici per verificare se l'utente può noleggiare la bici sia per quanto riguarda la vicinanza alla
* rastrelliera sia per quanto riguarda la data di inizio del noleggio. */
app.get("/rastrelliera_corrispondente", async (req, res) => {
var response = await getRastrellieraFromBici(req.query.bici, req.query.codP).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nella ricerca della rastrelliera!.' + '\n' + errore_completo);
} else {
console.log('Ricerca della rastrelliera andata a buon fine!.');
res.json(response.rows)
}
});
/* Ad URL "/intersezione_geofence" avremo la geofence intersecata dall'utente. */
app.get("/intersezione_geofence", async (req, res) => {
var response = await getIntersezioneGeofence(req.query.lng, req.query.lat).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nella ricerca di una geofence intersecata!.' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
});
/*Ad URL "/bici_fuori_range" avremo bici che risiedono al di fuori di una certa distanza dalla rastrelliera di partenza. */
app.get("/bici_fuori_range", async (req, res) => {
var response = await getBiciFuoriRange(req.query.distanza).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nella ricerca delle bici fuori range!.' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
})
/* Restituisce il numero di rastrelliere presenti nel db */
app.get("/n_rastrelliere", async (req, res) => {
var response = await getNRastrelliere().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nel conteggio delle rastrelliere!.' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
})
/* Dato l'id di una rastrelliera restituisce la longitudine e la latitudine di essa. */
app.get("/pos_rastr", async (req, res) => {
var response = await getPosRastr(req.query.id).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nel trovare la poszione della rastrelliera!' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
})
/* Restituisce i dati del noleggio dato il codice. */
app.get("/get_dati_noleggio", async (req, res) => {
var response = await getDatiNoleggio(req.query.codice_noleggio).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nel trovare i dati del noleggio! ' + '\n' + errore_completo);
} else {
res.json(response.rows)
}
})
/* Serve per inserire il delay della notifica. Inserisce anche il nome dell'utente. */
app.get("/insert_delay", async (req, res) => {
var response = await insertDelay(req.query.delay, req.query.user).catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nell\'inserimento del delay!' + '\n' + errore_completo);
} else {
console.log('Inserimento del ritardo avvenuto con successo!')
}
})
/* Restituisce le statistiche dei delay delle notifiche. */
app.get("/stats_delay", async (req, res) => {
var response = await statsDelay().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore nel calcolo delle statistiche!' + '\n' + errore_completo);
} else {
res.json(response.rows);
}
})
/*** Richieste POST ***/
/* Facendo una richiesta "POST" ad URL "/prenota" si effettua il noleggio di una bici con i dati passati al body. */
app.post("/prenota", (req, res) => {
client.query('INSERT INTO noleggio(codice, bicicletta, utente, data_inizio, iniziato) VALUES(' + apice + req.body.cod + apice + ',' + req.body.bici + ',' + apice + req.body.utente + apice + ',' + apice + req.body.di + apice + ',' + false + ')', (err, result) => {
if (err) {
console.log('Errore durante la prenotazione!' + err);
} else {
console.log('Prenotazione effettuata!');
}
});
res.end();
});
/* Facendo una richiesta "POST" ad URL "/rastrelliere_marker" si aggiunge tramite disegno una rastrelliera. */
app.post("/rastrelliere_marker", (req, res) => {
query_insert = 'INSERT INTO rastrelliere(name, geom) VALUES (' + apice + req.body.name + apice + ', ST_GeomFromText(' + apice + 'POINT(' + req.body.long + ' ' + req.body.lat + ')' + apice + ')) ON CONFLICT ON CONSTRAINT rastrelliere_name_key DO NOTHING;'
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere la rastrelliera!');
} else {
/* Riprendo le rastrelliere e se non ci sono stati errori rivado alla home. */
var response = await getRastrelliere().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le rastrelliere.' + '\n' + errore_completo);
} else {
console.log('Rastrelliera aggiunta!');
res.redirect('/home');
}
}
});
});
/* Facendo una richiesta "POST" ad URL "/rastrelliere_file" si aggiunge tramite file un insieme di rastrelliere. */
app.post("/rastrelliere_file", (req, res) => {
let rastrelliere;
try {
rastrelliere = JSON.parse(req.files.file.data.toString());
} catch (e) {
console.log("Non sono riuscito ad aggiungere rastrelliere, file non valido " + e);
res.redirect('/home');
return;
}
query_insert = "";
for (let ras of rastrelliere.features) {
var name = ras.properties.Nome;
var long = ras.geometry.coordinates[0];
var lat = ras.geometry.coordinates[1];
if (name !== undefined && long !== undefined && lat !== undefined) {
name = name.replace("'", " ")
query_insert += 'INSERT INTO rastrelliere(name, geom) VALUES (' + apice + name + apice + ', ST_GeomFromText(' + apice + 'POINT(' + long + ' ' + lat + ')' + apice + ')) ON CONFLICT ON CONSTRAINT rastrelliere_name_key DO NOTHING;';
} else {
console.log("Non stai aggiungendo rastrelliere in modo corretto! File non valido");
res.redirect('/home');
return;
}
}
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere la rastrelliera!' + err);
} else {
/* Riprendo le rastrelliere e se non ci sono stati errori rivado alla home. */
var response = await getRastrelliere().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le rastrelliere.' + '\n' + errore_completo);
} else {
console.log('Rastrelliere aggiunte!');
res.redirect('/home');
}
}
});
});
/* Facendo una richiesta "POST" ad URL "/geofence" si aggiunge tramite disegno una geofence. All'interno si distingue se
* è una geofence vietata oppure no. */
app.post("/geofence", (req, res) => {
let vietata;
vietata = req.body.geoVietata !== undefined;
query_insert = 'INSERT INTO geofence(name, geom, message, vietata) VALUES (' + apice + req.body.name + apice +
', ST_GeomFromGeoJSON(' + apice + req.body.geom + apice + ') , ' + apice + req.body.message + apice + ',' + vietata + ');'
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere la geofence!');
} else {
/* Riprendo le geofence e se non ci sono stati errori rivado alla home. */
var response = await getAllGeofences().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le geofence.' + '\n' + errore_completo);
} else {
console.log('Geofence aggiunta!');
res.redirect('/home');
}
}
});
});
/* Facendo una richiesta "POST" ad URL "/geofence_file" si aggiunge tramite file un insieme di geofence. */
app.post("/geofence_file", (req, res) => {
let geofence;
try {
geofence = JSON.parse(req.files.file.data.toString());
} catch (e) {
console.log("Non sono riuscito ad aggiungere geofence, file non valido " + e);
res.redirect('/home');
return;
}
query_insert = "";
if (geofence !== undefined) {
for (let ras of geofence.features) {
var name = ras.properties.name;
var vietata = ras.properties.vietata;
if (name !== undefined && vietata !== undefined) {
name = name.replace("'", " ")
query_insert += 'INSERT INTO geofence(name, geom, vietata) VALUES (' + apice + name + apice + ', ST_GeomFromGeoJSON(' + apice + JSON.stringify(ras.geometry) + apice + '), ' + vietata + ') ON CONFLICT ON CONSTRAINT ' +
'geofence_name_key DO NOTHING;';
} else {
console.log("Non stai aggiungendo geofence in modo corretto! File non valido");
res.redirect('/home');
return;
}
}
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere la geofence!' + err);
res.redirect('/home');
} else {
/* Riprendo le geofence e se non ci sono stati errori rivado alla home. */
var response = await getAllGeofences().catch((err) => errore_completo = err);
if (!response) {
console.log('Errore, non sono riuscito a caricare le geofence.' + '\n' + errore_completo);
res.redirect('/home');
} else {
console.log('Geofence aggiunte!');
res.redirect('/home');
}
}
});
}
});
/* Facendo una richiesta "POST" ad URL "/registrazione" si aggiunge un utente al database. */
app.post("/registrazione", (req, res) => {
query_insert = 'INSERT INTO utente(username, password) VALUES (' + apice + req.body.username + apice + ',' + apice + req.body.password + apice + ');'
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere l\'utente!' + err);
} else {
console.log('Utente aggiunto!');
}
});
res.end();
});
/* Facendo una richiesta "POST" ad URL "/addPosizione" si aggiorna la posizione della bicicletta ogni tot. secondi di tempo. */
app.post("/addPosizione", async (req, res) => {
query_insert = 'UPDATE bicicletta SET posizione = ST_GeomFromText(' + apice + 'POINT(' + req.body.long + ' ' + req.body.lat + ')' + apice + ') WHERE id = ' + req.body.id + ';'
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore non sono riuscito ad aggiungere la posizione!' + err);
} else {
console.log('Posizione aggiunta!');
res.json(result.rows);
}
});
});
/* Facendo una richiesta "POST" ad URL "/avvia_noleggio" si aggiorna lo stato del noleggio di una bicicletta in particolare. */
app.post("/avvia_noleggio", (req, res) => {
query_insert = 'UPDATE noleggio SET iniziato = ' + true + ' where codice = ' + apice + req.body.codNoleggio + apice + ';'
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore nel settare il noleggio come iniziato!' + err);
} else {
console.log('Noleggio avviato');
}
});
res.end();
});
/* Facendo una richiesta "POST" ad URL "/termina_noleggio" se tutte le query scritte non danno problemi si fa terminare il noleggio
* di una determinata bici. É particolare perchè viene effettuata una transazione. */
app.post("/termina_noleggio", async (req, res) => {
try {
await client.query('BEGIN')
const query1 = 'INSERT INTO storico VALUES (' + apice + req.body.codNoleggio + apice + ', ST_GeomFromGeoJSON(' + apice + '{"type":"LineString","coordinates":' + req.body.geom + '}' + apice + '))';
await client.query(query1);
const query2 = 'DELETE FROM lista_bici_rastrelliera WHERE bicicletta =' + req.body.bici + ';'
await client.query(query2);
const query3 = 'INSERT INTO lista_bici_rastrelliera VALUES (' + req.body.rastrelliera + ', ' + req.body.bici + ')'
await client.query(query3)
const query4 = 'UPDATE bicicletta set posizione = rastrelliere.geom from rastrelliere where rastrelliere.id =' + req.body.rastrelliera + ' and bicicletta.id = ' + req.body.bici;
await client.query(query4)
const query5 = 'UPDATE noleggio set data_fine = ' + apice + req.body.df + apice + ' where noleggio.codice = ' + apice + req.body.codNoleggio + apice + ';'
await client.query(query5)
await client.query('COMMIT')
} catch (e) {
await client.query('ROLLBACK')
console.log('Terminazione errata del noleggio !' + e);
}
console.log('Terminazione noleggio avvenuta con successo')
res.end();
});
/* Facendo una richiesta "POST" ad URL "/cancella_prenotazione" si rimuove la prenotazione dal db. */
app.post("/cancella_prenotazione", async (req, res) => {
query_insert = 'DELETE FROM noleggio WHERE codice = ' + apice + req.body.cod_prenotazione + apice + ';';
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore nella cancellazione della prenotazione.' + '\n' + errore_completo);
} else {
console.log('Cancellazione prenotazione effettuata');
}
});
});
/* Facendo una richiesta "POST" ad URL "/clustering" si effettua il clustering e si restituiscono le posizioni delle biciclette clusterizzate. */
app.post("/clustering", async (req, res) => {
var dataset = req.body;
var k_clusters = parseInt(dataset.numClusters);
dataset = dataset.lat.map((e, i) => [parseFloat(e), parseFloat(dataset.long[i])]);
var clustering = require('density-clustering');
var kmeans = new clustering.KMEANS();
var clusters = kmeans.run(dataset, k_clusters);
var i = 0;
var newDataset = [];
for (arrs of clusters) {
for (arr of arrs) {
newDataset.push({
id: arr + 1,
long: dataset[arr][1],
lat: dataset[arr][0],
cluster: i
});
}
i += 1;
}
const JSONClusters = JSON.parse(JSON.stringify(Object.assign({}, newDataset)));
res.json(JSONClusters);
});
/* Facendo una richiesta "POST" ad URL "/delete_inizializzazione" si effettua la cancellazione delle bici dal database, dei noleggi, della tabella lista_bici_rastrelliera e degli utenti. */
app.post("/delete_inizializzazione", async (req, res) => {
query_insert = 'TRUNCATE TABLE bicicletta RESTART IDENTITY CASCADE; DELETE FROM lista_bici_rastrelliera; DELETE FROM noleggio; DELETE FROM storico; DELETE FROM utente;';
client.query(query_insert, async (err, result) => {
if (err) {
console.log('Errore nella cancellazione dell\'inizializzazione del database! ' + '\n' + err + '\n' + err.detail);
} else {
console.log('Cancellazione in inizializzazione del database andata a buon fine!');
}
});
res.end();
});
// Inserimento della bici nel database e poi viene assegnata a una rastrelliera
app.post("/inizializza_database", async (req, res) => {
try {
await client.query('BEGIN')
const query1 = 'INSERT INTO bicicletta(posizione) VALUES (ST_GeomFromText(' + apice + 'POINT(' + req.body.long + ' ' + req.body.lat + ')' + apice + '));'
await client.query(query1);
const query2 = 'INSERT INTO lista_bici_rastrelliera(rastrelliera, bicicletta) VALUES (' + req.body.id + ', ' + req.body.id_bici + ');'
await client.query(query2);
await client.query('COMMIT')
} catch (e) {
await client.query('ROLLBACK')
console.log('Terminazione errata del reset del database! ' + e);
}
res.json({});
});
/* Metodi per prendere dal DB ciò che ci serve. Ritorna poi alla get che l'ha chiamata, in modo tale da controllare se
* ci sono stati errori altrimenti stampa nella pagina le righe della query trasformate in JSON. */
function getRastrelliere() {
return client.query('SELECT id, name, ST_X(geom) AS long, ST_Y(geom) AS lat FROM public.rastrelliere;');
}
function getGeofences() {
return client.query('SELECT name, ST_AsGeoJSON(geom) AS geometry FROM geofence WHERE vietata = false');
}
function getGeofencesVietate() {
return client.query('SELECT name, ST_AsGeoJSON(geom) AS geometry FROM geofence where vietata = true');
}
function getAllGeofences() {
return client.query('SELECT name, ST_AsGeoJSON(geom) AS geometry FROM geofence');
}
function getUsers() {
return client.query('SELECT * FROM utente;');
}
function getListaBici(id) {
return client.query('SELECT bicicletta AS id FROM public.lista_bici_rastrelliera WHERE rastrelliera = ' + id + '' +
' and bicicletta not in (select noleggio.bicicletta\n' +
' FROM bicicletta, noleggio\n' +
' WHERE noleggio.bicicletta = bicicletta.id\n' +
' AND codice NOT IN (SELECT noleggio FROM storico)\n' +
' )\n' +
' order by id;');
}
function getPrenotazione(cod_u) {
return client.query('SELECT codice, bicicletta, iniziato FROM noleggio WHERE utente =' + cod_u + ' AND codice NOT IN (SELECT noleggio FROM storico)');
}
function getRastrellieraVicino(longitudine, latitudine) {
let query1 = 'SELECT a1.id FROM rastrelliere AS A1 WHERE ST_Distance(A1.geom::geography, ST_GeomFromText(' +
apice + 'POINT(' + longitudine + ' ' + latitudine + ')' + apice + ')::geography) <= 75 order by ' +
'ST_Distance(A1.geom::geography, ST_GeomFromText(' +
apice + 'POINT(' + longitudine + ' ' + latitudine + ')' + apice + ')::geography)';
return client.query(query1);
}
function getRastrellieraFromBici(bici, codp) {
let query = 'SELECT rastrelliera, data_inizio FROM lista_bici_rastrelliera, noleggio ' +
'WHERE lista_bici_rastrelliera.bicicletta = ' + bici + ' ' +
'AND noleggio.codice =' + apice + codp + apice + ' ' +
'and noleggio.bicicletta = lista_bici_rastrelliera.bicicletta ;'
return client.query(query);
}
function getIntersezioneGeofence(longitudine, latitudine) {
return client.query('Select G1.name,G1.message,G1.vietata AS vietato from geofence as G1 where ST_Contains(G1.geom, ST_GeomFromText(' +
apice + 'POINT(' + longitudine + ' ' + latitudine + ')' + apice + ')::geography::geometry) ORDER BY vietata DESC,name ;');
}
function getStorico() {
return client.query('SELECT ST_AsGeoJSON(traiettoria) AS geometry, noleggio AS codice FROM storico');
}
function getBiciRealTime() {
return client.query('SELECT ST_X(posizione) AS long, ST_Y(posizione) AS lat, bicicletta.id FROM bicicletta, noleggio WHERE noleggio.iniziato = true AND noleggio.bicicletta = bicicletta.id AND codice NOT IN (SELECT noleggio FROM storico)');
}
function getBiciFuoriRange(distanza) {
return client.query('SELECT bicicletta.id\n' +
' FROM bicicletta, noleggio, rastrelliere, lista_bici_rastrelliera AS lbr\n' +
' WHERE noleggio.iniziato = true\n' +
' AND lbr.bicicletta = bicicletta.id\n' +
' AND lbr.rastrelliera = rastrelliere.id\n' +
' AND noleggio.bicicletta = bicicletta.id\n' +
' AND codice NOT IN (SELECT noleggio FROM storico)\n' +
' AND ST_DistanceSphere(rastrelliere.geom, bicicletta.posizione) > ' + distanza + ';'
)
}
function getNRastrelliere() {
return client.query('select count(*) as n_rastrelliere from rastrelliere;')
}
function getPosRastr(id) {
return client.query('SELECT ST_X(geom) AS long, ST_Y(geom) AS lat FROM rastrelliere WHERE rastrelliere.id = ' + id);
}
function getDatiNoleggio(codiceNoleggio) {
return client.query('SELECT bicicletta, utente, data_inizio, data_fine FROM noleggio WHERE codice =' + apice + codiceNoleggio + apice + ' ORDER BY data_inizio');
}
function insertDelay(delay, user) {
return client.query('Insert into delay (delay, utente) values (' + delay + ', ' + apice + user + apice + ')');
}
function statsDelay() {
return client.query('SELECT *\n' +
'FROM(\n' +
'\tSELECT count(*) AS numero_delay, round(avg(delay),2) AS media, utente\n' +
'\tFROM delay\n' +
'\tGROUP BY utente\n' +
'\tUNION\n' +
'\tSELECT count(*) AS numero_delay, round(avg(delay),2) AS media, \'Tutti\' AS utente\n' +
'\tFROM delay\n' +
') AS t1\n' +
'ORDER BY utente');
}
// All'avvio apriamo la home con il browser di default.
open("http://localhost:3000/home");