#include "web_server.h" #include "device_list.h" #include "ping.h" #include "esp_http_server.h" #include "esp_log.h" static const char *TAG = "web_server"; static esp_err_t main_page_handler(httpd_req_t *req); static esp_err_t ping_handler(httpd_req_t *req); static esp_err_t send_page_header(httpd_req_t *req); static esp_err_t send_device_table(httpd_req_t *req); static esp_err_t send_device_row(httpd_req_t *req, const device_info_t *device); static esp_err_t send_page_footer(httpd_req_t *req); static httpd_handle_t start_webserver(void); static void register_http_handlers(httpd_handle_t server); static const httpd_uri_t main_uri = {.uri = "/", .method = HTTP_GET, .handler = main_page_handler, .user_ctx = NULL}; static const httpd_uri_t ping_uri = {.uri = "/ping", .method = HTTP_GET, .handler = ping_handler, .user_ctx = NULL}; void web_server_init(void) { httpd_handle_t server_handle = start_webserver(); if (server_handle != NULL) { register_http_handlers(server_handle); } } static httpd_handle_t start_webserver(void) { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); httpd_handle_t server = NULL; if (httpd_start(&server, &config) == ESP_OK) { ESP_LOGI(TAG, "HTTP server started"); } else { ESP_LOGE(TAG, "Failed to start http server"); } return server; } static void register_http_handlers(httpd_handle_t server) { esp_err_t err = httpd_register_uri_handler(server, &main_uri); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register main URI"); } err = httpd_register_uri_handler(server, &ping_uri); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register ping URI"); } } static esp_err_t main_page_handler(httpd_req_t *req) { httpd_resp_set_type(req, "text/html"); esp_err_t err; err = send_page_header(req); if (err != ESP_OK) { ESP_LOGE(TAG, "Page header isn't created"); return err; } err = send_device_table(req); if (err != ESP_OK) { ESP_LOGE(TAG, "Table isn't created"); return err; } err = send_page_footer(req); if (err != ESP_OK) { ESP_LOGE(TAG, "Page footer isn't created"); return err; } return httpd_resp_sendstr_chunk(req, NULL);; } static esp_err_t send_page_header(httpd_req_t *req) { return httpd_resp_sendstr_chunk( req, "" "" "
" "" "