-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (105 loc) · 4.34 KB
/
server.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
import express from 'express';
import https from 'https';
import fs from 'fs';
import fetch from 'node-fetch';
import uuid from 'uuid-random';
import haversine from 'haversine';
import bodyParser from 'body-parser'
// import clipboardy from 'clipboardy'
import * as ml from './serverComponents/ml.js';
import * as db from './serverComponents/db.js';
import * as acc from './serverComponents/acceleration.js';
import * as speed from './serverComponents/over-speeding.js';
const app = express();
const port = 8080;
const dataset = [];
const httpsOptions = {
key: fs.readFileSync('key/key.pem'),
cert: fs.readFileSync('key/cert.pem')
};
// USER ACCOUNTS HANDLERS
app.get('/users', async (req, res) => {
const details = await db.getDetails('User');
res.json(details);
});
app.post('/users', express.json(), async (req, res) => {
const addUser = await db.addUserAccount(req.body);
res.json(addUser);
});
app.get('/data', async (req, res) => {
// console.log(await db.testing());
res.json(await db.getDetails('Maneuver'));
});
app.get('/:userId/:date', async (req, res) => {
console.log(req.params.userId, req.params.date);
const getJourney = await db.getDayJourney(req.params.userId, req.params.date);
console.log(getJourney);
res.json(getJourney);
})
// GPS LOCATION ACCESSES 60 DATASETS IN ONE SECONDS WITH DATA IN VARIANCE
// MEANING THE GPS COORDINATES ARE ACCURATE
let str = ''
app.post('/data', bodyParser({limit: '1gb'}), async (req, res) => {
console.log('Size: ', req.get("content-length")/1000000);
console.log(new Date());
console.log(req.body.gps);
for (let i = 0; i < req.body.gps.length; i++) {
let coord = req.body.gps[i]
str += `${i+1}, ${coord.lat}, ${coord.lon} \n`
}
console.log(str);
// TURNS GPS INTO CSV FILE TO VIEW IN GOOGLE MAPS (RAODS VISITED)
fs.writeFile("my.csv", str, function(err, result) {
if(err) console.log('error', err);
});
// clipboardy.writeSync(JSON.stringify(req.body.gps));
// console.log(req.body.normal);
// ARG 1: ALL COLLECTED CORRDS // ARG 2: EACH LAT/LONG IS 14 DIGITS LONG SO WE ONLY CONSIDERING FIRST 6
// const extracted = await acc.extractGPSlocations(req.body.gps, 3);
// ARG 1: ALL COORDS // ARG 2: EXTRACTED COORDS // ARG 3: TIME GAP TO MEASURE ACC. // ARG 4: MIN SPEED TO TEST ACC. ON
// const showDistance = await acc.showDistance(req.body.gps, extracted, 4, 30)
// console.log('Acc: ', showDistance);
// console.log('--------------------------------------------------');
// // ------------------------------------------------------------------------
// const testModel = await ml.init(req.body.normal);
// const testBraking = await ml.init(req.body.braking);
// console.log('All: ', extractBrakeANDOtherEvents(testModel, 'NON AGGRESSIVE'));
// console.log('Braking: ', extractBrakeANDOtherEvents(testBraking, 'BRAKING'));
// console.log('--------------------------------------------------');
// // dataset.push(testModel)
// // ------------------------------------------------------------------------
// ARG 1: ALL GPS COORDS // ARG 2: TIME GAP TO MEASURE SPEED IN SECS
// const overSpeed = await speed.calculateSpeed(speed.realSpeedData(), 4);
fs.writeFile("gpsdata.json", JSON.stringify(req.body.gps), function(err, result) {
if(err) console.log('error', err);
});
console.log('--------------------------------------------------');
// return res.json()
// const journeyEvents = {
// userId: req.body.userId,
// eventId: undefined,
// maneuver: extractBrakeANDOtherEvents(testModel, 'NON AGGRESSIVE'),
// braking: extractBrakeANDOtherEvents(testBraking, 'BRAKING'),
// acceleration: showDistance,
// overspeeding: overSpeed,
// eventDate: req.body.date
// };
// console.log(journeyEvents);
// const postEventData = await db.storeJourney(journeyEvents);
res.setHeader('Content-Type', 'application/json');
res.send({ data: 'DONE' })
});
function extractBrakeANDOtherEvents(events, type) {
if(type === 'BRAKING') {
return events.filter(e => e.eventType === type)
} else {
return events.filter(e => e.eventType !== 'BRAKING')
}
}
const server = https.createServer(httpsOptions, app)
app.use(express.static('files'));
app.use(bodyParser.json({limit: '1gb'}));
// server.addListener('upgrade', (req, res, head) => console.log('UPGRADE:', req.url));
server.listen(port, () => {
console.log('server running at ' + port)
});