Add timestamp API and only poll map after change

This commit is contained in:
30hours 2023-11-14 22:43:14 +10:30
parent e698ad984e
commit b77ac6b015
5 changed files with 126 additions and 68 deletions

View file

@ -7,6 +7,7 @@ const PORT = 3000;
const HOST = '0.0.0.0'; const HOST = '0.0.0.0';
var map = ''; var map = '';
var detection = ''; var detection = '';
var timestamp = '';
var data = ''; var data = '';
var capture = false; var capture = false;
@ -29,6 +30,9 @@ app.get('/map', (req, res) => {
app.get('/detection', (req, res) => { app.get('/detection', (req, res) => {
res.send(detection); res.send(detection);
}); });
app.get('/timestamp', (req, res) => {
res.send(timestamp);
});
// read state of capture // read state of capture
app.get('/capture', (req, res) => { app.get('/capture', (req, res) => {
res.send(capture); res.send(capture);
@ -77,3 +81,18 @@ const server_detection = net.createServer((socket)=>{
}) })
}); });
server_detection.listen(3002); 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);

View file

@ -31,6 +31,7 @@ network:
api: 3000 api: 3000
map: 3001 map: 3001
detect: 3002 detect: 3002
timestamp: 4000
save: save:
iq: true iq: true

View file

@ -31,6 +31,7 @@ network:
api: 3000 api: 3000
map: 3001 map: 3001
detection: 3002 detection: 3002
timestamp: 4000
save: save:
iq: true iq: true

View file

@ -2,6 +2,7 @@ var timestamp = -1;
var nRows = 3; var nRows = 3;
var nCols = 3; var nCols = 3;
var host = window.location.hostname; var host = window.location.hostname;
var timestamp = '';
var isLocalHost = (host === "localhost" || host === "127.0.0.1" || host === "192.168.0.112"); var isLocalHost = (host === "localhost" || host === "127.0.0.1" || host === "192.168.0.112");
@ -12,6 +13,13 @@ if (isLocalHost) {
urlMap = '//' + host + '/api/map?timestamp=' + Date.now(); urlMap = '//' + host + '/api/map?timestamp=' + Date.now();
} }
var urlTimestamp = '';
if (isLocalHost) {
urlTimestamp = '//' + host + ':3000/timestamp?timestamp=' + Date.now();
} else {
urlTimestamp = '//' + host + '/api/timestamp?timestamp=' + Date.now();
}
var data = [ var data = [
{ {
z: [[0, 0, 0], [0, 0, 0], [0, 0, 0]], z: [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
@ -58,78 +66,94 @@ var config = {
Plotly.newPlot('ddmap', data, layout, config); Plotly.newPlot('ddmap', data, layout, config);
var intervalId = window.setInterval(function () { var intervalId = window.setInterval(function () {
var apiData = $.getJSON(urlMap, function () { })
// check if timestamp is updated
var timestampData = $.get(urlTimestamp, function () { })
.done(function (data) { .done(function (data) {
if (timestamp != data) {
timestamp = data;
// get new map data
var apiData = $.getJSON(urlMap, function () { })
.done(function (data) {
// case draw new plot
if (data.nRows != nRows || data.nCols != nCols) {
nRows = data.nRows;
nCols = data.nCols;
// case draw new plot data = [
if (data.nRows != nRows || data.nCols != nCols) { {
nRows = data.nRows; z: data.data,
nCols = data.nCols; x: data.delay,
y: data.doppler,
colorscale: 'Jet',
zauto: false,
zmin: 0,
zmax: Math.max(13, data.maxPower),
type: 'heatmap'
}
];
layout = {
autosize: false,
margin: {
l: 50,
r: 50,
b: 50,
t: 50,
pad: 0
},
width: document.getElementById('ddmap').offsetWidth,
height: document.getElementById('ddmap').offsetHeight,
plot_bgcolor: "rgba(0,0,0,0)",
paper_bgcolor: "rgba(0,0,0,0)",
annotations: [],
displayModeBar: false,
xaxis: {
title: {
text: 'Bistatic Range (km)',
font: {
size: 24
}
},
ticks: '',
side: 'bottom'
},
yaxis: {
title: {
text: 'Bistatic Doppler (Hz)',
font: {
size: 24
}
},
ticks: '',
ticksuffix: ' ',
autosize: false,
categoryorder: "total descending"
}
};
Plotly.newPlot('ddmap', data, layout, { displayModeBar: false });
}
else {
data_update =
{
'z': [data.data],
'zmax': Math.max(13, data.maxPower)
};
layout_update = {
};
Plotly.update('ddmap', data_update, layout_update, { displayModeBar: false });
}
data = [ })
{
z: data.data, .fail(function () {
x: data.delay, console.log('API Fail');
y: data.doppler, })
colorscale: 'Jet',
zauto: false, .always(function () {
zmin: 0,
zmax: Math.max(13, data.maxPower), });
type: 'heatmap'
}
];
layout = {
autosize: false,
margin: {
l: 50,
r: 50,
b: 50,
t: 50,
pad: 0
},
width: document.getElementById('ddmap').offsetWidth,
height: document.getElementById('ddmap').offsetHeight,
plot_bgcolor: "rgba(0,0,0,0)",
paper_bgcolor: "rgba(0,0,0,0)",
annotations: [],
displayModeBar: false,
xaxis: {
title: {
text: 'Bistatic Range (km)',
font: {
size: 24
}
},
ticks: '',
side: 'bottom'
},
yaxis: {
title: {
text: 'Bistatic Doppler (Hz)',
font: {
size: 24
}
},
ticks: '',
ticksuffix: ' ',
autosize: false,
categoryorder: "total descending"
}
};
Plotly.newPlot('ddmap', data, layout, { displayModeBar: false });
} }
else {
data_update =
{
'z': [data.data],
'zmax': Math.max(13, data.maxPower)
};
layout_update = {
};
Plotly.update('ddmap', data_update, layout_update, { displayModeBar: false });
}
}) })
.fail(function () { .fail(function () {
@ -139,4 +163,5 @@ var intervalId = window.setInterval(function () {
.always(function () { .always(function () {
}); });
}, 500); }, 500);

View file

@ -93,22 +93,28 @@ int main(int argc, char **argv)
fftw_plan_with_nthreads(4); fftw_plan_with_nthreads(4);
// setup socket // setup socket
uint16_t port_map, port_detection; uint16_t port_map, port_detection, port_timestamp;
std::string ip; std::string ip;
tree["network"]["ports"]["map"] >> port_map; tree["network"]["ports"]["map"] >> port_map;
tree["network"]["ports"]["detection"] >> port_detection; tree["network"]["ports"]["detection"] >> port_detection;
tree["network"]["ports"]["timestamp"] >> port_timestamp;
tree["network"]["ip"] >> ip; tree["network"]["ip"] >> ip;
asio::io_service io_service; asio::io_service io_service;
asio::ip::tcp::socket socket_map(io_service); asio::ip::tcp::socket socket_map(io_service);
asio::ip::tcp::socket socket_detection(io_service); asio::ip::tcp::socket socket_detection(io_service);
asio::ip::tcp::socket socket_timestamp(io_service);
asio::ip::tcp::endpoint endpoint_map; asio::ip::tcp::endpoint endpoint_map;
asio::ip::tcp::endpoint endpoint_detection; asio::ip::tcp::endpoint endpoint_detection;
asio::ip::tcp::endpoint endpoint_timestamp;
endpoint_map = asio::ip::tcp::endpoint( endpoint_map = asio::ip::tcp::endpoint(
asio::ip::address::from_string(ip), port_map); asio::ip::address::from_string(ip), port_map);
endpoint_detection = asio::ip::tcp::endpoint( endpoint_detection = asio::ip::tcp::endpoint(
asio::ip::address::from_string(ip), port_detection); asio::ip::address::from_string(ip), port_detection);
endpoint_timestamp = asio::ip::tcp::endpoint(
asio::ip::address::from_string(ip), port_timestamp);
socket_map.connect(endpoint_map); socket_map.connect(endpoint_map);
socket_detection.connect(endpoint_detection); socket_detection.connect(endpoint_detection);
socket_timestamp.connect(endpoint_timestamp);
asio::error_code err; asio::error_code err;
std::string subdata; std::string subdata;
uint32_t MTU = 1024; uint32_t MTU = 1024;
@ -228,6 +234,12 @@ int main(int argc, char **argv)
auto t7 = std::chrono::high_resolution_clock::now(); auto t7 = std::chrono::high_resolution_clock::now();
double delta_t7 = std::chrono::duration<double, std::milli>(t7-t0).count(); double delta_t7 = std::chrono::duration<double, std::milli>(t7-t0).count();
std::cout << "CPI time (ms): " << delta_t7 << std::endl; std::cout << "CPI time (ms): " << delta_t7 << std::endl;
// output CPI timestamp for updating data
auto t0_duration = t0.time_since_epoch();
auto t0_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t0_duration).count();
std::string t0_string = std::to_string(t0_ms);
socket_timestamp.write_some(asio::buffer(t0_string, 100), err);
} }
} }
}); });