summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2021-06-04 12:46:15 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2021-06-10 17:00:01 +0200
commit1fdb7e6d1cf0709b1079b2d9990c0d53d8740461 (patch)
treee13f19f28e2ee379150171bad00fecec59168901 /src/rest.c
parent893101ffc6162d64cfbe5827c32d4e6de5e35f42 (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/rest.c')
-rw-r--r--src/rest.c76
1 files changed, 73 insertions, 3 deletions
diff --git a/src/rest.c b/src/rest.c
index feeb306..9c5d934 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -4076,9 +4076,72 @@ static int og_cmd_post_center_delete(json_t *element,
return 0;
}
+int og_procedure_add_steps(struct og_dbi *dbi, struct og_procedure *proc)
+{
+ struct og_procedure_step *step;
+ uint64_t procedure = 0;
+ const char *legacy_params;
+ const char *msglog;
+ dbi_result result;
+ int i;
+
+ for (i = 0; i < proc->num_steps; i++) {
+ step = &proc->steps[i];
+ switch (step->type) {
+ case OG_STEP_COMMAND:
+ legacy_params = og_msg_params_to_legacy(&step->cmd);
+ if (!legacy_params) {
+ og_dbi_close(dbi);
+ return -1;
+ }
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO procedimientos_acciones "
+ "(idprocedimiento, orden, parametros) "
+ "VALUES (%d, %d, '%s')",
+ procedure,
+ step->position,
+ legacy_params);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR,
+ "failed to add procedure command to database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ free((char *)legacy_params);
+ return -1;
+ }
+
+ dbi_result_free(result);
+ free((char *)legacy_params);
+ break;
+ case OG_STEP_PROCEDURE:
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO procedimientos_acciones "
+ "(idprocedimiento, orden, procedimientoid) "
+ "VALUES (%d, %d, %d)",
+ procedure,
+ step->position,
+ step->procedure.id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR,
+ "failed to add procedure child to database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ dbi_result_free(result);
+ break;
+ }
+ }
+
+ return 0;
+}
+
static int og_cmd_post_procedure_add(json_t *element,
struct og_msg_params *params)
{
+ struct og_procedure proc = {};
const char *key, *msglog;
struct og_dbi *dbi;
dbi_result result;
@@ -4092,8 +4155,11 @@ static int og_cmd_post_procedure_add(json_t *element,
} else if (!strcmp(key, "name")) {
err = og_json_parse_string(value, &params->name);
params->flags |= OG_REST_PARAM_NAME;
- } else if (!strcmp(key, "description"))
+ } else if (!strcmp(key, "description")) {
err = og_json_parse_string(value, &params->comment);
+ } else if (!strcmp(key, "steps")) {
+ err = og_json_parse_procedure(value, &proc);
+ }
if (err < 0)
return err;
@@ -4147,10 +4213,14 @@ static int og_cmd_post_procedure_add(json_t *element,
og_dbi_close(dbi);
return -1;
}
-
dbi_result_free(result);
+
+ proc.id = dbi_conn_sequence_last(dbi->conn, NULL);
+ err = og_procedure_add_steps(dbi, &proc);
+
og_dbi_close(dbi);
- return 0;
+
+ return err;
}
static int og_cmd_post_room_add(json_t *element,