-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
173 lines (161 loc) · 4.17 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
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
// require Cloudant for DB
const Cloudant = require("@cloudant/cloudant");
const express = require("express");
const app = express();
const port = 3000;
const geocenter = require("geographic-center");
//Cloudant authentication
const cloudant = new Cloudant({
account: "1e04787e-e7b3-4fe1-9288-8b2d56aa29ad-bluemix",
plugins: {
iamauth: {
iamApiKey: "1OvWJHv1kqWuS6Suk-FSQtFDb1qnPZ_gpD73pPp9jhFK"
}
}
});
app.use(express.json({ extended: false }));
app.use(express.static("public"));
app.set("view engine", "pug");
//Endpoint for nodes to send Data
app.post("/api/sendData", (req, res) => {
const blockchain = req.body;
async function asyncCall() {
let surv = await cloudant.use("survivors");
return await surv.insert(blockchain);
}
asyncCall()
.then(data => {
res.json(data);
})
.catch(err => {
res.json(err);
});
});
function getUnique(arr, comp) {
const unique = arr
.map(e => e[comp])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => arr[e])
.map(e => arr[e]);
return unique;
}
function sortByProp(property) {
return function(x, y) {
return x[property] === y[property] ? 0 : x[property] > y[property] ? 1 : -1;
};
}
//Endpoint for home screen
app.get("/home", (req, res) => {
var db = cloudant.db.use("survivors");
let mainData = new Array();
db.index(function(err, result) {
db.find(
{
selector: {
_id: {
$gt: null
}
}
},
function(err, result) {
if (err) {
throw err;
}
console.log(
"Found %d documents with name survivors",
result.docs.length
);
for (var i = 0; i < result.docs.length; i++) {
mainData.push(...result.docs[i].array);
}
const uniqData = getUnique(mainData, "prehash");
uniqData.sort(sortByProp("battery"));
//console.log(uniqData);
res.render("index", {
title: "DisCom",
message: "Survivor's List:",
data: uniqData
});
}
);
});
});
//END Point for the multimarker map
app.get("/map", (req, res) => {
var db = cloudant.db.use("survivors");
let mainData = new Array();
db.index(function(err, result) {
db.find(
{
selector: {
_id: {
$gt: null
}
}
},
function(err, result) {
if (err) {
throw err;
}
console.log(
"Found %d documents with name survivors",
result.docs.length
);
for (var i = 0; i < result.docs.length; i++) {
mainData.push(...result.docs[i].array);
}
const uniqData = getUnique(mainData, "prehash");
let value = helper(uniqData);
//console.log(value[0]);
//console.log(value[1]);
const data = value[0];
let points = new Array();
for (var j = 0; j < data.length; j++) {
points.push({
lat: parseFloat(data[j][1]),
lon: parseFloat(data[j][2])
});
}
points.shift();
let cent = geocenter(points);
//console.log(cent);
var midsy = JSON.parse(
JSON.stringify(cent).replace('"lon":', '"lng":')
);
//console.log(midsy);
res.render("map", {
mids: midsy,
data: value[0],
count: value[1]
});
}
);
});
});
function helper(uniqData) {
let locData = new Array();
let countOfNoLocation = 0;
for (x in uniqData) {
if (uniqData[x].locations !== "") {
var patt = new RegExp("gps((.*))hA");
var res = patt.exec(uniqData[x].locations);
var newarr = [
uniqData[x].message +
"\n" +
uniqData[x].time +
"\n" +
uniqData[x].battery +
"%",
uniqData[x].latitude,
uniqData[x].longitude
];
locData.push(newarr);
} else {
countOfNoLocation = countOfNoLocation + 1;
}
}
return [locData, countOfNoLocation];
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`));