This commit is contained in:
Tobias Reisinger 2020-06-16 22:05:44 +02:00
parent 0460b2e9f7
commit 176483d72f
59 changed files with 11 additions and 45 deletions

View file

@ -0,0 +1,30 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/controller.h>
#include <models/tag.h>
void
api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
(void)nc;
controller_t** all_controllers = controller_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_controllers[i] != NULL; ++i)
{
cJSON *json_controller = controller_to_json(all_controllers[i]);
cJSON_AddItemToArray(json, json_controller);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
controller_free_list(all_controllers);
}

View file

@ -0,0 +1,194 @@
#include <arpa/inet.h>
#include <cJSON.h>
#include <macros.h>
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/controller.h>
#include <models/tag.h>
void
api_v1_controllers_STR_GET(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))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
cJSON *json = controller_to_json(controller);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
controller_free(controller);
}
void
api_v1_controllers_STR_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))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
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));
controller_free(controller);
return;
}
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
if(json_name)
{
if(cJSON_IsString(json_name) && json_name->valuestring)
{
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
controller->name[MAX_NAME_LENGTH] = '\0';
}
else
{
static const char content[] = "the given name is no valid string";
endpoint_response_text(response, 400, content, STRLEN(content));
cJSON_Delete(json);
controller_free(controller);
return;
}
}
cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip");
if(json_ip)
{
if(cJSON_IsString(json_ip) && json_ip->valuestring)
{
unsigned char buf[sizeof(struct in_addr)];
if(inet_pton(AF_INET, json_ip->valuestring, buf))
{
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
controller->ip[IP_LENGTH] = '\0';
}
else
{
static const char content[] = "the given ip address is no valid IPv4 address";
endpoint_response_text(response, 400, content, STRLEN(content));
cJSON_Delete(json);
controller_free(controller);
return;
}
}
else
{
static const char content[] = "the given ip address is no valid string";
endpoint_response_text(response, 400, content, STRLEN(content));
cJSON_Delete(json);
controller_free(controller);
return;
}
}
if(controller_save(controller))
{
LOG_ERROR("failed to save controller\n");
controller_free(controller);
cJSON_Delete(json);
static const char content[] = "failed to save controller to database";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
cJSON_Delete(json);
json = controller_to_json(controller);
command_set_controller_name(controller);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
controller_free(controller);
}
void
api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
(void)nc;
const char *target_uid_str = args[0].value.v_str;
uuid_t target_uid;
if(uuid_parse(target_uid_str, target_uid))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
if(controller_remove(controller))
{
LOG_ERROR("failed to remove controller from database\n");
static const char content[] = "failed to remove controller from database";
endpoint_response_text(response, 500, content, STRLEN(content));
}
else
{
endpoint_response_text(response, 200, "", 0);
}
controller_free(controller);
return;
}

View file

@ -0,0 +1,51 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/controller.h>
#include <models/tag.h>
void
api_v1_controllers_STR_relays_GET(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))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
relay_t** all_relays = relay_get_by_controller_id(controller->id);
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_relays[i] != NULL; ++i)
{
cJSON *json_relay = relay_to_json(all_relays[i]);
cJSON_AddItemToArray(json, json_relay);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
relay_free_list(all_relays);
controller_free(controller);
}

View file

@ -0,0 +1,231 @@
#include <cJSON.h>
#include <macros.h>
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/controller.h>
#include <models/relay.h>
#include <models/tag.h>
void
api_v1_controllers_STR_relays_INT_GET(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))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
if(!relay)
{
LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
static const char content[] = "no relay for this controller found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
cJSON *json = relay_to_json(relay);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
relay_free(relay);
controller_free(controller);
}
void
api_v1_controllers_STR_relays_INT_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))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
if(!relay)
{
relay = malloc(sizeof(relay_t));
relay->id = 0;
relay->number = args[1].value.v_int;
snprintf(relay->name, MAX_NAME_LENGTH, "Relay %d", relay->number);
relay->name[MAX_NAME_LENGTH] = '\0';
relay->controller_id = controller->id;
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
for(int i = 0; i < 7; ++i)
{
relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
}
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)
{
strncpy(relay->name, json_name->valuestring, MAX_NAME_LENGTH);
relay->name[MAX_NAME_LENGTH] = '\0';
}
cJSON *json_schedule;
cJSON *json_schedules = cJSON_GetObjectItemCaseSensitive(json, "schedules");
if(cJSON_GetArraySize(json_schedules) == 7)
{
int schedule_position = 0;
cJSON_ArrayForEach(json_schedule, json_schedules)
{
cJSON *json_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_schedule, "id");
if(!cJSON_IsString(json_schedule_uid) || (json_schedule_uid->valuestring == NULL))
{
LOG_DEBUG("schedules[%d] is missing uid\n", schedule_position);
cJSON_Delete(json);
static const char content[] = "at least one schedule is missing an id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
uuid_t target_uid;
if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid))
{
LOG_DEBUG("schedules[%d] has bad uid\n", schedule_position);
cJSON_Delete(json);
static const char content[] = "at least one schedule has a bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
schedule_free(relay->schedules[schedule_position]);
relay->schedules[schedule_position] = schedule_get_by_uid_or_off(target_uid);
++schedule_position;
}
}
cJSON *json_active_schedule = cJSON_GetObjectItemCaseSensitive(json, "active_schedule");
if(cJSON_IsObject(json_active_schedule))
{
cJSON *json_active_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_active_schedule, "id");
if(cJSON_IsString(json_active_schedule_uid) && json_active_schedule_uid->valuestring)
{
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
int day_of_week = helper_get_weekday(time_struct);
schedule_free(relay->schedules[day_of_week]);
uuid_t target_uid;
if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid))
{
LOG_DEBUG("active_schedule has bad uid\n");
cJSON_Delete(json);
static const char content[] = "active_schedule has a bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid);
}
}
if(relay_save(relay))
{
LOG_ERROR("failed to save relay\n");
free(controller);
cJSON_Delete(json);
static const char content[] = "failed to save relay to database";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
cJSON *json_tag;
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
if(cJSON_IsArray(json_tags))
{
junction_tag_remove_for_relay(relay->id);
}
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, relay->id, 0);
}
cJSON_Delete(json);
json = relay_to_json(relay);
command_set_relay_schedule(relay);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
relay_free(relay);
controller_free(controller);
}

View file

@ -0,0 +1,309 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <models/controller.h>
#include <logger.h>
#include <mpack.h>
#define DISCOVERY_TIMEOUT_MS 2000
typedef enum
{
DISCOVERY_MAPPING_ID = 0,
DISCOVERY_MAPPING_NAME = 1,
DISCOVERY_MAPPING_COMMAND_PORT = 2,
DISCOVERY_MAPPING_RELAY_COUNT = 3,
} discovery_mapping_t;
static int
bind_tcp_server(const char *addr, const char *port, int max_client_backlog)
{
struct addrinfo hints, *res;
int fd;
int status;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(addr, port, &hints, &res)) != 0)
{
LOG_ERROR("error getting address info: %s\n", gai_strerror(status));
}
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
{
LOG_ERROR("error binding socket: %s\n", status);
freeaddrinfo(res);
return -1;
}
if ((status = listen(fd, max_client_backlog)) == -1)
{
LOG_ERROR("error setting up listener: %s\n", status);
freeaddrinfo(res);
return -1;
}
freeaddrinfo(res);
return fd;
}
static int
get_server_port(int fd)
{
if(fd == -1)
{
return -1;
}
struct sockaddr_in sin;
socklen_t addr_len = sizeof(sin);
if(getsockname(fd, (struct sockaddr *)&sin, &addr_len) == 0)
{
return ntohs(sin.sin_port);
}
return -1;
}
static int
send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length)
{
struct sockaddr_in their_addr;
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
LOG_ERROR("error creating socket\n");
return -1;
}
int broadcast = 1;
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) < 0)
{
LOG_ERROR("error setting broadcast\n");
return -1;
}
memset(&their_addr, 0, sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(port);
their_addr.sin_addr.s_addr = inet_addr(addr);
if(sendto(fd, message, length, 0, (struct sockaddr *)&their_addr, sizeof(their_addr)) < 0)
{
LOG_ERROR("error sending broadcast (%d): '%s'\n", errno, strerror(errno));
return -1;
}
close(fd);
return 0;
}
void
api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)nc;
(void)hm;
int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
int discover_server_port = get_server_port(discover_server_socket);
if(discover_server_port == -1)
{
LOG_ERROR("failed to get server port for discovery\n");
static const char content[] = "";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
int16_t payload[1];
payload[0] = discover_server_port;
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
{
LOG_ERROR("failed to send UDP broadcast\n");
static const char content[] = "";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
struct sockaddr_storage their_addr;
socklen_t addr_size;
int client_fd, s_ret;
fd_set accept_fds;
struct timeval timeout;
uint8_t discover_answer_buf[1];
controller_t **known_controllers = controller_get_all();
while(true)
{
addr_size = sizeof(their_addr);
FD_ZERO(&accept_fds);
FD_SET(discover_server_socket, &accept_fds); // NOLINT(hicpp-signed-bitwise)
timeout.tv_sec = DISCOVERY_TIMEOUT_MS / 1000;
timeout.tv_usec = (DISCOVERY_TIMEOUT_MS % 1000) * 1000;
s_ret = select(discover_server_socket + 1, &accept_fds, NULL, NULL, &timeout);
if(s_ret == 0)
{
break;
}
else
{
if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0)
{
LOG_ERROR("error accepting client %s\n", strerror(errno));
continue;
}
size_t payload_length;
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
{
LOG_ERROR("error receiving header from client\n");
continue;
}
char *answer_payload = (char*)malloc((payload_length));
ssize_t bytes_transferred;
if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0)
{
LOG_ERROR("error receiving payload from client\n");
continue;
}
struct sockaddr_in addr;
socklen_t client_addr_size = sizeof(struct sockaddr_in);
if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0)
{
LOG_ERROR("error receiving payload from client\n");
continue;
}
uuid_t discovered_id;
mpack_tree_t tree;
mpack_tree_init_data(&tree, answer_payload, payload_length);
mpack_tree_parse(&tree);
mpack_node_t root = mpack_tree_root(&tree);
memcpy(discovered_id, mpack_node_data(mpack_node_map_uint(root, DISCOVERY_MAPPING_ID)), sizeof(uuid_t));
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
size_t discovered_name_len = mpack_node_strlen(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
if(discovered_name_len > MAX_NAME_LENGTH)
{
discovered_name_len = MAX_NAME_LENGTH;
}
bool found_discovered_in_list = 0;
for(int i = 0; known_controllers[i] != NULL; i++)
{
if(!found_discovered_in_list)
{
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
{
known_controllers[i]->active = 1;
strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
known_controllers[i]->name[discovered_name_len] = '\0';
known_controllers[i]->port = discovered_command_port;
known_controllers[i]->relay_count = discovered_relay_count;
controller_save(known_controllers[i]);
controller_free(known_controllers[i]);
found_discovered_in_list = 1;
known_controllers[i] = known_controllers[i + 1];
}
}
else
{
known_controllers[i] = known_controllers[i + 1];
}
}
if(!found_discovered_in_list)
{
controller_t *discovered_controller = malloc(sizeof(controller_t));
discovered_controller->id = 0;
strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
memcpy(discovered_controller->uid, discovered_id, sizeof(uuid_t));
strncpy(discovered_controller->name, discovered_name, discovered_name_len);
discovered_controller->name[discovered_name_len] = '\0';
discovered_controller->relay_count = discovered_relay_count;
discovered_controller->port = discovered_command_port;
discovered_controller->active = 1;
controller_save(discovered_controller);
// TODO get relays during discovery and don't create empty ones
relay_t **discovered_relays = malloc(sizeof(relay_t*) * (discovered_controller->relay_count + 1));
for(int i = 0; i < discovered_controller->relay_count; ++i)
{
relay_t *new_relay = malloc(sizeof(relay_t));
new_relay->id = 0;
sprintf(new_relay->name, "Relay %d", i + 1);
new_relay->number = i;
new_relay->controller_id = discovered_controller->id;
uuid_t tmp_uuid;
memset(tmp_uuid, 0, sizeof(uuid_t));
memcpy(tmp_uuid, "off", 3);
for(int i = 0; i < 7; ++i)
{
new_relay->schedules[i] = schedule_get_by_uid(tmp_uuid);
}
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
new_relay->active_schedule = new_relay->schedules[helper_get_weekday(time_struct)];
relay_save(new_relay);
discovered_relays[i] = new_relay;
}
discovered_relays[discovered_controller->relay_count] = NULL;
discovered_controller->relays = discovered_relays;
controller_free(discovered_controller);
}
mpack_tree_destroy(&tree);
free(answer_payload);
discover_answer_buf[0] = 0; // TODO add discovery return codes
send(client_fd, discover_answer_buf, sizeof(uint8_t), 0);
close(client_fd);
}
}
for(int i = 0; known_controllers[i] != NULL; i++)
{
known_controllers[i]->active = false;
controller_save(known_controllers[i]);
LOG_DEBUG("lost: %s\n", known_controllers[i]->name);
}
controller_free_list(known_controllers);
api_v1_controllers_GET(nc, hm, args, response);
}

View file

@ -0,0 +1,31 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_relays.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/relay.h>
#include <models/tag.h>
void
api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
(void)nc;
relay_t** all_relays = relay_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_relays[i] != NULL; ++i)
{
cJSON *json_relay = relay_to_json(all_relays[i]);
cJSON_AddItemToArray(json, json_relay);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
relay_free_list(all_relays);
}

View file

@ -0,0 +1,47 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_relays.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/relay.h>
#include <models/tag.h>
void
api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
(void)nc;
int tag_id = tag_get_id(args[0].value.v_str);
int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id);
if(relays_ids == NULL)
{
LOG_ERROR("failed to load relays for tag from database\n");
static const char content[] = "failed to load relays for tag from database";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
cJSON *json = cJSON_CreateArray();
for(int i = 0; relays_ids[i] != 0; ++i)
{
relay_t* relay = relay_get_by_id(relays_ids[i]);
if(!relay)
{
continue;
}
cJSON *json_relay = relay_to_json(relay);
cJSON_AddItemToArray(json, json_relay);
relay_free(relay);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
free(relays_ids);
}

View file

@ -0,0 +1,176 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_schedules.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/schedule.h>
#include <models/tag.h>
void
api_v1_schedules_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))
{
LOG_DEBUG("no name for schedule provided\n");
cJSON_Delete(json);
static const char content[] = "no name for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
if(!cJSON_IsArray(json_periods))
{
LOG_DEBUG("no periods for schedule provided\n");
cJSON_Delete(json);
static const char content[] = "no periods for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_tag;
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
cJSON_ArrayForEach(json_tag, json_tags)
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOG_DEBUG("invalid tag in tags\n");
cJSON_Delete(json);
static const char content[] = "invalid tag in tags";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
}
schedule_t *new_schedule = malloc(sizeof(schedule_t));
new_schedule->id = 0;
uuid_generate(new_schedule->uid);
strncpy(new_schedule->name, json_name->valuestring, MAX_NAME_LENGTH);
new_schedule->name[MAX_NAME_LENGTH] = '\0';
int periods_count = cJSON_GetArraySize(json_periods);
new_schedule->periods = malloc(sizeof(period_t) * periods_count);
int periods_valid = 0;
cJSON *json_period;
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");
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing a start";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
{
LOG_DEBUG("period is missing end\n");
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing an end";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
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);
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the start for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
{
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the end for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
new_schedule->periods[periods_valid].start = start;
new_schedule->periods[periods_valid].end = end;
++periods_valid;
}
new_schedule->periods_count = periods_valid;
schedule_save(new_schedule);
junction_tag_remove_for_schedule(new_schedule->id);
json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
cJSON_ArrayForEach(json_tag, json_tags)
{
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, new_schedule->id);
}
cJSON_Delete(json);
json = schedule_to_json(new_schedule);
endpoint_response_json(response, 201, json);
cJSON_Delete(json);
schedule_free(new_schedule);
}
void
api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
(void)nc;
schedule_t** all_schedules = schedule_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_schedules[i] != NULL; ++i)
{
cJSON *json_schedule = schedule_to_json(all_schedules[i]);
cJSON_AddItemToArray(json, json_schedule);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
schedule_free_list(all_schedules);
}

View file

@ -0,0 +1,238 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_schedules.h>
#include <logger.h>
#include <command.h>
#include <models/junction_tag.h>
#include <models/junction_relay_schedule.h>
#include <models/schedule.h>
#include <models/relay.h>
#include <models/tag.h>
void
api_v1_schedules_STR_GET(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(schedule_uid_parse(args[0].value.v_str, target_uid))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
schedule_t* schedule = schedule_get_by_uid(target_uid);
if(!schedule)
{
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";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
cJSON *json = schedule_to_json(schedule);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
schedule_free(schedule);
}
void
api_v1_schedules_STR_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(schedule_uid_parse(args[0].value.v_str, target_uid))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
schedule_t* schedule = schedule_get_by_uid(target_uid);
if(!schedule)
{
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";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
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)
{
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;
}
schedule->periods[periods_valid].start = start;
schedule->periods[periods_valid].end = end;
++periods_valid;
}
schedule->periods_count = periods_valid;
}
if(schedule_save(schedule))
{
LOG_ERROR("failed to save schedule\n");
free(schedule);
cJSON_Delete(json);
static const char content[] = "failed to save schedule to database";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
relay_t **relays = relay_get_with_schedule(schedule->id);
for(int i = 0; relays[i] != NULL; ++i)
{
command_set_relay_schedule(relays[i]);
}
cJSON *json_tag;
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
if(cJSON_IsArray(json_tags))
{
junction_tag_remove_for_schedule(schedule->id);
}
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);
}
cJSON_Delete(json);
json = schedule_to_json(schedule);
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
relay_free_list(relays);
schedule_free(schedule);
}
void
api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
(void)nc;
const char *target_uid_str = args[0].value.v_str;
uuid_t target_uid;
if(schedule_uid_parse(target_uid_str, target_uid))
{
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
schedule_t* schedule = schedule_get_by_uid(target_uid);
if(!schedule)
{
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";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
if(schedule_is_protected(schedule))
{
static const char content[] = "target schedule is protected";
endpoint_response_text(response, 403, content, STRLEN(content));
schedule_free(schedule);
return;
}
if(schedule_remove(schedule))
{
LOG_ERROR("failed to remove schedule from database\n");
static const char content[] = "failed to remove schedule from database";
endpoint_response_text(response, 500, content, STRLEN(content));
}
else
{
endpoint_response_text(response, 200, "", 0);
}
schedule_free(schedule);
return;
}

View file

@ -0,0 +1,155 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_schedules.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/schedule.h>
#include <models/tag.h>
void
api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)nc;
cJSON *json_list = cJSON_ParseWithLength(hm->body.p, hm->body.len);
cJSON *json;
cJSON_ArrayForEach(json, json_list)
{
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))
{
LOG_DEBUG("no name for schedule provided\n");
cJSON_Delete(json_list);
static const char content[] = "no name for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
if(!cJSON_IsArray(json_periods))
{
LOG_DEBUG("no periods for schedule provided\n");
cJSON_Delete(json_list);
static const char content[] = "no periods for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_tag;
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
cJSON_ArrayForEach(json_tag, json_tags)
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOG_DEBUG("invalid tag in tags\n");
cJSON_Delete(json_list);
static const char content[] = "invalid tag in tags";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
}
schedule_t *new_schedule = malloc(sizeof(schedule_t));
new_schedule->id = 0;
uuid_generate(new_schedule->uid);
strncpy(new_schedule->name, json_name->valuestring, MAX_NAME_LENGTH);
new_schedule->name[MAX_NAME_LENGTH] = '\0';
int periods_count = cJSON_GetArraySize(json_periods);
new_schedule->periods = malloc(sizeof(period_t) * periods_count);
int periods_valid = 0;
cJSON *json_period;
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");
cJSON_Delete(json_list);
schedule_free(new_schedule);
static const char content[] = "one period is missing a start";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
{
LOG_DEBUG("period is missing end\n");
cJSON_Delete(json_list);
schedule_free(new_schedule);
static const char content[] = "one period is missing an end";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
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);
cJSON_Delete(json_list);
schedule_free(new_schedule);
static const char content[] = "the start for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
{
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
cJSON_Delete(json_list);
schedule_free(new_schedule);
static const char content[] = "the end for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
new_schedule->periods[periods_valid].start = start;
new_schedule->periods[periods_valid].end = end;
++periods_valid;
}
new_schedule->periods_count = periods_valid;
schedule_save(new_schedule);
junction_tag_remove_for_schedule(new_schedule->id);
json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
cJSON_ArrayForEach(json_tag, json_tags)
{
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, new_schedule->id);
}
schedule_free(new_schedule);
}
cJSON_Delete(json_list);
api_v1_schedules_GET(nc, hm, args, response);;
}

View file

@ -0,0 +1,47 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_schedules.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/schedule.h>
#include <models/tag.h>
void
api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
(void)nc;
int tag_id = tag_get_id(args[0].value.v_str);
int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
if(schedules_ids == NULL)
{
LOG_ERROR("failed to load schedules for tag from database\n");
static const char content[] = "failed to load schedules for tag from database";
endpoint_response_text(response, 500, content, STRLEN(content));
return;
}
cJSON *json = cJSON_CreateArray();
for(int i = 0; schedules_ids[i] != 0; ++i)
{
schedule_t* schedule = schedule_get_by_id(schedules_ids[i]);
if(!schedule)
{
continue;
}
cJSON *json_schedule = schedule_to_json(schedule);
cJSON_AddItemToArray(json, json_schedule);
schedule_free(schedule);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
free(schedules_ids);
}

View file

@ -0,0 +1,35 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_tags.h>
#include <logger.h>
#include <models/tag.h>
void
api_v1_tags_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)hm;
(void)nc;
char** all_tags = tag_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_tags[i] != NULL; ++i)
{
cJSON *json_tag = cJSON_CreateString(all_tags[i]);
if (json_tag == NULL)
{
LOG_DEBUG("failed to add tag from string '%s'\n", all_tags[i]);
free(all_tags[i]);
continue;
}
cJSON_AddItemToArray(json, json_tag);
free(all_tags[i]);
}
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
free(all_tags);
}