From 42b8c7926303c1cf0845a83d387604ebac62cc2b Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Wed, 11 Dec 2024 11:04:39 +0200 Subject: [PATCH] Allow using app command handlers without SensESPApp --- src/sensesp/net/web/app_command_handler.cpp | 14 +++++++------- src/sensesp/net/web/app_command_handler.h | 4 +++- src/sensesp_app.h | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/sensesp/net/web/app_command_handler.cpp b/src/sensesp/net/web/app_command_handler.cpp index bb2aea332..eb68bb2b6 100644 --- a/src/sensesp/net/web/app_command_handler.cpp +++ b/src/sensesp/net/web/app_command_handler.cpp @@ -7,10 +7,10 @@ namespace sensesp { -void add_scan_wifi_networks_handlers(std::shared_ptr& server) { +void add_scan_wifi_networks_handlers(std::shared_ptr& server, + std::shared_ptr& networking) { auto scan_wifi_networks_handler = std::make_shared( - 1 << HTTP_POST, "/api/wifi/scan", [](httpd_req_t* req) { - auto networking = SensESPApp::get()->get_networking(); + 1 << HTTP_POST, "/api/wifi/scan", [networking](httpd_req_t* req) { networking->start_wifi_scan(); // Return status code 202 and "SCAN STARTED" message httpd_resp_set_status(req, "202 Accepted"); @@ -21,8 +21,7 @@ void add_scan_wifi_networks_handlers(std::shared_ptr& server) { server->add_handler(scan_wifi_networks_handler); auto scan_results_handler = std::make_shared( - 1 << HTTP_GET, "/api/wifi/scan-results", [](httpd_req_t* req) { - auto networking = SensESPApp::get()->get_networking(); + 1 << HTTP_GET, "/api/wifi/scan-results", [networking](httpd_req_t* req) { std::vector ssid_list; int16_t result = networking->get_wifi_scan_results(ssid_list); if (result == WIFI_SCAN_RUNNING) { @@ -53,8 +52,9 @@ void add_scan_wifi_networks_handlers(std::shared_ptr& server) { server->add_handler(scan_results_handler); } -void add_app_http_command_handlers(std::shared_ptr& server) { - add_scan_wifi_networks_handlers(server); +void add_app_http_command_handlers(std::shared_ptr& server, + std::shared_ptr& networking) { + add_scan_wifi_networks_handlers(server, networking); } } // namespace sensesp diff --git a/src/sensesp/net/web/app_command_handler.h b/src/sensesp/net/web/app_command_handler.h index c700c8f64..2bd5aff1a 100644 --- a/src/sensesp/net/web/app_command_handler.h +++ b/src/sensesp/net/web/app_command_handler.h @@ -6,10 +6,12 @@ #include "ArduinoJson.h" #include "sensesp/net/http_server.h" +#include "sensesp/net/networking.h" namespace sensesp { -void add_app_http_command_handlers(std::shared_ptr& server); +void add_app_http_command_handlers(std::shared_ptr& server, + std::shared_ptr& networking); } // namespace sensesp diff --git a/src/sensesp_app.h b/src/sensesp_app.h index 20a19fbd4..09d762a87 100644 --- a/src/sensesp_app.h +++ b/src/sensesp_app.h @@ -167,7 +167,7 @@ class SensESPApp : public SensESPBaseApp { // Add the default HTTP server response handlers add_static_file_handlers(this->http_server_); add_base_app_http_command_handlers(this->http_server_); - add_app_http_command_handlers(this->http_server_); + add_app_http_command_handlers(this->http_server_, this->networking_); add_config_handlers(this->http_server_); ConfigItem(this->http_server_);