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

View file

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

View file

@ -2,6 +2,7 @@ var timestamp = -1;
var nRows = 3;
var nCols = 3;
var host = window.location.hostname;
var timestamp = '';
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();
}
var urlTimestamp = '';
if (isLocalHost) {
urlTimestamp = '//' + host + ':3000/timestamp?timestamp=' + Date.now();
} else {
urlTimestamp = '//' + host + '/api/timestamp?timestamp=' + Date.now();
}
var data = [
{
z: [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
@ -58,10 +66,16 @@ var config = {
Plotly.newPlot('ddmap', data, layout, config);
var intervalId = window.setInterval(function () {
var apiData = $.getJSON(urlMap, function () { })
// check if timestamp is updated
var timestampData = $.get(urlTimestamp, function () { })
.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;
@ -139,4 +153,15 @@ var intervalId = window.setInterval(function () {
.always(function () {
});
}
})
.fail(function () {
console.log('API Fail');
})
.always(function () {
});
}, 500);

View file

@ -93,22 +93,28 @@ int main(int argc, char **argv)
fftw_plan_with_nthreads(4);
// setup socket
uint16_t port_map, port_detection;
uint16_t port_map, port_detection, port_timestamp;
std::string ip;
tree["network"]["ports"]["map"] >> port_map;
tree["network"]["ports"]["detection"] >> port_detection;
tree["network"]["ports"]["timestamp"] >> port_timestamp;
tree["network"]["ip"] >> ip;
asio::io_service io_service;
asio::ip::tcp::socket socket_map(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_detection;
asio::ip::tcp::endpoint endpoint_timestamp;
endpoint_map = asio::ip::tcp::endpoint(
asio::ip::address::from_string(ip), port_map);
endpoint_detection = asio::ip::tcp::endpoint(
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_detection.connect(endpoint_detection);
socket_timestamp.connect(endpoint_timestamp);
asio::error_code err;
std::string subdata;
uint32_t MTU = 1024;
@ -228,6 +234,12 @@ int main(int argc, char **argv)
auto t7 = std::chrono::high_resolution_clock::now();
double delta_t7 = std::chrono::duration<double, std::milli>(t7-t0).count();
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);
}
}
});