add: macro endpoints

add: basic macro support
fix: database locking with lock-pointer
fix: memory leaks
This commit is contained in:
Tobias Reisinger 2020-09-04 00:28:49 +02:00
parent 6a2b94ef1c
commit 9d2c48d645
30 changed files with 606 additions and 213 deletions
src/endpoints

View file

@ -0,0 +1,257 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <database.h>
#include <endpoints/api_v1_macros.h>
#include <logger.h>
#include <models/macro.h>
#include <models/macro_action.h>
#include <models/schedule.h>
#include <models/relay.h>
#include <models/controller.h>
void
api_v1_macros_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
(void)nc;
macro_t** all_macros = macro_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_macros[i] != NULL; ++i)
{
cJSON_AddItemToArray(json, macro_to_json(all_macros[i]));
free(all_macros[i]);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
free(all_macros);
}
void
api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)nc;
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
if(json == NULL)
{
static const char content[] = "no valid json was supplied";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
{
LOGGER_DEBUG("no name for macro provided\n");
cJSON_Delete(json);
static const char content[] = "no name for macro provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_actions = cJSON_GetObjectItemCaseSensitive(json, "actions");
if(!cJSON_IsArray(json_actions))
{
LOGGER_DEBUG("no actions for macro provided\n");
cJSON_Delete(json);
static const char content[] = "no actions for macro provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
database_transaction_lock lock;
database_transaction_begin(&lock);
macro_t *new_macro = malloc(sizeof(macro_t));
new_macro->id = 0;
uuid_generate(new_macro->uid);
strncpy(new_macro->name, json_name->valuestring, MAX_NAME_LENGTH);
new_macro->name[MAX_NAME_LENGTH] = '\0';
if(macro_save(new_macro))
{
LOGGER_DEBUG("macro could not be saved\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "macro could not be saved";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
cJSON *json_action;
cJSON_ArrayForEach(json_action, json_actions)
{
cJSON *json_action_weekday = cJSON_GetObjectItemCaseSensitive(json_action, "weekday");
if(!cJSON_IsNumber(json_action_weekday) || (json_action_weekday->valueint < 0) || (json_action_weekday->valueint > 6))
{
LOGGER_DEBUG("one action is missing a weekday\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing a weekday";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_action_schedule = cJSON_GetObjectItemCaseSensitive(json_action, "schedule");
if(!cJSON_IsObject(json_action_schedule))
{
LOGGER_DEBUG("action is missing schedule\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing schedule";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_action_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_action_schedule, "id");
if(!cJSON_IsString(json_action_schedule_uid) || (json_action_schedule_uid->valuestring == NULL))
{
LOGGER_DEBUG("action is missing schedule id\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing schedule id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
uuid_t action_schedule_uid;
if(schedule_uid_parse(json_action_schedule_uid->valuestring, action_schedule_uid))
{
LOGGER_DEBUG("action schedule has bad uid\n");
cJSON_Delete(json);
static const char content[] = "action schedule has bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
schedule_t *action_schedule = schedule_get_by_uid(action_schedule_uid);
if(action_schedule == NULL)
{
LOGGER_DEBUG("action schedule was not found\n");
cJSON_Delete(json);
static const char content[] = "action schedule was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
int action_schedule_id = action_schedule->id;
schedule_free(action_schedule);
cJSON *json_action_relay = cJSON_GetObjectItemCaseSensitive(json_action, "relay");
if(!cJSON_IsObject(json_action_relay))
{
LOGGER_DEBUG("action is missing relay\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_action_relay_number = cJSON_GetObjectItemCaseSensitive(json_action_relay, "number");
if(!cJSON_IsNumber(json_action_relay_number))
{
LOGGER_DEBUG("action is missing relay number\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay number";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_action_relay_controller_uid = cJSON_GetObjectItemCaseSensitive(json_action_relay, "controller_id");
if(!cJSON_IsString(json_action_relay_controller_uid) || (json_action_relay_controller_uid->valuestring == NULL))
{
LOGGER_DEBUG("action is missing relay controller id\n");
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay controller id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
uuid_t action_controller_uid;
if(uuid_parse(json_action_relay_controller_uid->valuestring, action_controller_uid))
{
LOGGER_DEBUG("action controller has bad uid\n");
cJSON_Delete(json);
static const char content[] = "action controller has bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t *action_controller = controller_get_by_uid(action_controller_uid);
if(action_controller == NULL)
{
LOGGER_DEBUG("action controller was not found\n");
cJSON_Delete(json);
static const char content[] = "action controller was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
int controller_id = action_controller->id;
int relay_num = json_action_relay_number->valueint;
controller_free(action_controller);
relay_t *action_relay = relay_get_for_controller(controller_id, relay_num);
if(action_relay == NULL)
{
LOGGER_DEBUG("action relay was not found\n");
cJSON_Delete(json);
static const char content[] = "action relay was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
int action_relay_id = action_relay->id;
relay_free(action_relay);
macro_action_t *new_action = malloc(sizeof(macro_action_t));
new_action->macro_id = new_macro->id;
new_action->relay_id = action_relay_id;
new_action->schedule_id = action_schedule_id;
new_action->weekday = json_action_weekday->valueint;
macro_action_insert(new_action);
free(new_action);
}
database_transaction_commit(&lock);
cJSON_Delete(json);
json = macro_to_json(new_macro);
endpoint_response_json(response, 201, json);
cJSON_Delete(json);
macro_free(new_macro);
}