-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
267 lines (242 loc) · 8.43 KB
/
app.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
'use strict'
const timers = require('timers');
const setInterval = timers.setInterval;
const request = require('request');
const express = require('express');
const path = require('path');
const SerialPort = require('serialport');
const fs = require('fs');
const WEBHOOK_URL_MEDICAL_USERS = 'https://car-production-app.herokuapp.com/api/cars/getAssociatedQRs';
const WEBHOOK_URL_DELETE_MEDICAL_USER = 'https://car-production-app.herokuapp.com/api/cars/deleteMedicalUser';
const WEBHOOK_URL_UPDATE = 'https://car-production-app.herokuapp.com/api/cars/update';
const WEBHOOK_URL_REQUEST = 'https://car-production-app.herokuapp.com/api/cars/request';
//this is an important flag, if some accident happened, we shall not update accident status with anything that's
//returned from the device since except the GPS location.
let accidentHappened = 'false';
//This is where the local car data exists, it's directly updated from the serial port.
const carInfoLocal = {
carId: 'cqowieucop98034ckle65689cwer2132we', //CHANGE THIS BETWEEN BOTH RASPBERRIES
longitude: 0,
latitude: 0,
accidentStatus: 0,
speed: 70
};
let issueConnectingToDevice = 0;
//this is where other nearby cars exist, it's updated from the server
const otherNearbyCars = [
];
//this is the notifications array, it contains all notifications
const notificationsArray = [
];
//SECTION RESPONSIBLE FOR MAP//
//loading app
const app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//loading static files
app.use(express.static(path.join(__dirname, 'maps/dist')));
//internal endpointss
app.get('/getNearbyCars', (req, res) => res.json({
nearbyCars: otherNearbyCars,
thisCar: {
lng: carInfoLocal.longitude,
lat: carInfoLocal.latitude,
accidentStatus: carInfoLocal.accidentStatus
}
}));
app.get('/getMedicalUsers', (req, res) => {
request.post(WEBHOOK_URL_MEDICAL_USERS, {
json: {
carId: carInfoLocal.carId,
}
}, function(err, response, body) {
console.log(err)
console.log(body)
return res.json(body);
});
});
app.get('/deleteMedicalUser', (req, res) => {
try {
if (req.query !== null && req.query.userToBeDeleted !== null) {
request.post(WEBHOOK_URL_DELETE_MEDICAL_USER, {
timeout: 1000,
json: {
carId: carInfoLocal.carId,
userToBeDeleted: req.query.userToBeDeleted
}
}, function(err, response, body) {
console.log(err)
console.log('body: ' + body) //should display OK
});
}
} catch (error) {
return res.sendStatus(503);
}
});
app.post('/myOwnCarInfo', (req, res) => {
res.json(carInfoLocal);
})
app.get('/getNofifications', (req, res) => {
res.json({
notifications: notificationsArray
});
});
/**
* This function should just load from the file. As the location is updated
* It should update the location into the global variable
*/
function locationFromFileUpdate() {
fs.readFile('lastKnownLocation', function(err, data) {
if (!err) {
let stringifiedData = (data.toString());
let splitData = stringifiedData.split(',');
try {
carInfoLocal.latitude = parseFloat(splitData[0]);
carInfoLocal.longitude = parseFloat(splitData[1]);
} catch (error) {
console.log("Some error happened reading the file.");
}
} else {
console.log(err);
}
});
}
locationFromFileUpdate();
//open internal communication port
app.listen(43421, () => console.log('Example app listening on port 43421!'));
//////////////////////////////////SERIAL COMMUNICATIONS SETCION//////////////////////////
//parsers
const Delimiter = SerialPort.parsers.Delimiter;
const port = new SerialPort('/dev/ttyACM0', {
baudRate: 115200,
autoOpen: false
});
const parser = port.pipe(new Delimiter({
delimiter: Buffer.from('\r\n'),
encoding: 'ascii'
}))
portOpening();
//this should be also sent with the car data if there is any error in the connection
let MasterConnectionErrorCounter = 0;
let previousStateIsError = true;
function portOpening() {
port.open((error) => {
if (error) {
setTimeout(() => {
portOpening();
MasterConnectionErrorCounter++;
previousStateIsError = true;
}, 1000);
} else {
previousStateIsError = false;
}
})
}
port.on('open', () => {
console.log('Port opened.');
port.flush();
})
//this is what gets data from the serial port.
parser.on('data', (data) => {
let stringifiedBuffer = data.toString();
console.log("buff buff" + stringifiedBuffer);
//here's the data
let locationData = stringifiedBuffer.split(',');
let longitude = locationData[0];
let latitude = locationData[1];
let speed = locationData[2];
let accidentFlag = 0;
console.log(locationData.length);
console.log(latitude.length);
if (locationData.hasOwnProperty('length') && locationData.length === 4 && speed === '7' && longitude.hasOwnProperty('length') && longitude.length === 11 &&
latitude.hasOwnProperty('length') && latitude.length === 11) {
//use of accidentFlag
//no accident already happened - flag is false - should send 0
// if (accidentHappened == 'false') {
// if (locationData[3] && locationData[3] != 0) {
// accidentHappened = locationData[3];
// console.log("ASASDASDASDASDASD" + accidentHappened);
// accidentFlag = locationData[3].split('\r')[0];
// } else {
// accidentFlag = 0;
// }
// } else {
// accidentFlag = accidentHappened;
// }
console.log('a: lng' + longitude + ' b: lat' + latitude + ' c: ' + speed + ' d: ' + accidentFlag);
carInfoLocal.longitude = parseFloat(parseFloat(longitude).toFixed(6));
carInfoLocal.latitude = parseFloat(parseFloat(latitude).toFixed(6));
carInfoLocal.speed = parseInt(speed);
//carInfoLocal.accidentStatus = accidentFlag;
console.log(locationData[3])
if (locationData[3]) {
try {
console.log("GOT HERE")
carInfoLocal.accidentStatus = parseInt(locationData[3].split('\r')[0]);
console.log(carInfoLocal.accidentStatus);
} catch (ex) {
console.log(ex)
}
}
//port.update();
} else {
port.flush();
}
});
port.on('error', (err) => {
console.log(err);
});
let i = 0;
setInterval(() => {
updateData();
}, 1200);
setInterval(() => {
//getUpdates();
}, 2000);
///////////////////END TEST////////////////////////////////
function updateData() {
if (carInfoLocal.latitude !== 0 && !Number.isNaN(carInfoLocal.latitude) && typeof carInfoLocal.latitude === 'number' &&
carInfoLocal.longitude !== 0 && !Number.isNaN(carInfoLocal.longitude) && typeof carInfoLocal.longitude === 'number' &&
previousStateIsError === false) {
updateTheFileWithTheLocation();
console.log(carInfoLocal)
request.post(WEBHOOK_URL_UPDATE, {
timeout: 1000,
json: {
carId: carInfoLocal.carId,
speed: carInfoLocal.speed,
accidentStatus: carInfoLocal.accidentStatus,
location: {
longitude: carInfoLocal.longitude,
latitude: carInfoLocal.latitude
}
}
}, function(err, response, body) {
console.log(err)
console.log('body: ' + body) //should display OK
});
}
}
function updateTheFileWithTheLocation() {
if (carInfoLocal.longitude !== 0 && carInfoLocal.latitude !== 0) {
fs.writeFile("lastKnownLocation", carInfoLocal.latitude + "," + carInfoLocal.longitude, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
}
function getUpdates() {
request.post(WEBHOOK_URL_REQUEST, {
timeout: 1000,
json: {
carId: carInfoLocal.carId
}
}, function(err, response, body) {
console.log(body);
});
}