Skip to content

Commit

Permalink
Allow using app command handlers without SensESPApp
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Dec 11, 2024
1 parent 2b06f1d commit 42b8c79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/sensesp/net/web/app_command_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace sensesp {

void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server) {
void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server,
std::shared_ptr<Networking>& networking) {
auto scan_wifi_networks_handler = std::make_shared<HTTPRequestHandler>(
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");
Expand All @@ -21,8 +21,7 @@ void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server) {
server->add_handler(scan_wifi_networks_handler);

auto scan_results_handler = std::make_shared<HTTPRequestHandler>(
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<WiFiNetworkInfo> ssid_list;
int16_t result = networking->get_wifi_scan_results(ssid_list);
if (result == WIFI_SCAN_RUNNING) {
Expand Down Expand Up @@ -53,8 +52,9 @@ void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server) {
server->add_handler(scan_results_handler);
}

void add_app_http_command_handlers(std::shared_ptr<HTTPServer>& server) {
add_scan_wifi_networks_handlers(server);
void add_app_http_command_handlers(std::shared_ptr<HTTPServer>& server,
std::shared_ptr<Networking>& networking) {
add_scan_wifi_networks_handlers(server, networking);
}

} // namespace sensesp
4 changes: 3 additions & 1 deletion src/sensesp/net/web/app_command_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPServer>& server);
void add_app_http_command_handlers(std::shared_ptr<HTTPServer>& server,
std::shared_ptr<Networking>& networking);

} // namespace sensesp

Expand Down
2 changes: 1 addition & 1 deletion src/sensesp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down

0 comments on commit 42b8c79

Please sign in to comment.