mirror of
https://github.com/30hours/blah2.git
synced 2025-03-19 00:21:33 +00:00
Add timestamp API and only poll map after change
This commit is contained in:
parent
e698ad984e
commit
b77ac6b015
5 changed files with 126 additions and 68 deletions
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
29
html/plot.js
29
html/plot.js
|
@ -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,10 +66,16 @@ 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
|
// case draw new plot
|
||||||
if (data.nRows != nRows || data.nCols != nCols) {
|
if (data.nRows != nRows || data.nCols != nCols) {
|
||||||
nRows = data.nRows;
|
nRows = data.nRows;
|
||||||
|
@ -139,4 +153,15 @@ var intervalId = window.setInterval(function () {
|
||||||
.always(function () {
|
.always(function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
.fail(function () {
|
||||||
|
console.log('API Fail');
|
||||||
|
})
|
||||||
|
|
||||||
|
.always(function () {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue