Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 1.94 KB
/
Copy pathserver.js
File metadata and controls
62 lines (55 loc) · 1.94 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
const { json } = require('body-parser');
const express = require('express');
const fs = require('fs');
// Create an Express application
const app = express();
let droneOnline = false;
let gpsData = [];
let tracking = false;
// Middleware for CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
// Middleware to parse JSON bodies
app.use(express.json());
// Route to receive batch GPS data
app.post('/batch', (req, res) => {
const batch = req.body.data; // Assuming the body contains { data: [ {latitude, longitude, heading}, ... ] }
console.log('Received batch of GPS points:', batch,"Tracking + " + tracking);
if (tracking) {
// Append batch to gpsData array
gpsData.push(...batch);
// Write GPS data to JSON file
console.log("Wrote this batch of GPS points",gpsData)
const jsonData = JSON.stringify(gpsData);
fs.writeFile('gps_data.json', jsonData, (err) => {
if (err) {
console.error('Error writing data to file:', err);
res.status(500).json({ error: 'Failed to write GPS data to file' });
}
});
}
res.status(200)
res.send("working")
});
// Route to get current GPS data
app.get('/gps', (req, res) => {
res.json(gpsData);
});
// Route to indicate drone is online
app.get('/tracking', (req, res) => {
tracking = req.query.tracking === 'true'; // Convert the query parameter to a boolean
console.log("Setting tracking to ",tracking); // Logs true or false
res.send("Setting tracking to" + tracking)
});
app.get('/IsTracking', (req, res) => {
res.send(tracking);
})
// Start the server
const PORT = process.env.PORT || 4300;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});