-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (58 loc) · 1.7 KB
/
server.js
File metadata and controls
70 lines (58 loc) · 1.7 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
// server.js
const express = require("express");
const fs = require("fs");
const app = express();
const PORT = process.env.PORT || 3000;
let dataStored = [];
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post("/submit", (req, res) => {
const data = req.body;
const date = new Date().toISOString();
const zurichTime = new Date().toLocaleString('sv-SE', {timeZone:'Europe/Zurich'});
const entry = {
timestamp: date,
zuDate: zurichTime,
...data,
};
if (entry.device) {
dataStored.push(entry);
console.log("Data received:", entry);
console.log("dataStored size:", dataStored.length);
res.sendStatus(200);
return;
}
console.log("Unknown data type : ", entry.device);
res.sendStatus(200);
});
app.get("/data", (req, res) => {
const { device, index } = req.query; // for wget "http://localhost:3000/data?device=AIR780&index=2" -O response.json
if (index !== undefined) {
const i = parseInt(index, 10);
if (isNaN(i) || i < 0 || i >= dataStored.length) {
return res.status(404).json({ error: "Index out of bounds" });
}
const item = dataStored[i];
return res.json(item);
}
if (device) {
const filtered = dataStored.filter(
(entry) => entry.device === device
);
dataStored = dataStored.filter(
(entry) => entry.device !== device
); // remove matched ones
console.log(
`Returned and removed ${filtered.length} entries for device: ${device}`
);
return res.json(filtered);
}
res.json(dataStored);
});
// Add this before app.listen
app.get('/', (req, res) => {
res.send('📡 PicoGPRSClient is running. Use POST /submit or GET /data');
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server V0.2 running on http://0.0.0.0:${PORT}`);
});