-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
155 lines (127 loc) · 4.03 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
const express = require("express");
const app = express();
const PORT = process.env.PORT || 8080;
const path = require('path');
var randomCoordinates = require('random-coordinates');
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(express.urlencoded());
const { body,validationResult } = require('express-validator');
const { sanitizeBody } = require('express-validator');
var dataCounter = 0;
var data = [];
var archive = [];
var newData = false;
var restCounter = 0;
coords = [{status: "active", lat: 40.7127837,lng: -74.0059413, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 34.0522342, lng: -118.2436849, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 41.8781136, lng: -87.6297982, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 29.7604267, lng: -95.3698028, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 39.9525839, lng: -75.1652215, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 33.4483771, lng: -112.0740373, temp: 24, Sat: 1351, speed: 215},
{status: "active", lat: 29.4241219, lng: -98.4936282, temp: 24, Sat: 1351, speed: 215},
];
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
// set static folder
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
/*
app.get('/login', function(req, res) {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.post('/login', function(req, res) {
console.log(req.body);
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
*/
// app data basic function
app.post('/post', function(req, res) {
if (dataCounter > 0) {
var info = data.pop();
archive.push(info);
dataCounter--;
console.log("SENDING");
console.log(info);
res.send(JSON.stringify(info));
}
else {
console.log("PASSIVE");
res.send(JSON.stringify({status: "passive"}));
}
});
app.post('/test', function(req, res) {
"Test Post"
res.send(coords[Math.floor(Math.random() * 10)]);
});
// for the reset buttons
app.post('/reset', function(req, res) {
console.log("Status Reset")
dataCounter = 0;
newData = false;
data = [];
archive = [];
res.send(200);
});
app.post('/csv', function(req, res) {
console.log("CSV Request Recieved");
res.send(JSON.stringify(data));
});
app.post('/restore', function(req, res) {
if (restCounter == archive.length) {
restCounter = 0;
}
res.send(archive[restCounter]);
restCounter++;
});
app.post('/data', function(req, res) {
try {
console.log(req.body);
var hex2 = hex2a(req.body.data);
var str = hex2.split(",");
var latitude = parseFloat(str[0]);
var longitude = parseFloat(str[1]);
var alt = parseFloat(str[2]);
var temperature = parseFloat(str[3]);
var speedIndicator = parseFloat(str[4]);
var satNum = req.body.IMEI;
var obj = {status: "active", lat: latitude,lng: longitude, temp: temperature, Sat: alt, speed: speedIndicator};
//{status: "active", code: str};
// {status: "active", lat: latitude,lng: longitude, temp: temperature, Sat: satNum, speed: alt};
console.log(obj.status);
console.log(obj.lat);
console.log(obj.lng);
console.log(obj.temp);
console.log(obj.Sat);
console.log(obj.speed);
data.push({status: "active", lat: latitude,lng: longitude, temp: temperature, Sat: alt, speed: speedIndicator});
console.log((typeof({status: "active", lat: latitude,lng: longitude, temp: temperature, Sat: satNum, speed: alt})));
dataCounter++;
newData = true;
res.send(200);
} catch {
res.send(200);
}
});
// establish access
app.post('/access', function(req, res) {
res.redirect('/');
/*
var user = req.body.loginUsername;
var pass = req.body.loginPassword;
if (user == "[email protected]" && pass == "bakken") {
res.redirect('/');
}
else {
res.redirect('/');
}
*/
});
app.listen(PORT, ()=> console.log('Server Started'));