2020-05-05 09:42:02 +00:00
|
|
|
#include <cJSON.h>
|
2020-05-20 23:33:18 +00:00
|
|
|
#include <macros.h>
|
2020-05-05 20:29:04 +00:00
|
|
|
#include <constants.h>
|
2020-05-05 09:42:02 +00:00
|
|
|
#include <endpoints/api_v1_schedules.h>
|
|
|
|
#include <logger.h>
|
2020-05-11 12:50:25 +00:00
|
|
|
#include <command.h>
|
2020-05-05 09:42:02 +00:00
|
|
|
#include <models/junction_tag.h>
|
2020-05-11 12:50:25 +00:00
|
|
|
#include <models/junction_relay_schedule.h>
|
2020-05-05 09:42:02 +00:00
|
|
|
#include <models/schedule.h>
|
2020-05-11 12:50:25 +00:00
|
|
|
#include <models/relay.h>
|
2020-05-05 09:42:02 +00:00
|
|
|
#include <models/tag.h>
|
|
|
|
|
|
|
|
void
|
2020-05-20 23:33:18 +00:00
|
|
|
api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-05 09:42:02 +00:00
|
|
|
{
|
|
|
|
(void)hm;
|
|
|
|
|
|
|
|
uuid_t target_uid;
|
|
|
|
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("failed to unparse uid\n");
|
|
|
|
|
|
|
|
static const char content[] = "given id was invalid";
|
|
|
|
response->status_code = 400;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 09:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
if(!schedule)
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
|
|
|
|
|
|
|
static const char content[] = "no schedule for id found";
|
|
|
|
response->status_code = 404;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *json = schedule_to_json(schedule);
|
|
|
|
|
|
|
|
char *json_str = cJSON_Print(json);
|
|
|
|
if (json_str == NULL)
|
|
|
|
{
|
|
|
|
LOG_ERROR("failed to print schedules json\n");
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
static const char content[] = "failed to print json for schedules";
|
|
|
|
response->status_code = 500;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
response->status_code = 200;
|
|
|
|
response->content_type = "application/json";
|
|
|
|
response->content_length = strlen(json_str);
|
|
|
|
response->content = json_str;
|
|
|
|
response->alloced_content = true;
|
2020-05-05 20:29:04 +00:00
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
|
|
schedule_free(schedule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-20 23:33:18 +00:00
|
|
|
api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-05 20:29:04 +00:00
|
|
|
{
|
|
|
|
(void)hm;
|
|
|
|
|
|
|
|
uuid_t target_uid;
|
|
|
|
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("failed to unparse uid\n");
|
|
|
|
|
|
|
|
static const char content[] = "given id was invalid";
|
|
|
|
response->status_code = 400;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
|
|
|
|
|
|
|
if(!schedule)
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
|
|
|
|
|
|
|
static const char content[] = "no schedule for id found";
|
|
|
|
response->status_code = 404;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
|
|
|
|
|
|
|
if(json == NULL)
|
|
|
|
{
|
|
|
|
const char *error_ptr = cJSON_GetErrorPtr();
|
|
|
|
if (error_ptr != NULL)
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("error before: %s\n", error_ptr);
|
2020-05-05 20:29:04 +00:00
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
static const char content[] = "no valid json was supplied";
|
|
|
|
response->status_code = 400;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-06 23:38:13 +00:00
|
|
|
return;
|
2020-05-05 20:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
|
|
|
if(cJSON_IsString(json_name) && json_name->valuestring)
|
|
|
|
{
|
|
|
|
strncpy(schedule->name, json_name->valuestring, MAX_NAME_LENGTH);
|
|
|
|
schedule->name[MAX_NAME_LENGTH] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!schedule_is_protected(schedule))
|
|
|
|
{
|
|
|
|
cJSON *json_period;
|
|
|
|
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
|
|
|
|
|
|
|
|
int periods_count = cJSON_GetArraySize(json_periods);
|
|
|
|
free(schedule->periods);
|
|
|
|
schedule->periods = malloc(sizeof(period_t) * periods_count);
|
|
|
|
|
|
|
|
int periods_valid = 0;
|
|
|
|
|
|
|
|
cJSON_ArrayForEach(json_period, json_periods)
|
|
|
|
{
|
|
|
|
cJSON *json_period_start = cJSON_GetObjectItemCaseSensitive(json_period, "start");
|
|
|
|
cJSON *json_period_end = cJSON_GetObjectItemCaseSensitive(json_period, "end");
|
|
|
|
|
|
|
|
if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("period is missing start\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("period is missing end\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t start;
|
|
|
|
uint16_t end;
|
|
|
|
if(period_helper_parse_hhmm(json_period_start->valuestring, &start))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
schedule->periods[periods_valid].start = start;
|
|
|
|
schedule->periods[periods_valid].end = end;
|
|
|
|
++periods_valid;
|
|
|
|
}
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
schedule->periods_count = periods_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(schedule_save(schedule))
|
2020-05-05 09:42:02 +00:00
|
|
|
{
|
2020-05-05 20:29:04 +00:00
|
|
|
LOG_ERROR("failed to save schedule\n");
|
|
|
|
free(schedule);
|
|
|
|
cJSON_Delete(json);
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
static const char content[] = "failed to save schedule to database";
|
|
|
|
response->status_code = 500;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-05 09:42:02 +00:00
|
|
|
|
2020-05-19 22:51:16 +00:00
|
|
|
relay_t **relays = relay_get_with_schedule(schedule->id);
|
|
|
|
for(int i = 0; relays[i] != NULL; ++i)
|
2020-05-11 12:50:25 +00:00
|
|
|
{
|
2020-05-19 22:51:16 +00:00
|
|
|
command_set_relay_schedule(relays[i]);
|
2020-05-11 12:50:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
cJSON *json_tag;
|
|
|
|
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
|
2020-05-06 20:49:22 +00:00
|
|
|
if(cJSON_IsArray(json_tags))
|
|
|
|
{
|
|
|
|
junction_tag_remove_for_schedule(schedule->id);
|
|
|
|
}
|
2020-05-05 20:29:04 +00:00
|
|
|
cJSON_ArrayForEach(json_tag, json_tags)
|
|
|
|
{
|
|
|
|
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
|
|
|
|
{
|
|
|
|
LOG_DEBUG("invalid tag in tags\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char *tag = json_tag->valuestring;
|
|
|
|
int tag_id = tag_get_id(tag);
|
|
|
|
if(tag_id == 0)
|
|
|
|
{
|
|
|
|
tag_save(tag_id, tag);
|
|
|
|
tag_id = tag_get_id(tag);
|
|
|
|
}
|
|
|
|
junction_tag_insert(tag_id, 0, schedule->id);
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
cJSON_Delete(json);
|
|
|
|
json = schedule_to_json(schedule);
|
|
|
|
|
2020-05-05 09:42:02 +00:00
|
|
|
char *json_str = cJSON_Print(json);
|
|
|
|
if (json_str == NULL)
|
|
|
|
{
|
2020-05-05 20:29:04 +00:00
|
|
|
LOG_ERROR("failed to print schedule json\n");
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
static const char content[] = "failed to print json for schedule";
|
|
|
|
response->status_code = 500;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
response->status_code = 200;
|
|
|
|
response->content_type = "application/json";
|
|
|
|
response->content_length = strlen(json_str);
|
|
|
|
response->content = json_str;
|
|
|
|
response->alloced_content = true;
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
2020-05-05 20:29:04 +00:00
|
|
|
schedule_free(schedule);
|
2020-05-05 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 20:29:04 +00:00
|
|
|
void
|
2020-05-20 23:33:18 +00:00
|
|
|
api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-05 20:29:04 +00:00
|
|
|
{
|
|
|
|
(void)hm;
|
|
|
|
|
2020-05-06 08:53:42 +00:00
|
|
|
const char *target_uid_str = args[0].value.v_str;
|
2020-05-05 20:29:04 +00:00
|
|
|
|
|
|
|
uuid_t target_uid;
|
|
|
|
if(schedule_uid_parse(target_uid_str, target_uid))
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("failed to unparse uid\n");
|
|
|
|
|
|
|
|
static const char content[] = "given id was invalid";
|
|
|
|
response->status_code = 400;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
|
|
|
|
2020-05-20 23:33:18 +00:00
|
|
|
if(!schedule)
|
2020-05-05 20:29:04 +00:00
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
|
|
|
|
|
|
|
static const char content[] = "no schedule for id found";
|
|
|
|
response->status_code = 404;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:33:18 +00:00
|
|
|
if(schedule_is_protected(schedule))
|
2020-05-05 20:29:04 +00:00
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
static const char content[] = "target schedule is protected";
|
|
|
|
response->status_code = 403;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(schedule_remove(schedule))
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_ERROR("failed to remove schedule from database\n");
|
|
|
|
|
|
|
|
static const char content[] = "failed to remove schedule from database";
|
|
|
|
response->status_code = 500;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response->status_code = 200;
|
|
|
|
response->content_type = "application/json";
|
|
|
|
response->content_length = 0;
|
|
|
|
response->content = "";
|
|
|
|
response->alloced_content = false;
|
2020-05-05 20:29:04 +00:00
|
|
|
}
|
2020-05-20 23:33:18 +00:00
|
|
|
schedule_free(schedule);
|
2020-05-05 20:29:04 +00:00
|
|
|
return;
|
|
|
|
}
|