From de1c882b0fc2a01e4eaea5c433262e7e8399ac24 Mon Sep 17 00:00:00 2001 From: Aang23 Date: Tue, 26 Jul 2022 13:35:14 +0200 Subject: [PATCH] Add stats webserver back --- pipelines/GOES.json | 2 +- .../grb/module_goes_grb_cadu_extractor.cpp | 5 +- src-cli/live.cpp | 70 +++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/pipelines/GOES.json b/pipelines/GOES.json index 5cd8a974b..cec370896 100644 --- a/pipelines/GOES.json +++ b/pipelines/GOES.json @@ -156,7 +156,7 @@ "name": "GOES-R GRB", "live": true, "live_cfg": { - "pkt_size": 22528, //2048 * 11, + "pkt_size": 204800, // 2048 * 100, "default": [ [ 1, diff --git a/plugins/goes_support/goes/grb/module_goes_grb_cadu_extractor.cpp b/plugins/goes_support/goes/grb/module_goes_grb_cadu_extractor.cpp index f5cc0baa1..152aba72b 100644 --- a/plugins/goes_support/goes/grb/module_goes_grb_cadu_extractor.cpp +++ b/plugins/goes_support/goes/grb/module_goes_grb_cadu_extractor.cpp @@ -105,10 +105,13 @@ namespace goes { std::memmove(cadu_buffer, &cadu_buffer[best_pos], CADU_SIZE - best_pos); - memcpy(&cadu_buffer[BBFRAME_SIZE - best_pos], &caduVector[0], best_pos); + memcpy(&cadu_buffer[CADU_SIZE - best_pos], &caduVector[0], best_pos); caduVector.erase(caduVector.begin(), caduVector.begin() + best_pos); } + module_stats["correlation"] = best_cor; + module_stats["synced"] = cadu_sync; + if (output_data_type == DATA_FILE) data_out.write((char *)cadu_buffer, CADU_SIZE); else diff --git a/src-cli/live.cpp b/src-cli/live.cpp index 2da60a39c..ac925add7 100644 --- a/src-cli/live.cpp +++ b/src-cli/live.cpp @@ -5,6 +5,9 @@ #include "logger.h" #include "init.h" #include "common/cli_utils.h" +#include +#include +#include // Catch CTRL+C to exit live properly! bool live_should_exit = false; @@ -14,6 +17,52 @@ void sig_handler_live(int signo) live_should_exit = true; } +// Webserver for stats +namespace webserver +{ + nng_http_server *http_server; + nng_url *url; + nng_http_handler *handler; + + satdump::LivePipeline *live_pipeline; + bool is_active = false; + + // HTTP Handler for stats + void http_handle(nng_aio *aio) + { + std::string jsonstr = live_pipeline->getModulesStats().dump(4); + + nng_http_res *res; + nng_http_res_alloc(&res); + nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size()); + nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8"); + nng_aio_set_output(aio, 0, res); + nng_aio_finish(aio, 0); + } + + void start(std::string http_server_url) + { + http_server_url = "http://" + http_server_url; + nng_url_parse(&url, http_server_url.c_str()); + nng_http_server_hold(&http_server, url); + nng_http_handler_alloc(&handler, url->u_path, http_handle); + nng_http_handler_set_method(handler, "GET"); + nng_http_server_add_handler(http_server, handler); + nng_http_server_start(http_server); + nng_url_free(url); + is_active = true; + } + + void stop() + { + if (is_active) + { + nng_http_server_stop(http_server); + nng_http_server_release(http_server); + } + } +}; + int main_live(int argc, char *argv[]) { if (argc < 5) // Check overall command @@ -70,6 +119,15 @@ int main_live(int argc, char *argv[]) return 1; } + // If requested, boot up webserver + if (parameters.contains("http_server")) + { + std::string http_addr = parameters["http_server"].get(); + webserver::live_pipeline = live_pipeline.get(); + logger->info("Start webserver on {:s}", http_addr.c_str()); + webserver::start(http_addr); + } + // Attach signal signal(SIGINT, sig_handler_live); @@ -174,6 +232,15 @@ int main_live(int argc, char *argv[]) return 1; } + // If requested, boot up webserver + if (parameters.contains("http_server")) + { + std::string http_addr = parameters["http_server"].get(); + webserver::live_pipeline = live_pipeline.get(); + logger->info("Start webserver on {:s}", http_addr.c_str()); + webserver::start(http_addr); + } + // Attach signal signal(SIGINT, sig_handler_live); @@ -205,5 +272,8 @@ int main_live(int argc, char *argv[]) live_pipeline->stop(); } + if (parameters.contains("http_server")) + webserver::stop(); + return 0; } \ No newline at end of file