12constexpr char kButtonsUriPrefix[] =
"/api/buttons/";
18void add_button_list_handler(std::shared_ptr<HTTPServer>& server) {
19 auto handler = std::make_shared<HTTPRequestHandler>(
20 1 << HTTP_GET,
"/api/buttons", [](httpd_req_t* req) {
21 JsonDocument json_doc;
22 JsonArray arr = json_doc.to<JsonArray>();
24 for (
const auto& entry : UIButton::get_ui_buttons()) {
30 JsonObject obj = arr.add<JsonObject>();
31 obj[
"name"] = entry.second->get_name();
32 obj[
"title"] = entry.second->get_title();
33 obj[
"mustConfirm"] = entry.second->get_must_confirm();
37 serializeJson(json_doc, response);
38 httpd_resp_set_type(req,
"application/json");
39 httpd_resp_sendstr(req, response.c_str());
42 server->add_handler(handler);
45void add_button_click_handler(std::shared_ptr<HTTPServer>& server) {
46 auto handler = std::make_shared<HTTPRequestHandler>(
47 1 << HTTP_POST, String(kButtonsUriPrefix) +
"*", [](httpd_req_t* req) {
52 String url_tail = String(req->uri).substring(strlen(kButtonsUriPrefix));
53 int query_start = url_tail.indexOf(
'?');
54 if (query_start != -1) {
55 url_tail = url_tail.substring(0, query_start);
57 if (url_tail.length() > kMaxEncodedNameLength) {
58 httpd_resp_send_err(req, HTTPD_404_NOT_FOUND,
59 "No button found with that name");
62 char name_cstr[kMaxEncodedNameLength + 1];
64 String name(name_cstr);
66 httpd_resp_send_err(req, HTTPD_404_NOT_FOUND,
67 "No button found with that name");
72 auto it = buttons.find(name);
73 if (it == buttons.end()) {
74 httpd_resp_send_err(req, HTTPD_404_NOT_FOUND,
75 "No button found with that name");
79 httpd_resp_set_type(req,
"application/json");
80 httpd_resp_sendstr(req,
"{\"status\":\"ok\"}");
87 auto button = it->second;
88 event_loop()->onDelay(0, [button]() { button->notify(); });
91 server->add_handler(handler);
97 add_button_list_handler(server);
98 add_button_click_handler(server);
std::shared_ptr< reactesp::EventLoop > event_loop()
bool check_origin(httpd_req_t *req)
void add_button_handlers(std::shared_ptr< HTTPServer > &server)
Handle HTTP requests to /api/buttons.
void urldecode2(char *dst, const char *src)