send current state via WS
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 6m9s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 4m54s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 4m35s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 4m59s
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 6m9s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 4m54s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 4m35s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 4m59s
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -2,12 +2,17 @@
|
||||
#include <cJSON.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "persistence_manager.h"
|
||||
|
||||
// Gibt ein cJSON-Objekt with dem aktuellen Lichtstatus zurück
|
||||
cJSON *create_light_status_json(void)
|
||||
{
|
||||
persistence_manager_t pm;
|
||||
persistence_manager_init(&pm, "config");
|
||||
cJSON *json = cJSON_CreateObject();
|
||||
// TODO: Echte Werte einfügen, aktuell Dummy-Daten
|
||||
cJSON_AddBoolToObject(json, "on", false);
|
||||
|
||||
bool light_active = persistence_manager_get_bool(&pm, "light_active", false);
|
||||
cJSON_AddBoolToObject(json, "on", light_active);
|
||||
cJSON_AddBoolToObject(json, "thunder", false);
|
||||
cJSON_AddStringToObject(json, "mode", "day");
|
||||
cJSON_AddStringToObject(json, "schema", "schema_03.csv");
|
||||
@@ -16,5 +21,8 @@ cJSON *create_light_status_json(void)
|
||||
cJSON_AddNumberToObject(color, "g", 240);
|
||||
cJSON_AddNumberToObject(color, "b", 220);
|
||||
cJSON_AddItemToObject(json, "color", color);
|
||||
|
||||
persistence_manager_deinit(&pm);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "websocket_handler.h"
|
||||
#include "api_server.h"
|
||||
#include "common.h"
|
||||
|
||||
#include "message_manager.h"
|
||||
#include <esp_http_server.h>
|
||||
#include <esp_log.h>
|
||||
#include <string.h>
|
||||
@@ -11,6 +13,16 @@ static const char *TAG = "websocket_handler";
|
||||
static int ws_clients[WS_MAX_CLIENTS];
|
||||
static int ws_client_count = 0;
|
||||
|
||||
static void on_message_received(const message_t *msg)
|
||||
{
|
||||
cJSON *json = create_light_status_json();
|
||||
cJSON_AddStringToObject(json, "type", "status");
|
||||
char *response = cJSON_PrintUnformatted(json);
|
||||
cJSON_Delete(json);
|
||||
api_server_ws_broadcast(response);
|
||||
free(response);
|
||||
}
|
||||
|
||||
static void ws_clients_init(void)
|
||||
{
|
||||
for (int i = 0; i < WS_MAX_CLIENTS; i++)
|
||||
@@ -179,6 +191,8 @@ static void ws_async_send(void *arg)
|
||||
|
||||
esp_err_t websocket_handler_init(httpd_handle_t server)
|
||||
{
|
||||
message_manager_register_listener(on_message_received);
|
||||
|
||||
ws_clients_init();
|
||||
// Register WebSocket URI handler
|
||||
httpd_uri_t ws_uri = {.uri = "/ws",
|
||||
|
||||
Reference in New Issue
Block a user