Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav committed Feb 2, 2024
1 parent 178b9ce commit 3669fad
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
6 changes: 4 additions & 2 deletions components/protocols/src/http_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/script/include/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
11 changes: 10 additions & 1 deletion components/script/src/l_evse_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
104 changes: 102 additions & 2 deletions components/script/src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 3669fad

Please sign in to comment.