SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
base_command_handler.cpp
Go to the documentation of this file.
2
5
6namespace sensesp {
7
8void add_http_reset_handler(std::shared_ptr<HTTPServer>& server) {
9 auto reset_handler = std::make_shared<HTTPRequestHandler>(
10 1 << HTTP_POST, "/api/device/reset", [](httpd_req_t* req) {
11 httpd_resp_send(req,
12 "Resetting device back to factory defaults. "
13 "You may have to reconfigure the WiFi settings.",
14 0);
15 event_loop()->onDelay(500, []() { SensESPBaseApp::get()->reset(); });
16 return ESP_OK;
17 });
18 server->add_handler(reset_handler);
19}
20
21void add_http_restart_handler(std::shared_ptr<HTTPServer>& server) {
22 auto restart_handler = std::make_shared<HTTPRequestHandler>(
23 1 << HTTP_POST, "/api/device/restart", [](httpd_req_t* req) {
24 httpd_resp_send(req, "Restarting device", 0);
25 event_loop()->onDelay(500, []() { ESP.restart(); });
26 return ESP_OK;
27 });
28 server->add_handler(restart_handler);
29}
30
31void add_http_info_handler(std::shared_ptr<HTTPServer>& server) {
32 auto info_handler = std::make_shared<HTTPRequestHandler>(
33 1 << HTTP_GET, "/api/info", [](httpd_req_t* req) {
34 auto status_page_items = StatusPageItemBase::get_status_page_items();
35
36 JsonDocument json_doc;
37 JsonArray info_items = json_doc.to<JsonArray>();
38
39 for (auto info_item = status_page_items->begin();
40 info_item != status_page_items->end(); ++info_item) {
41 info_items.add(info_item->second->as_json());
42 }
43
44 String response;
45 serializeJson(json_doc, response);
46 httpd_resp_set_type(req, "application/json");
47 httpd_resp_sendstr(req, response.c_str());
48 return ESP_OK;
49 });
50 server->add_handler(info_handler);
51}
52
53void add_routes_handlers(std::shared_ptr<HTTPServer>& server) {
54 std::vector<RouteDefinition> routes;
55
56 routes.push_back(RouteDefinition("Status", "/status", "StatusPage"));
57 routes.push_back(RouteDefinition("System", "/system", "SystemPage"));
58 routes.push_back(RouteDefinition("WiFi", "/wifi", "WiFiConfigPage"));
59 routes.push_back(RouteDefinition("Signal K", "/signalk", "SignalKPage"));
60 routes.push_back(
61 RouteDefinition("Configuration", "/configuration", "ConfigurationPage"));
62
63 // Pre-render the response
64 JsonDocument json_doc;
65 JsonArray routes_json = json_doc.to<JsonArray>();
66
67 int sz = routes.size();
68
69 for (auto it = routes.begin(); it != routes.end(); ++it) {
70 routes_json.add(it->as_json());
71 }
72
73 String response;
74
75 serializeJson(routes_json, response);
76
77 auto routes_handler = std::make_shared<HTTPRequestHandler>(
78 1 << HTTP_GET, "/api/routes", [response](httpd_req_t* req) {
79 httpd_resp_set_type(req, "application/json");
80 httpd_resp_sendstr(req, response.c_str());
81 return ESP_OK;
82 });
83 server->add_handler(routes_handler);
84
85 // Find the root page
86
87 StaticFileData* root_page = nullptr;
88 for (int i = 0; i < sizeof(kFrontendFiles) / sizeof(StaticFileData); i++) {
89 if (strcmp(kFrontendFiles[i].url, "/") == 0) {
90 root_page = (StaticFileData*)&kFrontendFiles[i];
91 break;
92 }
93 }
94 if (root_page == nullptr) {
95 ESP_LOGE(__FILENAME__, "Root page not found in kWebUIFiles");
96 return;
97 }
98
99 // Add a handler for each route that returns the root page
100
101 for (auto it = routes.begin(); it != routes.end(); ++it) {
102 String path = it->get_path();
103 auto route_handler = std::make_shared<HTTPRequestHandler>(
104 1 << HTTP_GET, path.c_str(), [root_page](httpd_req_t* req) {
105 httpd_resp_set_type(req, root_page->content_type);
106 if (root_page->content_encoding != nullptr) {
107 httpd_resp_set_hdr(req, kContentEncoding,
108 root_page->content_encoding);
109 }
110 httpd_resp_send(req, root_page->content, root_page->content_length);
111 return ESP_OK;
112 });
113 server->add_handler(route_handler);
114 }
115}
116
117void add_base_app_http_command_handlers(std::shared_ptr<HTTPServer>& server) {
120 add_http_info_handler(server);
121 add_routes_handlers(server);
122}
123
124} // namespace sensesp
static const std::shared_ptr< SensESPBaseApp > & get()
Get the singleton instance of the SensESPBaseApp.
static const std::map< String, StatusPageItemBase * > * get_status_page_items()
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
void add_http_info_handler(std::shared_ptr< HTTPServer > &server)
const StaticFileData kFrontendFiles[]
void add_http_restart_handler(std::shared_ptr< HTTPServer > &server)
void add_http_reset_handler(std::shared_ptr< HTTPServer > &server)
void add_base_app_http_command_handlers(std::shared_ptr< HTTPServer > &server)
void add_routes_handlers(std::shared_ptr< HTTPServer > &server)
const unsigned int content_length