show website via mdns

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2026-01-05 23:47:00 +01:00
parent 29785a96bc
commit ebf0dc6556
15 changed files with 1549 additions and 5 deletions

View File

@@ -0,0 +1,63 @@
#pragma once
#include <esp_http_server.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Register all API handlers with the HTTP server
*
* @param server HTTP server handle
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_handlers_register(httpd_handle_t server);
// Capabilities API
esp_err_t api_capabilities_get_handler(httpd_req_t *req);
// WiFi API
esp_err_t api_wifi_scan_handler(httpd_req_t *req);
esp_err_t api_wifi_config_handler(httpd_req_t *req);
esp_err_t api_wifi_status_handler(httpd_req_t *req);
// Light Control API
esp_err_t api_light_power_handler(httpd_req_t *req);
esp_err_t api_light_thunder_handler(httpd_req_t *req);
esp_err_t api_light_mode_handler(httpd_req_t *req);
esp_err_t api_light_schema_handler(httpd_req_t *req);
esp_err_t api_light_status_handler(httpd_req_t *req);
// LED Configuration API
esp_err_t api_wled_config_get_handler(httpd_req_t *req);
esp_err_t api_wled_config_post_handler(httpd_req_t *req);
// Schema API
esp_err_t api_schema_get_handler(httpd_req_t *req);
esp_err_t api_schema_post_handler(httpd_req_t *req);
// Devices API (Matter)
esp_err_t api_devices_scan_handler(httpd_req_t *req);
esp_err_t api_devices_pair_handler(httpd_req_t *req);
esp_err_t api_devices_paired_handler(httpd_req_t *req);
esp_err_t api_devices_update_handler(httpd_req_t *req);
esp_err_t api_devices_unpair_handler(httpd_req_t *req);
esp_err_t api_devices_toggle_handler(httpd_req_t *req);
// Scenes API
esp_err_t api_scenes_get_handler(httpd_req_t *req);
esp_err_t api_scenes_post_handler(httpd_req_t *req);
esp_err_t api_scenes_delete_handler(httpd_req_t *req);
esp_err_t api_scenes_activate_handler(httpd_req_t *req);
// Static file serving
esp_err_t api_static_file_handler(httpd_req_t *req);
// Captive portal detection
esp_err_t api_captive_portal_handler(httpd_req_t *req);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,119 @@
#pragma once
#include <esp_err.h>
#include <esp_http_server.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Configuration for the API server
*/
typedef struct
{
const char *hostname; ///< mDNS hostname (default: "system-control")
uint16_t port; ///< HTTP server port (default: 80)
const char *base_path; ///< Base path for static files (default: "/storage/www")
bool enable_cors; ///< Enable CORS headers (default: true)
} api_server_config_t;
#ifdef CONFIG_API_SERVER_ENABLE_CORS
#define API_SERVER_ENABLE_CORS true
#else
#define API_SERVER_ENABLE_CORS false
#endif
/**
* @brief Default configuration for the API server
*/
#define API_SERVER_CONFIG_DEFAULT() \
{ \
.hostname = CONFIG_API_SERVER_HOSTNAME, \
.port = CONFIG_API_SERVER_PORT, \
.base_path = CONFIG_API_SERVER_STATIC_FILES_PATH, \
.enable_cors = API_SERVER_ENABLE_CORS, \
}
/**
* @brief Initialize and start the API server with mDNS
*
* This function starts an HTTP server with:
* - REST API endpoints
* - WebSocket endpoint at /ws
* - mDNS registration (system-control.local)
* - Static file serving from SPIFFS
*
* @param config Pointer to server configuration, or NULL for defaults
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_start(const api_server_config_t *config);
/**
* @brief Stop the API server and mDNS
*
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_stop(void);
/**
* @brief Check if the API server is running
*
* @return true if server is running
*/
bool api_server_is_running(void);
/**
* @brief Get the HTTP server handle
*
* @return httpd_handle_t Server handle, or NULL if not running
*/
httpd_handle_t api_server_get_handle(void);
/**
* @brief Broadcast a message to all connected WebSocket clients
*
* @param message JSON message to broadcast
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_ws_broadcast(const char *message);
/**
* @brief Broadcast a status update to all WebSocket clients
*
* @param on Light power state
* @param mode Current mode (day/night/simulation)
* @param schema Active schema filename
* @param r Red value (0-255)
* @param g Green value (0-255)
* @param b Blue value (0-255)
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_ws_broadcast_status(bool on, const char *mode, const char *schema, uint8_t r, uint8_t g,
uint8_t b);
/**
* @brief Broadcast a color update to all WebSocket clients
*
* @param r Red value (0-255)
* @param g Green value (0-255)
* @param b Blue value (0-255)
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_ws_broadcast_color(uint8_t r, uint8_t g, uint8_t b);
/**
* @brief Broadcast a WiFi status update to all WebSocket clients
*
* @param connected Connection state
* @param ip IP address (can be NULL if not connected)
* @param rssi Signal strength
* @return esp_err_t ESP_OK on success
*/
esp_err_t api_server_ws_broadcast_wifi(bool connected, const char *ip, int rssi);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,60 @@
#pragma once
#include <esp_http_server.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Maximum number of concurrent WebSocket connections
*/
#define WS_MAX_CLIENTS CONFIG_API_SERVER_MAX_WS_CLIENTS
/**
* @brief Initialize WebSocket handler
*
* @param server HTTP server handle
* @return esp_err_t ESP_OK on success
*/
esp_err_t websocket_handler_init(httpd_handle_t server);
/**
* @brief WebSocket request handler
*
* @param req HTTP request
* @return esp_err_t ESP_OK on success
*/
esp_err_t websocket_handler(httpd_req_t *req);
/**
* @brief Send message to a specific WebSocket client
*
* @param server HTTP server handle
* @param fd Socket file descriptor
* @param message Message to send
* @return esp_err_t ESP_OK on success
*/
esp_err_t websocket_send(httpd_handle_t server, int fd, const char *message);
/**
* @brief Broadcast message to all connected WebSocket clients
*
* @param server HTTP server handle
* @param message Message to broadcast
* @return esp_err_t ESP_OK on success
*/
esp_err_t websocket_broadcast(httpd_handle_t server, const char *message);
/**
* @brief Get number of connected WebSocket clients
*
* @return int Number of connected clients
*/
int websocket_get_client_count(void);
#ifdef __cplusplus
}
#endif