change light mode

day/night/simulation

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2026-01-18 16:09:09 +01:00
parent ccdc2bb63f
commit 3f32b791b7
6 changed files with 87 additions and 15 deletions

View File

@@ -309,7 +309,37 @@ esp_err_t api_light_mode_handler(httpd_req_t *req)
ESP_LOGI(TAG, "Received light mode: %s", buf);
// TODO: Parse JSON and set light mode
cJSON *json = cJSON_Parse(buf);
if (json)
{
cJSON *mode = cJSON_GetObjectItem(json, "mode");
if (cJSON_IsString(mode))
{
message_t msg = {};
msg.type = MESSAGE_TYPE_SETTINGS;
msg.data.settings.type = SETTINGS_TYPE_INT;
strncpy(msg.data.settings.key, "light_mode", sizeof(msg.data.settings.key) - 1);
if (strcmp(mode->valuestring, "simulation") == 0)
{
msg.data.settings.value.int_value = 0;
}
else if (strcmp(mode->valuestring, "day") == 0)
{
msg.data.settings.value.int_value = 1;
}
else if (strcmp(mode->valuestring, "night") == 0)
{
msg.data.settings.value.int_value = 2;
}
else
{
msg.data.settings.value.int_value = -1; // Unbekannter Modus
}
message_manager_post(&msg, pdMS_TO_TICKS(100));
}
cJSON_Delete(json);
}
set_cors_headers(req);
return httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
}

View File

@@ -13,8 +13,21 @@ cJSON *create_light_status_json(void)
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");
int mode = persistence_manager_get_int(&pm, "light_mode", 0);
const char *mode_str = "simulation";
if (mode == 1)
{
mode_str = "day";
}
else if (mode == 2)
{
mode_str = "night";
}
cJSON_AddStringToObject(json, "mode", mode_str);
cJSON_AddStringToObject(json, "schema", "schema_03.csv");
cJSON *color = cJSON_CreateObject();
cJSON_AddNumberToObject(color, "r", 255);

View File

@@ -206,6 +206,8 @@ class Menu : public Widget
*/
MenuItem switchValue(const MenuItem &menuItem, ButtonType button);
void setSelectionIndex(const MenuItem &menuItem, int index);
private:
MenuItem replaceItem(int index, const MenuItem &item);

View File

@@ -126,6 +126,15 @@ MenuItem Menu::switchValue(const MenuItem &menuItem, ButtonType button)
return result;
}
void Menu::setSelectionIndex(const MenuItem &menuItem, int index)
{
if (index >= 0 && index < menuItem.getItemCount())
{
auto item = menuItem.copyWith(index);
replaceItem(menuItem.getId(), item);
}
}
MenuItem Menu::replaceItem(const int index, const MenuItem &item)
{
m_items.at(index) = item;

View File

@@ -91,11 +91,14 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
const auto value = getItem(item.getId()).getIndex();
if (m_options && m_options->persistenceManager)
{
persistence_manager_set_int(m_options->persistenceManager, LightMenuOptions::LIGHT_MODE, value);
persistence_manager_save(m_options->persistenceManager);
// Post change via message_manager
message_t msg = {};
msg.type = MESSAGE_TYPE_SETTINGS;
msg.data.settings.type = SETTINGS_TYPE_INT;
strncpy(msg.data.settings.key, LightMenuOptions::LIGHT_MODE, sizeof(msg.data.settings.key) - 1);
msg.data.settings.value.int_value = value;
message_manager_post(&msg, pdMS_TO_TICKS(100));
}
start_simulation();
}
break;
}
@@ -108,11 +111,14 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
const auto value = getItem(item.getId()).getIndex() + 1;
if (m_options && m_options->persistenceManager)
{
persistence_manager_set_int(m_options->persistenceManager, LightMenuOptions::LIGHT_VARIANT, value);
persistence_manager_save(m_options->persistenceManager);
// Post change via message_manager
message_t msg = {};
msg.type = MESSAGE_TYPE_SETTINGS;
msg.data.settings.type = SETTINGS_TYPE_INT;
strncpy(msg.data.settings.key, LightMenuOptions::LIGHT_VARIANT, sizeof(msg.data.settings.key) - 1);
msg.data.settings.value.int_value = value;
message_manager_post(&msg, pdMS_TO_TICKS(100));
}
start_simulation();
}
break;
}
@@ -133,10 +139,20 @@ void LightMenu::onMessageReceived(const message_t *msg)
{
// Here you can react to messages, e.g. set toggle status
// Example: If light_active was changed, synchronize toggle
if (msg && msg->type == MESSAGE_TYPE_SETTINGS && msg->data.settings.type == SETTINGS_TYPE_BOOL &&
std::strcmp(msg->data.settings.key, "light_active") == 0)
if (msg && msg->type == MESSAGE_TYPE_SETTINGS)
{
setToggle(getItem(LightMenuItem::ACTIVATE), msg->data.settings.value.bool_value);
if (std::strcmp(msg->data.settings.key, LightMenuOptions::LIGHT_ACTIVE) == 0)
{
setToggle(getItem(LightMenuItem::ACTIVATE), msg->data.settings.value.bool_value);
}
if (std::strcmp(msg->data.settings.key, LightMenuOptions::LIGHT_MODE) == 0)
{
setSelectionIndex(getItem(LightMenuItem::MODE), msg->data.settings.value.int_value);
}
if (std::strcmp(msg->data.settings.key, LightMenuOptions::LIGHT_VARIANT) == 0)
{
setSelectionIndex(getItem(LightMenuItem::VARIANT), msg->data.settings.value.int_value - 1);
}
}
}