2023-05-04 11:25:37 +00:00
|
|
|
const express = require('express');
|
|
|
|
const dgram = require('dgram');
|
2023-11-12 12:03:43 +00:00
|
|
|
const net = require("net");
|
2023-05-04 11:25:37 +00:00
|
|
|
|
|
|
|
// constants
|
|
|
|
const PORT = 3000;
|
|
|
|
const HOST = '0.0.0.0';
|
|
|
|
var map = '';
|
2023-11-12 12:03:43 +00:00
|
|
|
var detection = '';
|
2023-11-14 12:13:14 +00:00
|
|
|
var timestamp = '';
|
2023-05-04 11:25:37 +00:00
|
|
|
var data = '';
|
|
|
|
var capture = false;
|
|
|
|
|
|
|
|
// api server
|
|
|
|
const app = express();
|
|
|
|
// header on all requests
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
2023-10-27 05:53:42 +00:00
|
|
|
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
|
|
|
|
res.header('Expires', '-1');
|
|
|
|
res.header('Pragma', 'no-cache');
|
2023-05-04 11:25:37 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.send('Hello World');
|
|
|
|
});
|
|
|
|
app.get('/map', (req, res) => {
|
|
|
|
res.send(map);
|
|
|
|
});
|
2023-11-12 12:03:43 +00:00
|
|
|
app.get('/detection', (req, res) => {
|
|
|
|
res.send(detection);
|
|
|
|
});
|
2023-11-14 12:13:14 +00:00
|
|
|
app.get('/timestamp', (req, res) => {
|
|
|
|
res.send(timestamp);
|
|
|
|
});
|
2023-05-04 11:25:37 +00:00
|
|
|
// read state of capture
|
|
|
|
app.get('/capture', (req, res) => {
|
|
|
|
res.send(capture);
|
|
|
|
});
|
|
|
|
// toggle state of capture
|
|
|
|
app.get('/capture/toggle', (req, res) => {
|
|
|
|
capture = !capture;
|
|
|
|
res.send('{}');
|
|
|
|
});
|
|
|
|
app.listen(PORT, HOST, () => {
|
|
|
|
console.log(`Running on http://${HOST}:${PORT}`);
|
|
|
|
});
|
|
|
|
|
2023-11-12 12:03:43 +00:00
|
|
|
// tcp listener map
|
|
|
|
const server_map = net.createServer((socket)=>{
|
2023-05-04 11:25:37 +00:00
|
|
|
socket.write("Hello From Server!")
|
|
|
|
socket.on("data",(msg)=>{
|
|
|
|
data = data + msg.toString();
|
|
|
|
if (data.slice(-1) === "}")
|
|
|
|
{
|
|
|
|
console.log('EOF');
|
|
|
|
map = data;
|
|
|
|
data = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
socket.on("close",()=>{
|
|
|
|
console.log("Connection closed.");
|
|
|
|
})
|
|
|
|
});
|
2023-11-12 12:03:43 +00:00
|
|
|
server_map.listen(3001);
|
|
|
|
|
|
|
|
// tcp listener detection
|
|
|
|
const server_detection = net.createServer((socket)=>{
|
|
|
|
socket.write("Hello From Server!")
|
|
|
|
socket.on("data",(msg)=>{
|
|
|
|
data = data + msg.toString();
|
|
|
|
if (data.slice(-1) === "}")
|
|
|
|
{
|
|
|
|
console.log('EOF');
|
|
|
|
detection = data;
|
|
|
|
data = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
socket.on("close",()=>{
|
|
|
|
console.log("Connection closed.");
|
|
|
|
})
|
|
|
|
});
|
|
|
|
server_detection.listen(3002);
|
2023-11-14 12:13:14 +00:00
|
|
|
|
|
|
|
// tcp listener timestamp
|
|
|
|
const server_timestamp = net.createServer((socket)=>{
|
|
|
|
socket.write("Hello From Server!")
|
|
|
|
socket.on("data",(msg)=>{
|
|
|
|
data = data + msg.toString();
|
|
|
|
console.log('EOF');
|
|
|
|
timestamp = data;
|
|
|
|
data = '';
|
|
|
|
});
|
|
|
|
socket.on("close",()=>{
|
|
|
|
console.log("Connection closed.");
|
|
|
|
})
|
|
|
|
});
|
|
|
|
server_timestamp.listen(4000);
|