diff --git a/src/cmnds/cmd_tasmota.c b/src/cmnds/cmd_tasmota.c index 39ef90b71..ebe7565ff 100644 --- a/src/cmnds/cmd_tasmota.c +++ b/src/cmnds/cmd_tasmota.c @@ -6,6 +6,7 @@ #include "cmd_local.h" #include "../new_pins.h" #include "../new_cfg.h" +#include "../cJSON/cJSON.h" #if ENABLE_LITTLEFS #include "../littlefs/our_lfs.h" #endif @@ -20,6 +21,37 @@ int parsePowerArgument(const char *s) { return atoi(s); } + +static commandResult_t cmnd_JsonCommand(const void *context, const char *cmd, const char *args, int cmdFlags) { + cJSON *root = cJSON_Parse(args); + if(!root) { + ADDLOG_ERROR(LOG_FEATURE_CMD, "Invalid JSON input: %s", args); + return CMD_RES_BAD_ARGUMENT; + } + + cJSON *element = root->child; + while(element) { + if (cJSON_IsString(element)) { + char buffer[256]; + snprintf(buffer, sizeof(buffer), "%s %s", element->string, element->valuestring); + CMD_ExecuteCommand(buffer, cmdFlags); + } + else if (cJSON_IsNumber(element)) { + char buffer[256]; + if (element->valuedouble == (int)element->valuedouble) { + snprintf(buffer, sizeof(buffer), "%s %d", element->string, (int)element->valuedouble); + } else { + snprintf(buffer, sizeof(buffer), "%s %.2f", element->string, element->valuedouble); + } + CMD_ExecuteCommand(buffer, cmdFlags); + } + element = element->next; + } + + cJSON_Delete(root); + return CMD_RES_OK; +} + static commandResult_t power(const void *context, const char *cmd, const char *args, int cmdFlags){ //if (!wal_strnicmp(cmd, "POWER", 5)){ int channel = 0; @@ -474,5 +506,10 @@ int taslike_commands_init(){ //cmddetail:"fn":"cmnd_stub","file":"cmnds/cmd_tasmota.c","requires":"", //cmddetail:"examples":""} CMD_RegisterCommand("Result", cmnd_stub, NULL); + //cmddetail:{"name":"Json","args":"json array of commands", + //cmddetail:"descr":"A stub for Tasmota", + //cmddetail:"fn":"cmnd_JsonCommand","file":"cmnds/cmd_tasmota.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("Json", cmnd_JsonCommand, NULL); return 0; }