blah2/api/server.js

218 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-05-04 11:25:37 +00:00
const express = require('express');
const net = require("net");
const fs = require('fs');
const yaml = require('js-yaml');
const dns = require('dns');
// parse config file
var config;
try {
const file = process.argv[2];
config = yaml.load(fs.readFileSync(file, 'utf8'));
} catch (e) {
console.error('Error reading or parsing the YAML file:', e);
}
2023-05-04 11:25:37 +00:00
var stash_map = require('./stash/maxhold.js');
var stash_detection = require('./stash/detection.js');
var stash_iqdata = require('./stash/iqdata.js');
var stash_timing = require('./stash/timing.js');
2023-05-04 11:25:37 +00:00
// constants
const PORT = config.network.ports.api;
const HOST = config.network.ip;
2023-05-04 11:25:37 +00:00
var map = '';
var detection = '';
var track = '';
var timestamp = '';
2023-11-20 12:35:01 +00:00
var timing = '';
2023-12-11 12:11:54 +00:00
var iqdata = '';
var data_map;
var data_detection;
var data_tracker;
var data_timestamp;
var data_timing;
var data_iqdata;
2023-05-04 11:25:37 +00:00
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('/api/map', (req, res) => {
2023-05-04 11:25:37 +00:00
res.send(map);
});
app.get('/api/detection', (req, res) => {
res.send(detection);
});
app.get('/api/tracker', (req, res) => {
res.send(track);
});
app.get('/api/timestamp', (req, res) => {
res.send(timestamp);
});
app.get('/api/timing', (req, res) => {
2023-11-20 12:35:01 +00:00
res.send(timing);
});
2023-12-11 12:11:54 +00:00
app.get('/api/iqdata', (req, res) => {
res.send(iqdata);
});
app.get('/api/config', (req, res) => {
res.send(config);
});
app.get('/api/adsb2dd', (req, res) => {
if (config.truth.adsb.enabled == true) {
2024-02-18 01:48:50 +00:00
const api_url = "http://" + config.truth.adsb.adsb2dd + "/api/dd";
const api_query =
api_url +
"?rx=" + config.location.rx.latitude + "," +
config.location.rx.longitude + "," +
config.location.rx.altitude +
"&tx=" + config.location.tx.latitude + "," +
config.location.tx.longitude + "," +
config.location.tx.altitude +
2024-02-12 09:04:13 +00:00
"&fc=" + (config.capture.fc / 1000000) +
2024-02-18 01:48:50 +00:00
"&server=" + "http://" + config.truth.adsb.tar1090;
2024-02-12 09:04:13 +00:00
const jsonResponse = {
url: api_query
};
res.json(jsonResponse);
}
else {
res.status(400).end();
}
});
2023-12-12 14:40:52 +00:00
// stash API
app.get('/stash/map', (req, res) => {
res.send(stash_map.get_data_map());
});
app.get('/stash/detection', (req, res) => {
res.send(stash_detection.get_data_detection());
});
2023-12-12 14:40:52 +00:00
app.get('/stash/iqdata', (req, res) => {
res.send(stash_iqdata.get_data_iqdata());
2023-12-12 14:40:52 +00:00
});
2023-12-17 03:47:40 +00:00
app.get('/stash/timing', (req, res) => {
res.send(stash_timing.get_data_timing());
2023-12-17 03:47:40 +00:00
});
2023-12-12 14:40:52 +00:00
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}`);
});
// tcp listener map
const server_map = net.createServer((socket)=>{
2023-05-04 11:25:37 +00:00
socket.on("data",(msg)=>{
data_map = data_map + msg.toString();
if (data_map.slice(-1) === "}")
2023-05-04 11:25:37 +00:00
{
map = data_map;
data_map = '';
2023-05-04 11:25:37 +00:00
}
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_map.listen(config.network.ports.map);
// tcp listener detection
const server_detection = net.createServer((socket)=>{
socket.on("data",(msg)=>{
data_detection = data_detection + msg.toString();
if (data_detection.slice(-1) === "}")
{
detection = data_detection;
data_detection = '';
}
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_detection.listen(config.network.ports.detection);
// tcp listener tracker
const server_tracker = net.createServer((socket)=>{
socket.on("data",(msg)=>{
data_tracker = data_tracker + msg.toString();
if (data_tracker.slice(-1) === "}")
{
track = data_tracker;
data_tracker = '';
}
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_tracker.listen(config.network.ports.track);
// tcp listener timestamp
const server_timestamp = net.createServer((socket)=>{
socket.on("data",(msg)=>{
data_timestamp = data_timestamp + msg.toString();
timestamp = data_timestamp;
data_timestamp = '';
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_timestamp.listen(config.network.ports.timestamp);
2023-11-20 12:35:01 +00:00
// tcp listener timing
const server_timing = net.createServer((socket)=>{
socket.on("data",(msg)=>{
data_timing = data_timing + msg.toString();
if (data_timing.slice(-1) === "}")
2023-11-20 12:35:01 +00:00
{
timing = data_timing;
data_timing = '';
2023-11-20 12:35:01 +00:00
}
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_timing.listen(config.network.ports.timing);
2023-12-11 12:11:54 +00:00
// tcp listener iqdata metadata
const server_iqdata = net.createServer((socket)=>{
socket.on("data",(msg)=>{
data_iqdata = data_iqdata + msg.toString();
if (data_iqdata.slice(-1) === "}")
2023-12-11 12:11:54 +00:00
{
iqdata = data_iqdata;
data_iqdata = '';
2023-12-11 12:11:54 +00:00
}
});
socket.on("close",()=>{
console.log("Connection closed.");
})
});
server_iqdata.listen(config.network.ports.iqdata);
2024-03-23 15:59:16 +00:00
process.on('SIGTERM', () => {
console.log('SIGTERM signal received.');
process.exit(0);
});