SensESP 3.0.1
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_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server) {
11 auto scan_wifi_networks_handler = std::make_shared<HTTPRequestHandler>(
12 1 << HTTP_POST, "/api/wifi/scan", [](httpd_req_t* req) {
13 auto networking = SensESPApp::get()->get_networking();
14 networking->start_wifi_scan();
15 // Return status code 202 and "SCAN STARTED" message
16 httpd_resp_set_status(req, "202 Accepted");
17 httpd_resp_send(req, "SCAN STARTED", 0);
18 return ESP_OK;
19 });
20
21 server->add_handler(scan_wifi_networks_handler);
22
23 auto scan_results_handler = std::make_shared<HTTPRequestHandler>(
24 1 << HTTP_GET, "/api/wifi/scan-results", [](httpd_req_t* req) {
25 auto networking = SensESPApp::get()->get_networking();
26 std::vector<WiFiNetworkInfo> ssid_list;
27 int16_t result = networking->get_wifi_scan_results(ssid_list);
28 if (result == WIFI_SCAN_RUNNING) {
29 // Return status code 202 and "SCAN RUNNING" message
30 httpd_resp_set_status(req, "202 Accepted");
31 httpd_resp_send(req, "SCAN RUNNING", 0);
32 return ESP_OK;
33 } else if (result == WIFI_SCAN_FAILED) {
34 // Return status code 400 and "SCAN FAILED" message
35 httpd_resp_set_status(req, "400 Bad Request");
36 httpd_resp_send(req, "SCAN FAILED", 0);
37 return ESP_OK;
38 }
39 // Construct a JSON array of the scan results
40 JsonDocument doc;
41 JsonArray networks = doc["networks"].to<JsonArray>();
42 for (auto& ssid_info : ssid_list) {
43 JsonObject network = networks.add<JsonObject>();
44 ssid_info.as_json(network);
45 }
46 String json_str;
47 serializeJson(doc, json_str);
48 httpd_resp_set_type(req, "application/json");
49 httpd_resp_send(req, json_str.c_str(), json_str.length());
50 return ESP_OK;
51 });
52
53 server->add_handler(scan_results_handler);
54}
55
56void add_app_http_command_handlers(std::shared_ptr<HTTPServer>& server) {
58}
59
60} // namespace sensesp
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:57
void add_app_http_command_handlers(std::shared_ptr< HTTPServer > &server)
void add_scan_wifi_networks_handlers(std::shared_ptr< HTTPServer > &server)