105 lines
2.7 KiB
C++
105 lines
2.7 KiB
C++
#include "web_server.hpp"
|
|
#include "app_config.hpp"
|
|
#include <esp_log.h>
|
|
#include <string.h>
|
|
|
|
static const char *TAG = "web_server";
|
|
|
|
WebServer WebServer::instance;
|
|
|
|
// Root handler - serves a simple dashboard
|
|
static esp_err_t rootHandler(httpd_req_t* req)
|
|
{
|
|
const char* response = R"(
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>ZoneBridge Dashboard</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<style>
|
|
body { font-family: Arial; margin: 20px; background-color: #f0f0f0; }
|
|
.container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; }
|
|
h1 { color: #333; }
|
|
.status { padding: 10px; margin: 10px 0; border-radius: 4px; }
|
|
.online { background-color: #d4edda; color: #155724; }
|
|
.offline { background-color: #f8d7da; color: #721c24; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>ZoneBridge Control Panel</h1>
|
|
<div class="status online">System Online</div>
|
|
<p>ESP32-S3 Touch LCD 4.3" is running</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
)";
|
|
|
|
httpd_resp_set_type(req, "text/html");
|
|
return httpd_resp_send(req, response, strlen(response));
|
|
}
|
|
|
|
// API handler for status
|
|
static esp_err_t apiStatusHandler(httpd_req_t* req)
|
|
{
|
|
const char* json_response = R"({"status":"online","uptime":0})";
|
|
httpd_resp_set_type(req, "application/json");
|
|
return httpd_resp_send(req, json_response, strlen(json_response));
|
|
}
|
|
|
|
WebServer& WebServer::getInstance()
|
|
{
|
|
return instance;
|
|
}
|
|
|
|
void WebServer::start(int port)
|
|
{
|
|
if (server != nullptr) {
|
|
ESP_LOGW(TAG, "Web server already running");
|
|
return;
|
|
}
|
|
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
config.server_port = port;
|
|
config.stack_size = WEB_SERVER_STACK_SIZE;
|
|
config.max_uri_handlers = 16;
|
|
|
|
if (httpd_start(&server, &config) != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to start web server");
|
|
return;
|
|
}
|
|
|
|
// Register URL handlers
|
|
httpd_uri_t root = {
|
|
.uri = "/",
|
|
.method = HTTP_GET,
|
|
.handler = rootHandler,
|
|
.user_ctx = nullptr
|
|
};
|
|
httpd_register_uri_handler(server, &root);
|
|
|
|
httpd_uri_t api_status = {
|
|
.uri = "/api/status",
|
|
.method = HTTP_GET,
|
|
.handler = apiStatusHandler,
|
|
.user_ctx = nullptr
|
|
};
|
|
httpd_register_uri_handler(server, &api_status);
|
|
|
|
ESP_LOGI(TAG, "Web server started on port %d", port);
|
|
}
|
|
|
|
void WebServer::stop()
|
|
{
|
|
if (server != nullptr) {
|
|
httpd_stop(server);
|
|
server = nullptr;
|
|
ESP_LOGI(TAG, "Web server stopped");
|
|
}
|
|
}
|
|
|
|
bool WebServer::isRunning() const
|
|
{
|
|
return server != nullptr;
|
|
}
|