diff options
author | Javier Sánchez Parra <jsanchez@soleta.eu> | 2021-06-04 12:46:15 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2021-06-10 17:00:01 +0200 |
commit | 1fdb7e6d1cf0709b1079b2d9990c0d53d8740461 (patch) | |
tree | e13f19f28e2ee379150171bad00fecec59168901 /src/json.c | |
parent | 893101ffc6162d64cfbe5827c32d4e6de5e35f42 (diff) |
#915 Add commands and procedures to procedure creation
Adds the possibility to create a procedure with commands and other
procedures integrated as steps.
Note: "steps" parameter is optional and "steps" array object order
defines execution order.
Request:
POST /procedure/add
{
"center": "1",
"name": "procedure",
"description": "My procedure",
"steps": [
{
"command": "wol",
"params": { "type": "broadcast" }
},
{
"procedure": 22
},
{
"command": "poweroff",
"params": {}
}
]
}
Response:
200 OK
This commit also updates unit tests for /procedure/add POST method to
include steps.
Diffstat (limited to 'src/json.c')
-rw-r--r-- | src/json.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -140,3 +140,75 @@ int og_json_parse_partition(json_t *element, struct og_partition *part, return err; } + +static int og_json_parse_procedure_cmd(json_t *element, int position, + struct og_procedure *proc) +{ + struct og_procedure_step *step; + uint32_t err = 0; + const char *key; + json_t *value; + + step = &proc->steps[proc->num_steps++]; + step->type = OG_STEP_COMMAND; + step->position = position; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "command")) + err = og_json_parse_string(value, &step->cmd.type); + else if (!strcmp(key, "params")) + step->cmd.json = value; + else + return -1; + } + + return err; +} + +static int og_json_parse_procedure_call(json_t *element, int position, + struct og_procedure *proc) +{ + struct og_procedure_step *step; + uint32_t err = 0; + const char *key; + json_t *value; + + step = &proc->steps[proc->num_steps++]; + step->type = OG_STEP_PROCEDURE; + step->position = position; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "procedure")) + err = og_json_parse_uint64(value, &step->procedure.id); + else + return -1; + } + + return err; +} + +int og_json_parse_procedure(json_t *element, struct og_procedure *proc) +{ + unsigned int i; + json_t *item; + int err = 0; + + if (json_typeof(element) != JSON_ARRAY) + return -1; + + for (i = 0; i < json_array_size(element) && i < OG_PROCEDURE_STEPS_MAX; ++i) { + item = json_array_get(element, i); + + if (json_object_get(item, "command")) + err = og_json_parse_procedure_cmd(item, i, proc); + else if (json_object_get(item, "procedure")) + err = og_json_parse_procedure_call(item, i, proc); + else + err = -1; + + if (err) + break; + } + + return err; +} |