change light mode
day/night/simulation Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -309,7 +309,37 @@ esp_err_t api_light_mode_handler(httpd_req_t *req)
|
|||||||
|
|
||||||
ESP_LOGI(TAG, "Received light mode: %s", buf);
|
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);
|
set_cors_headers(req);
|
||||||
return httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
return httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,21 @@ cJSON *create_light_status_json(void)
|
|||||||
|
|
||||||
bool light_active = persistence_manager_get_bool(&pm, "light_active", false);
|
bool light_active = persistence_manager_get_bool(&pm, "light_active", false);
|
||||||
cJSON_AddBoolToObject(json, "on", light_active);
|
cJSON_AddBoolToObject(json, "on", light_active);
|
||||||
|
|
||||||
cJSON_AddBoolToObject(json, "thunder", false);
|
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_AddStringToObject(json, "schema", "schema_03.csv");
|
||||||
cJSON *color = cJSON_CreateObject();
|
cJSON *color = cJSON_CreateObject();
|
||||||
cJSON_AddNumberToObject(color, "r", 255);
|
cJSON_AddNumberToObject(color, "r", 255);
|
||||||
|
|||||||
@@ -206,6 +206,8 @@ class Menu : public Widget
|
|||||||
*/
|
*/
|
||||||
MenuItem switchValue(const MenuItem &menuItem, ButtonType button);
|
MenuItem switchValue(const MenuItem &menuItem, ButtonType button);
|
||||||
|
|
||||||
|
void setSelectionIndex(const MenuItem &menuItem, int index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MenuItem replaceItem(int index, const MenuItem &item);
|
MenuItem replaceItem(int index, const MenuItem &item);
|
||||||
|
|
||||||
|
|||||||
@@ -126,6 +126,15 @@ MenuItem Menu::switchValue(const MenuItem &menuItem, ButtonType button)
|
|||||||
return result;
|
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)
|
MenuItem Menu::replaceItem(const int index, const MenuItem &item)
|
||||||
{
|
{
|
||||||
m_items.at(index) = item;
|
m_items.at(index) = item;
|
||||||
|
|||||||
@@ -91,11 +91,14 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
|
|||||||
const auto value = getItem(item.getId()).getIndex();
|
const auto value = getItem(item.getId()).getIndex();
|
||||||
if (m_options && m_options->persistenceManager)
|
if (m_options && m_options->persistenceManager)
|
||||||
{
|
{
|
||||||
persistence_manager_set_int(m_options->persistenceManager, LightMenuOptions::LIGHT_MODE, value);
|
// Post change via message_manager
|
||||||
persistence_manager_save(m_options->persistenceManager);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -108,11 +111,14 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
|
|||||||
const auto value = getItem(item.getId()).getIndex() + 1;
|
const auto value = getItem(item.getId()).getIndex() + 1;
|
||||||
if (m_options && m_options->persistenceManager)
|
if (m_options && m_options->persistenceManager)
|
||||||
{
|
{
|
||||||
persistence_manager_set_int(m_options->persistenceManager, LightMenuOptions::LIGHT_VARIANT, value);
|
// Post change via message_manager
|
||||||
persistence_manager_save(m_options->persistenceManager);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -133,10 +139,20 @@ void LightMenu::onMessageReceived(const message_t *msg)
|
|||||||
{
|
{
|
||||||
// Here you can react to messages, e.g. set toggle status
|
// Here you can react to messages, e.g. set toggle status
|
||||||
// Example: If light_active was changed, synchronize toggle
|
// Example: If light_active was changed, synchronize toggle
|
||||||
if (msg && msg->type == MESSAGE_TYPE_SETTINGS && msg->data.settings.type == SETTINGS_TYPE_BOOL &&
|
if (msg && msg->type == MESSAGE_TYPE_SETTINGS)
|
||||||
std::strcmp(msg->data.settings.key, "light_active") == 0)
|
|
||||||
{
|
{
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -130,8 +130,10 @@ static void init_ui(void)
|
|||||||
|
|
||||||
static void on_message_received(const message_t *msg)
|
static void on_message_received(const message_t *msg)
|
||||||
{
|
{
|
||||||
if (msg && msg->type == MESSAGE_TYPE_SETTINGS && msg->data.settings.type == SETTINGS_TYPE_BOOL &&
|
if (msg && msg->type == MESSAGE_TYPE_SETTINGS &&
|
||||||
std::strcmp(msg->data.settings.key, "light_active") == 0)
|
(std::strcmp(msg->data.settings.key, "light_active") == 0 ||
|
||||||
|
std::strcmp(msg->data.settings.key, "light_variant") == 0 ||
|
||||||
|
std::strcmp(msg->data.settings.key, "light_mode") == 0))
|
||||||
{
|
{
|
||||||
start_simulation();
|
start_simulation();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user