From 3669fad5180698914c9b5a704adf2d17a115f3cc Mon Sep 17 00:00:00 2001 From: Miroslav Dzurik Date: Fri, 2 Feb 2024 15:49:20 +0100 Subject: [PATCH] stuff --- components/protocols/src/http_json.c | 6 +- components/script/include/script.h | 2 +- components/script/src/l_evse_lib.c | 11 ++- components/script/src/script.c | 104 ++++++++++++++++++++++++++- 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/components/protocols/src/http_json.c b/components/protocols/src/http_json.c index fdb9b94..0a85f63 100644 --- a/components/protocols/src/http_json.c +++ b/components/protocols/src/http_json.c @@ -324,7 +324,9 @@ cJSON* http_json_get_script_drivers_config(void) uint8_t count = script_get_driver_count(); - ESP_LOGI("SCR", "driver count %d", count); + for (uint8_t i = 0; i < count; i++) { + cJSON_AddItemToArray(json, script_read_driver_config(i)); + } return json; } @@ -415,7 +417,7 @@ esp_err_t http_json_set_time(cJSON* json) gettimeofday(&tv, NULL); tv.tv_sec = cJSON_GetNumberValue(json); settimeofday(&tv, NULL); - + scheduler_execute_schedules(); } else { return ESP_ERR_INVALID_ARG; diff --git a/components/script/include/script.h b/components/script/include/script.h index a36acca..7a0e349 100644 --- a/components/script/include/script.h +++ b/components/script/include/script.h @@ -51,7 +51,7 @@ bool script_output_read(uint16_t *index, char **str, uint16_t* len); uint8_t script_get_driver_count(void); -cJSON* script_read_config(uint8_t index); +cJSON* script_read_driver_config(uint8_t index); #endif /* SCRIPT_H_ */ diff --git a/components/script/src/l_evse_lib.c b/components/script/src/l_evse_lib.c index 6c09095..b21a576 100644 --- a/components/script/src/l_evse_lib.c +++ b/components/script/src/l_evse_lib.c @@ -224,9 +224,18 @@ uint8_t l_evse_get_driver_count(lua_State* L) return len; } -void l_evse_get_driver(lua_State* L, uint8_t index); +void l_evse_get_driver(lua_State* L, uint8_t index) { + lua_rawgeti(L, LUA_REGISTRYINDEX, userdata_ref); + evse_userdata_t* userdata = lua_touserdata(L, -1); + + lua_rawgeti(L, LUA_REGISTRYINDEX, userdata->drivers_ref); + + lua_rawgeti(L, -1, index + 1); + + lua_remove(L, -2); + lua_remove(L, -2); } static const luaL_Reg lib[] = { diff --git a/components/script/src/script.c b/components/script/src/script.c index 0f7b4d5..7c38a53 100644 --- a/components/script/src/script.c +++ b/components/script/src/script.c @@ -204,21 +204,121 @@ uint8_t script_get_driver_count(void) { xSemaphoreTake(script_mutex, portMAX_DELAY); - uint8_t count = l_evse_get_driver_count(L); + uint8_t count = l_evse_get_driver_count(L); xSemaphoreGive(script_mutex); return count; } +static cJSON* l_read_config_key(int table_idx, int config_key_idx) +{ + if (lua_istable(L, config_key_idx)) { + lua_getfield(L, config_key_idx, "type"); + const char* type = lua_tostring(L, -1); + lua_pop(L, 1); + + if (strcmp("string", type) != 0 && strcmp("number", type) != 0 && strcmp("boolean", type) != 0) { + return NULL; // unknown type + } + + lua_getfield(L, config_key_idx, "key"); + const char* key = lua_tostring(L, -1); + lua_pop(L, 1); + + lua_getfield(L, config_key_idx, "name"); + const char* name = lua_tostring(L, -1); + lua_pop(L, 1); + + cJSON* value_json = NULL; + lua_getfield(L, table_idx, key); + if (lua_isnil(L, -1)) { + value_json = cJSON_CreateNull(); + } else { + if (strcmp("string", type) == 0) { + value_json = cJSON_CreateString(lua_tostring(L, -1)); + } else if (strcmp("number", type) == 0) { + value_json = cJSON_CreateNumber(lua_tonumber(L, -1)); + } else { + value_json = cJSON_CreateBool(lua_toboolean(L, -1)); + } + } + lua_pop(L, 1); + + cJSON* json = cJSON_CreateObject(); + cJSON_AddStringToObject(json, "key", key); + cJSON_AddStringToObject(json, "type", type); + cJSON_AddStringToObject(json, "name", name); + cJSON_AddItemToObject(json, "value", value_json); + + return json; + } + + return NULL; +} + +static cJSON* l_read_config_keys(int table_idx, int config_idx) +{ + cJSON* json = cJSON_CreateArray(); + + int count = lua_rawlen(L, config_idx); + for (int i = 1; i <= count; i++) { + lua_rawgeti(L, config_idx, i); + + cJSON* item_json = l_read_config_key(table_idx - 1, -1); + if (item_json != NULL) { + cJSON_AddItemToArray(json, item_json); + } + + lua_pop(L, 1); + } + + return json; +} + cJSON* script_read_driver_config(uint8_t index) { + cJSON* json = cJSON_CreateObject(); + xSemaphoreTake(script_mutex, portMAX_DELAY); l_evse_get_driver(L, index); + lua_getfield(L, -1, "name"); + if (lua_isstring(L, -1)) { + cJSON_AddStringToObject(json, "name", lua_tostring(L, -1)); + } + lua_pop(L, 1); + + lua_getfield(L, -1, "readconfig"); + + if (lua_isfunction(L, -1)) { + if (lua_pcall(L, 0, 1, 0) == LUA_OK) { + if (lua_istable(L, -1)) { + lua_getfield(L, -2, "config"); + + cJSON_AddItemToObject(json, "config", l_read_config_keys(-2, -1)); + + lua_pop(L, 1); + } else { + const char* err = "readconfig not return table"; + lua_writestring(err, strlen(err)); + lua_writeline(); + } + lua_pop(L, 1); + } else { + const char* err = lua_tostring(L, -1); + lua_writestring(err, strlen(err)); + lua_writeline(); + lua_pop(L, 1); + } + } else { + lua_pop(L, 1); + } + + lua_pop(L, 1); xSemaphoreGive(script_mutex); - return NULL; + return json; } \ No newline at end of file