-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (57 loc) · 1.56 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
const express = require("express");
const cors = require("cors");
const db = require("./db");
const fs = require("fs");
const app = express();
app.use(express.json());
app.use(cors());
const wsServer = require("./websockets");
app.post("/send-command", (req, res) => {
const command = req.body.command;
const type = req.body.type;
let sql = `INSERT INTO commands (command) VALUES ('${JSON.stringify(
command
)}');`;
db.query(sql, (err, result) => {
if (err) throw err;
if (wsServer.getClient()) {
const payload = {
type: type,
command: command,
};
wsServer.getClient().send(JSON.stringify(payload));
console.log("Command sent to WebSocket client");
} else {
console.log("No WebSocket client connected");
}
res.status(200).json({ message: "Command received successfully" });
});
});
app.get("/get-current-position", (req, res) => {
// Query the database
let sql = "SELECT * FROM positions ORDER BY id DESC LIMIT 1";
db.query(sql, (err, result) => {
if (err) throw err;
res.send(result[0]);
});
});
app.get("/get-drone-log", (req, res) => {
fs.readFile("../drone-log/logfile.log", "utf8", (err, data) => {
if (err) {
return res.sendStatus(500);
}
res.send(data);
});
});
app.get("/get-aws-log", (req, res) => {
fs.readFile("../aws-log/logfile.log", "utf8", (err, data) => {
if (err) {
return res.sendStatus(500);
}
res.send(data);
});
});
const port = 8080;
app.listen(port, () => {
console.log(`HTTP server is listening on port ${port}`);
});