This repository was archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.bak
More file actions
64 lines (54 loc) · 1.44 KB
/
server.bak
File metadata and controls
64 lines (54 loc) · 1.44 KB
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
var express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require('fs');
const port = 8080;
var altitude = "nope";
var temperature = "nope";
var pressure = "nope";
app.use(express.static('web'))
const getData = function (type, callback) {
fs.readFile('data/readable.json', function (err, data) {
if (err) throw err;
var parsedData = JSON.parse(data);
var returnValue;
for (var i = 0; i < parsedData.length; i++) {
if (parsedData[i][type]) {
returnValue = parsedData[i][type];
}
}
callback(null, returnValue);
});
}
setInterval(function () {
getData("altitude", function (err, alt) {
if (err) throw err;
altitude = alt;
})
}, 200);
setInterval(function () {
getData("temperature", function (err, temp) {
if (err) throw err;
temperature = temp;
})
}, 200);
setInterval(function () {
getData("pressure", function (err, pres) {
if (err) throw err;
pressure = pres;
})
}, 200);
io.on('connection', function (socket) {
//if (err) throw err;
console.log("connected")
setInterval(function () {
io.emit('temperature', Math.round(temperature));
io.emit('altitude', Math.round(altitude));
io.emit('pressure', Math.round(pressure));
}, 200);
});
//Dit gedeelte moet ALTIJD onderaan staan
http.listen(port, function () {
console.log("Server running at 0.0.0.0:8080")
});