This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (72 loc) · 2.16 KB
/
server.js
File metadata and controls
87 lines (72 loc) · 2.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require("express");
const { v4: uuidv4 } = require("uuid");
const app = express();
const port = 3000;
/* const dataStructure = {
ID: "9d451283-9e83-48eb-9c29-e7411f81eaf5",
timestamp: new Date(),
latitude: 51.494449497662984,
longitude: -0.17357715798513457,
};
*/
function randLatitude() {
//latitude coordinate must be between -90 and 90
const max = 90;
const min = -90;
return (Math.random() * (max - min + 1) + min).toFixed(4);
}
function randLongitude() {
// longitude coordinate must be between -180 and 180
const max = 180;
const min = -180;
return (Math.random() * (max - min + 1) + min).toFixed(4);
}
function randData() {
const dataObject = {
ID: uuidv4(),
timestamp: new Date().toISOString(),
latitude: randLatitude(),
longitude: randLongitude(),
};
console.log(dataObject);
}
// ----------------------------------------------------
/* Express App */
const initialEventsPerMinute = 30;
let sleepTimeMilliseconds = (60 / initialEventsPerMinute) * 1000;
let timer = null;
app.use(express.json());
// Bereitstellen der statischen Dateien (HTML, CSS, JS), die für die Darstellung der Webseite genutzt werden
app.use("/", express.static("www"));
app.use("/lib", express.static("node_modules"));
// Bereitstellen der Router
app.get("/api/start", (req, res) => {
timer = setInterval(randData, sleepTimeMilliseconds);
res.send("Starting to generate data");
});
app.get("/api/stop", (req, res) => {
clearInterval(timer);
timer = null;
res.send("Stopping to generate data");
});
app.get("/api/config", (req, res) => {
const configObj = {
running: timer ? true : false,
sleepTimeMilliseconds: sleepTimeMilliseconds,
};
res.send(configObj);
});
app.post("/api/config", function (req, res) {
console.log("Request: " + "Method=" + req.method + ", URL=" + req.originalUrl);
if (timer === null) {
const eventsPerMinute = req.body.eventsPerMinute;
sleepTimeMilliseconds = (60 / eventsPerMinute) * 1000;
res.sendStatus(200);
} else if (timer != null) {
res.sendStatus(401);
}
});
// Starten der App
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});