add: macro endpoint for execution
This commit is contained in:
parent
01ffb1d58d
commit
d3fd48b35a
9 changed files with 110 additions and 26 deletions
src/endpoints
|
@ -86,11 +86,11 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin
|
|||
}
|
||||
}
|
||||
|
||||
macro_action_delete_for_macro(macro->id);
|
||||
|
||||
cJSON *json_actions = cJSON_GetObjectItemCaseSensitive(json, "actions");
|
||||
if(cJSON_IsArray(json_actions))
|
||||
{
|
||||
macro_action_delete_for_macro(macro->id);
|
||||
|
||||
cJSON *json_action;
|
||||
cJSON_ArrayForEach(json_action, json_actions)
|
||||
{
|
||||
|
|
64
src/endpoints/api_v1_macros_STR_execute.c
Normal file
64
src/endpoints/api_v1_macros_STR_execute.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_macros.h>
|
||||
#include <logger.h>
|
||||
#include <command.h>
|
||||
#include <models/macro_action.h>
|
||||
#include <models/macro.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
(void)nc;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(uuid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
M_RESPONSE_400_NO_VALID_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
macro_t* macro = macro_get_by_uid(target_uid);
|
||||
|
||||
if(!macro)
|
||||
{
|
||||
M_RESPONSE_404_NO_MACRO_FOUND_FOR_ID(response);
|
||||
return;
|
||||
}
|
||||
|
||||
macro_action_t** macro_actions = macro_action_get_for_macro(macro->id);
|
||||
|
||||
database_transaction_lock lock;
|
||||
database_transaction_begin(&lock);
|
||||
|
||||
for(int i = 0; macro_actions[i] != NULL; ++i)
|
||||
{
|
||||
macro_action_execute(macro_actions[i]);
|
||||
}
|
||||
|
||||
database_transaction_commit(&lock);
|
||||
|
||||
int *target_relay_ids = macro_get_target_relay_ids(macro->id);
|
||||
for(int i = 0; target_relay_ids[i] != 0; ++i)
|
||||
{
|
||||
relay_t *target_relay = relay_get_by_id(target_relay_ids[i]);
|
||||
if(!target_relay)
|
||||
{
|
||||
LOGGER_ERR("failed to load target relay from database\n");
|
||||
continue;
|
||||
}
|
||||
command_relay_schedules_set(target_relay);
|
||||
|
||||
relay_free(target_relay);
|
||||
}
|
||||
|
||||
free(target_relay_ids);
|
||||
macro_action_free_list(macro_actions);
|
||||
macro_free(macro);
|
||||
|
||||
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "macro got executed");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue