-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (75 loc) · 2.6 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
/**
* Created by Raj on 3/2/2017.
*/
var http = require('http');
var fs = require('fs');
var ping = require('ping');
var express = require('express'),
path = require('path'),
app = express();
var devicejson = require('./server/devices.json');
//var testlog = require('./server/logfile.txt');
var bodyParser = require('body-parser');
app.use(express.static(path.normalize(__dirname + '/')));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
var engines = require('consolidate');
app.set('public', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
// Express Middleware for serving static files
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.redirect('index.html');
});
app.get('/DevicesConfigured', function (req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify(devicejson));
res.end();
});
app.post('/addDevice', function (req, res) {
var iError = 0;
var strError = "";
console.log("Add device:" + req.body.ipAddress);
ping.promise.probe(req.body.ipAddress, {
timeout: 10,
extra: ["-i 2"],
}).then(function (pRes) {
console.log(pRes);
});
devicejson.push(req.body);
json = JSON.stringify(devicejson); //convert it back to json
fs.writeFile('./server/devices.json', json, 'utf8', function (err) {
if (err) throw err;
}); // write it back
res.send({ "deviceAdded": 1 });
});
app.post('/deleteDevice', function (req, res) {
var index = -1;
var comArr = eval(devicejson);
for (var i = 0; i < comArr.length; i++) {
if (comArr[i].ipAddress === req.body.ipAddress) {
index = i;
break;
}
}
if (index === -1) {
console.log("Something gone wrong");
} else {
devicejson.splice(index, 1);
json = JSON.stringify(devicejson); //convert it back to json
fs.writeFile('./server/devices.json', json, 'utf8', function (err) {
if (err) throw err;
}); // write it back
}
res.send("success");
});
app.post('/measure', function (req, res) {
console.log("Measure post request:" + JSON.stringify(req.body));
fs.writeFile('./server/mesuredata.txt', JSON.stringify(req.body), 'utf8', function (err) {
if (err) throw err;
});
res.send("success");
});
http.createServer(app).listen(8888);
console.log("Server started running..");