-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (80 loc) · 2.6 KB
/
index.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
const express = require("express");
const app = express();
const axios = require("axios");
const xml2js = require("xml2js").parseStringPromise;
const cors = require("cors");
app.use(cors());
const droneDistance = [];
const contactDetails = [];
app.get("/", async (request, response) => {
const data = await axios.get(
"https://assignments.reaktor.com/birdnest/drones"
);
const parsedData = await xml2js(data.data);
const drones = parsedData.report.capture[0].drone;
const dronesWithinNDZ = drones.map((drone) => {
const yDifference = Math.abs(250000 - drone.positionY);
const xDifference = Math.abs(250000 - drone.positionX);
const getHypotenuse = (sideA, sideB) => {
return Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));
};
const distance = getHypotenuse(xDifference, yDifference);
const distanceInMeters = (distance / 1000).toFixed(2);
if (distance < 100000) {
const singleViolator = {
distanceInMeters: distanceInMeters,
time: parsedData.report.capture[0]["$"].snapshotTimestamp,
};
droneDistance.push(singleViolator);
return drone;
}
});
const NDZviolators = dronesWithinNDZ.filter((drone) => drone !== undefined);
const serialNumbers = NDZviolators.map((e) => {
return e.serialNumber[0];
});
const NDZviolatorsContact = await Promise.all(
serialNumbers.map(async (serialNumber) => {
const response = await axios.get(
`https://assignments.reaktor.com/birdnest/pilots/${serialNumber}`
);
const contact = response.data;
contactDetails.push(contact);
return data;
})
);
const violators = droneDistance.map((drone, index) => {
return {
distance: drone.distanceInMeters,
time: drone.time,
contact: contactDetails[index],
};
});
const violatorsWithin10Minutes = violators.filter((violator) => {
const timeNow = new Date();
const MSperMinute = 60000;
const tenMinsago = new Date(timeNow - 10 * MSperMinute);
const formatViolatorDate = new Date(violator.time);
if (formatViolatorDate > tenMinsago) {
return violator;
}
});
const dronesSortedByDistance = violatorsWithin10Minutes.sort(
(a, b) => a.distance - b.distance
);
const droneData = dronesSortedByDistance.reduce((accumulator, current) => {
if (
!accumulator.find(
(item) => item.contact.pilotId === current.contact.pilotId
)
) {
accumulator.push(current);
}
return accumulator;
}, []);
response.send(droneData);
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});