mirror of
https://github.com/30hours/blah2.git
synced 2024-11-08 12:25:42 +00:00
104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const dgram = require('dgram');
|
|
const net = require("net");
|
|
|
|
var maxhold = require('./maxhold.js');
|
|
module.exports = Object.assign({}, maxhold)
|
|
|
|
// constants
|
|
const PORT = 3000;
|
|
const HOST = '0.0.0.0';
|
|
var map = '';
|
|
var detection = '';
|
|
var timestamp = '';
|
|
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", "*");
|
|
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
|
|
res.header('Expires', '-1');
|
|
res.header('Pragma', 'no-cache');
|
|
next();
|
|
});
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello World');
|
|
});
|
|
app.get('/map', (req, res) => {
|
|
res.send(map);
|
|
});
|
|
app.get('/detection', (req, res) => {
|
|
res.send(detection);
|
|
});
|
|
app.get('/timestamp', (req, res) => {
|
|
res.send(timestamp);
|
|
});
|
|
app.get('/maxhold', (req, res) => {
|
|
res.send(maxhold.get_data());
|
|
});
|
|
// 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}`);
|
|
});
|
|
|
|
// tcp listener map
|
|
const server_map = net.createServer((socket)=>{
|
|
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.");
|
|
})
|
|
});
|
|
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);
|
|
|
|
// 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);
|