SensESP 3.2.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
app_command_handler.cpp
Go to the documentation of this file.
2
3#include <esp_http_server.h>
4#include <memory>
5
6#include "sensesp_app.h"
7
8namespace sensesp {
9
10void add_tofu_reset_handler(std::shared_ptr<HTTPServer>& server) {
11 auto tofu_reset_handler = std::make_shared<HTTPRequestHandler>(
12 1 << HTTP_POST, "/api/signalk/reset-tofu", [](httpd_req_t* req) {
13 auto ws_client = SensESPApp::get()->get_ws_client();
14 if (ws_client == nullptr) {
15 httpd_resp_set_status(req, "500 Internal Server Error");
16 httpd_resp_send(req, "WebSocket client not available", 0);
17 return ESP_OK;
18 }
19 ws_client->reset_tofu_fingerprint();
20 httpd_resp_set_type(req, "application/json");
21 httpd_resp_send(req, "{\"status\":\"ok\"}", 0);
22 return ESP_OK;
23 });
24
25 server->add_handler(tofu_reset_handler);
26}
27
28void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server,
29 std::shared_ptr<Networking>& networking) {
30 auto scan_wifi_networks_handler = std::make_shared<HTTPRequestHandler>(
31 1 << HTTP_POST, "/api/wifi/scan", [networking](httpd_req_t* req) {
32 networking->start_wifi_scan();
33 // Return status code 202 and "SCAN STARTED" message
34 httpd_resp_set_status(req, "202 Accepted");
35 httpd_resp_send(req, "SCAN STARTED", 0);
36 return ESP_OK;
37 });
38
39 server->add_handler(scan_wifi_networks_handler);
40
41 auto scan_results_handler = std::make_shared<HTTPRequestHandler>(
42 1 << HTTP_GET, "/api/wifi/scan-results", [networking](httpd_req_t* req) {
43 std::vector<WiFiNetworkInfo> ssid_list;
44 int16_t result = networking->get_wifi_scan_results(ssid_list);
45 if (result == WIFI_SCAN_RUNNING) {
46 // Return status code 202 and "SCAN RUNNING" message
47 httpd_resp_set_status(req, "202 Accepted");
48 httpd_resp_send(req, "SCAN RUNNING", 0);
49 return ESP_OK;
50 } else if (result == WIFI_SCAN_FAILED) {
51 // Return status code 400 and "SCAN FAILED" message
52 httpd_resp_set_status(req, "400 Bad Request");
53 httpd_resp_send(req, "SCAN FAILED", 0);
54 return ESP_OK;
55 }
56 // Construct a JSON array of the scan results
57 JsonDocument doc;
58 JsonArray networks = doc["networks"].to<JsonArray>();
59 for (auto& ssid_info : ssid_list) {
60 JsonObject network = networks.add<JsonObject>();
61 ssid_info.as_json(network);
62 }
63 String json_str;
64 serializeJson(doc, json_str);
65 httpd_resp_set_type(req, "application/json");
66 httpd_resp_send(req, json_str.c_str(), json_str.length());
67 return ESP_OK;
68 });
69
70 server->add_handler(scan_results_handler);
71}
72
73void add_app_http_command_handlers(std::shared_ptr<HTTPServer>& server,
74 std::shared_ptr<Networking>& networking) {
75 add_scan_wifi_networks_handlers(server, networking);
77}
78
79} // namespace sensesp
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:49
SKWSClient * ws_client
void add_scan_wifi_networks_handlers(std::shared_ptr< HTTPServer > &server, std::shared_ptr< Networking > &networking)
void add_tofu_reset_handler(std::shared_ptr< HTTPServer > &server)
void add_app_http_command_handlers(std::shared_ptr< HTTPServer > &server, std::shared_ptr< Networking > &networking)