From 0edb16a2d53663c45ca4606c9dc325e8a95efbe3 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 6 May 2020 22:49:22 +0200 Subject: [PATCH] add: controller/..../relays endpoints add: relay functions remove: drivers --- drivers/gpio.c | 14 - drivers/piface.c | 14 - endpoints/api_v1_controllers_STR.c | 172 +++++++++++++ endpoints/api_v1_controllers_STR_relays.c | 59 +++++ endpoints/api_v1_controllers_STR_relays_INT.c | 239 ++++++++++++++++++ endpoints/api_v1_controllers_discover.c | 6 +- endpoints/api_v1_schedules_STR.c | 5 +- include/endpoints/api_v1_controllers.h | 18 ++ include/models/controller.h | 4 +- include/models/relay.h | 11 +- include/models/schedule.h | 3 + models/controller.c | 1 - models/relay.c | 53 +++- models/schedule.c | 25 ++ router.c | 7 + 15 files changed, 587 insertions(+), 44 deletions(-) delete mode 100644 drivers/gpio.c delete mode 100644 drivers/piface.c create mode 100644 endpoints/api_v1_controllers_STR.c create mode 100644 endpoints/api_v1_controllers_STR_relays.c create mode 100644 endpoints/api_v1_controllers_STR_relays_INT.c diff --git a/drivers/gpio.c b/drivers/gpio.c deleted file mode 100644 index 0fd78b9..0000000 --- a/drivers/gpio.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -#include - -void -driver_gpio_set(int pin, int value) -{ - // disable "unused parameter" warning (happens when using wiring_debug) - (void)pin; - (void)value; - digitalWrite(pin, value); -} diff --git a/drivers/piface.c b/drivers/piface.c deleted file mode 100644 index d26eec9..0000000 --- a/drivers/piface.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -#include - -void -driver_piface_set(int pin, int value) -{ - // disable "unused parameter" warning (happens when using wiring_debug) - (void)pin; - (void)value; - digitalWrite(PIFACE_GPIO_BASE + pin, value); -} diff --git a/endpoints/api_v1_controllers_STR.c b/endpoints/api_v1_controllers_STR.c new file mode 100644 index 0000000..c6646c7 --- /dev/null +++ b/endpoints/api_v1_controllers_STR.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include + +void +api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + uuid_t target_uid; + if(uuid_parse(args[0].value.v_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + cJSON *json = controller_to_json(controller); + + char *json_str = cJSON_Print(json); + if (json_str == NULL) + { + LOG_ERROR("failed to print controllers json\n"); + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "[]"); + } + else + { + mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "%s", json_str); + free(json_str); + } + cJSON_Delete(json); + controller_free(controller); +} + +void +api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + uuid_t target_uid; + if(uuid_parse(args[0].value.v_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); + + if(json == NULL) + { + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) + { + LOG_ERROR("error before: %s\n", error_ptr); + } + cJSON_Delete(json); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + cJSON *json_name = cJSON_GetObjectItemCaseSensitive(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'; + } + + cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip"); + if(cJSON_IsString(json_ip) && json_ip->valuestring) + { + strncpy(controller->ip, json_ip->valuestring, IP_LENGTH); + controller->ip[IP_LENGTH] = '\0'; + } + + if(controller_save(controller)) + { + LOG_ERROR("failed to save controller\n"); + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + free(controller); + cJSON_Delete(json); + return; + } + + cJSON_Delete(json); + json = controller_to_json(controller); + + char *json_str = cJSON_Print(json); + if (json_str == NULL) + { + LOG_ERROR("failed to print controller json\n"); + mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + } + else + { + mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "%s", json_str); + free(json_str); + } + cJSON_Delete(json); + controller_free(controller); +} + +void +api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + const char *target_uid_str = args[0].value.v_str; + + uuid_t target_uid; + if(uuid_parse(target_uid_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", target_uid_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + if(controller_remove(controller)) + { + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + } + else + { + mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + } + controller_free(controller); + return; +} diff --git a/endpoints/api_v1_controllers_STR_relays.c b/endpoints/api_v1_controllers_STR_relays.c new file mode 100644 index 0000000..daf630b --- /dev/null +++ b/endpoints/api_v1_controllers_STR_relays.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +void +api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + uuid_t target_uid; + if(uuid_parse(args[0].value.v_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + 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); + } + + char *json_str = cJSON_Print(json); + if (json_str == NULL) + { + LOG_ERROR("failed to print relays json\n"); + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "[]"); + } + else + { + mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "%s", json_str); + free(json_str); + } + cJSON_Delete(json); + relay_free_list(all_relays); + controller_free(controller); +} diff --git a/endpoints/api_v1_controllers_STR_relays_INT.c b/endpoints/api_v1_controllers_STR_relays_INT.c new file mode 100644 index 0000000..7bdc2d1 --- /dev/null +++ b/endpoints/api_v1_controllers_STR_relays_INT.c @@ -0,0 +1,239 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void +api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + uuid_t target_uid; + if(uuid_parse(args[0].value.v_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int); + + if(!relay) + { + LOG_ERROR("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + cJSON *json = relay_to_json(relay); + + char *json_str = cJSON_Print(json); + if (json_str == NULL) + { + LOG_ERROR("failed to print relays json\n"); + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "[]"); + } + else + { + mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "%s", json_str); + free(json_str); + } + cJSON_Delete(json); + relay_free(relay); + controller_free(controller); +} + +void +api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm) +{ + (void)hm; + + uuid_t target_uid; + if(uuid_parse(args[0].value.v_str, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + + controller_t* controller = controller_get_by_uid(target_uid); + + if(!controller) + { + LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str); + mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + 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); + } + relay->active_schedule = schedule_get_by_uid(tmp_uuid); + } + + cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); + + if(json == NULL) + { + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) + { + LOG_ERROR("error before: %s\n", error_ptr); + } + cJSON_Delete(json); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + 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); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + return; + } + uuid_t target_uid; + if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + 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) + { + int day_of_week = helper_get_weekday(time(NULL)); + schedule_free(relay->schedules[day_of_week]); + + uuid_t target_uid; + if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid)) + { + LOG_ERROR("failed to unparse uid\n"); + mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + 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"); + mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + free(relay); + cJSON_Delete(json); + 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); + + char *json_str = cJSON_Print(json); + if (json_str == NULL) + { + LOG_ERROR("failed to print relay json\n"); + mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "{}"); + } + else + { + mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS); + mg_printf(c, "%s", json_str); + free(json_str); + } + cJSON_Delete(json); + relay_free(relay); + controller_free(controller); +} diff --git a/endpoints/api_v1_controllers_discover.c b/endpoints/api_v1_controllers_discover.c index 14ea2fb..971dd8b 100644 --- a/endpoints/api_v1_controllers_discover.c +++ b/endpoints/api_v1_controllers_discover.c @@ -214,7 +214,7 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, { known_controllers[i]->active = 1; strncpy(known_controllers[i]->name, discovered_name, discovered_name_len); - known_controllers[i]->name[MAX_NAME_LENGTH] = '\0'; + known_controllers[i]->name[discovered_name_len] = '\0'; known_controllers[i]->port = discovered_command_port; known_controllers[i]->relay_count = discovered_relay_count; @@ -237,8 +237,8 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, 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, MAX_NAME_LENGTH); - discovered_controller->name[MAX_NAME_LENGTH] = '\0'; + 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; diff --git a/endpoints/api_v1_schedules_STR.c b/endpoints/api_v1_schedules_STR.c index 4577483..6643ccd 100644 --- a/endpoints/api_v1_schedules_STR.c +++ b/endpoints/api_v1_schedules_STR.c @@ -150,9 +150,12 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct return; } - junction_tag_remove_for_schedule(schedule->id); 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)) diff --git a/include/endpoints/api_v1_controllers.h b/include/endpoints/api_v1_controllers.h index a862e5b..050680d 100644 --- a/include/endpoints/api_v1_controllers.h +++ b/include/endpoints/api_v1_controllers.h @@ -9,4 +9,22 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, void api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); +void +api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + +void +api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + +void +api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + +void +api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + +void +api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + +void +api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm); + #endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */ diff --git a/include/models/controller.h b/include/models/controller.h index 3e9cf81..0aaff64 100644 --- a/include/models/controller.h +++ b/include/models/controller.h @@ -9,12 +9,14 @@ #include #include +#define IP_LENGTH 16 + typedef struct { int id; uuid_t uid; char name[MAX_NAME_LENGTH + 1]; - char ip[17]; + char ip[IP_LENGTH + 1]; int active; int port; int relay_count; diff --git a/include/models/relay.h b/include/models/relay.h index b69e742..c220845 100644 --- a/include/models/relay.h +++ b/include/models/relay.h @@ -27,9 +27,15 @@ relay_save(); int relay_remove(); +void +relay_reload_active_schedule(relay_t *relay); + cJSON* relay_to_json(); +void +relay_free(relay_t *relay); + void relay_free_list(relay_t **relays_list); @@ -40,10 +46,7 @@ relay_t* relay_get_by_id(int id); relay_t* -relay_get_relay_for_controller(uuid_t controller_id, int relay_num); - -bool -relay_valid_num_is_for_controller(uuid_t controller_id, int relay_num); +relay_get_for_controller(int controller_id, int relay_num); relay_t** relay_get_all(); diff --git a/include/models/schedule.h b/include/models/schedule.h index e3860f4..5b2210a 100644 --- a/include/models/schedule.h +++ b/include/models/schedule.h @@ -46,6 +46,9 @@ schedule_get_by_id_or_off(int id); schedule_t* schedule_get_by_id(int id); +schedule_t* +schedule_get_by_uid_or_off(uuid_t uid); + schedule_t* schedule_get_by_uid(uuid_t uid); diff --git a/models/controller.c b/models/controller.c index 68f105a..7ad9be7 100644 --- a/models/controller.c +++ b/models/controller.c @@ -280,5 +280,4 @@ controller_get_all() sqlite3_prepare_v2(global_database, "SELECT * FROM controllers;", -1, &stmt, NULL); return controller_db_select(stmt); - } diff --git a/models/relay.c b/models/relay.c index eb6cb37..d86e0f9 100644 --- a/models/relay.c +++ b/models/relay.c @@ -71,6 +71,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt) int schedule_id = junction_relay_schedule_get_schedule_id(i, new_relay->id); new_relay->schedules[i] = schedule_get_by_id_or_off(schedule_id); } + relay_reload_active_schedule(new_relay); return new_relay; } @@ -165,10 +166,15 @@ relay_remove(relay_t *relay) return rc != SQLITE_DONE; } +void +relay_reload_active_schedule(relay_t *relay) +{ + relay->active_schedule = relay->schedules[helper_get_weekday(time(NULL))]; +} + void relay_free(relay_t *relay) { - schedule_free(relay->active_schedule); for(int i = 0; i < 7; ++i) { schedule_free(relay->schedules[i]); @@ -189,11 +195,14 @@ relay_free_list(relay_t **relays) cJSON* relay_to_json(relay_t *relay) { + relay_reload_active_schedule(relay); + cJSON *json = cJSON_CreateObject(); cJSON *json_number = cJSON_CreateNumber(relay->number); if(json_number == NULL) { + LOG_DEBUG("failed to make number\n"); cJSON_Delete(json); return NULL; } @@ -202,27 +211,41 @@ relay_to_json(relay_t *relay) cJSON *json_name = cJSON_CreateString(relay->name); if(json_name == NULL) { + LOG_DEBUG("failed to make name\n"); cJSON_Delete(json); return NULL; } cJSON_AddItemToObject(json, "name", json_name); - controller_t *controller = controller_get_by_id(relay->id); + controller_t *controller = controller_get_by_id(relay->controller_id); if(!controller) { + LOG_DEBUG("failed to get controller\n"); cJSON_Delete(json); return NULL; } char uuid_str[UUID_STR_LEN]; uuid_unparse(controller->uid, uuid_str); - cJSON *json_id = cJSON_CreateString(uuid_str); - if(json_name == NULL) + cJSON *json_controller_id = cJSON_CreateString(uuid_str); + if(json_controller_id == NULL) { + LOG_DEBUG("failed to make controller id\n"); cJSON_Delete(json); return NULL; } - cJSON_AddItemToObject(json, "id", json_id); + cJSON_AddItemToObject(json, "controller_id", json_controller_id); + + controller_free(controller); + + cJSON_AddItemToObject(json, "active_schedule", schedule_to_json(relay->active_schedule)); + + cJSON *json_schedules = cJSON_CreateArray(); + for(int i = 0; i < 7; ++i) + { + cJSON_AddItemToArray(json_schedules, schedule_to_json(relay->schedules[i])); + } + cJSON_AddItemToObject(json, "schedules", json_schedules); cJSON *json_tags = cJSON_CreateArray(); int *tags_ids = junction_tag_get_tags_for_relay_id(relay->id); @@ -295,12 +318,30 @@ relay_get_all() return relay_db_select(stmt); } +relay_t* +relay_get_for_controller(int controller_id, int relay_num) +{ + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE controller_id = ?1 AND number = ?2;", -1, &stmt, NULL); + sqlite3_bind_int(stmt, 1, controller_id); + sqlite3_bind_int(stmt, 2, relay_num); + + relay_t **sql_result = relay_db_select(stmt); + + relay_t *result = sql_result[0]; + free(sql_result); + + return result; + +} + relay_t** relay_get_by_controller_id(int controller_id) { sqlite3_stmt *stmt; - sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE controller_id;", -1, &stmt, NULL); + sqlite3_prepare_v2(global_database, "SELECT * FROM relays WHERE controller_id = ?1;", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, controller_id); return relay_db_select(stmt); diff --git a/models/schedule.c b/models/schedule.c index 5d33870..b10f200 100644 --- a/models/schedule.c +++ b/models/schedule.c @@ -347,6 +347,31 @@ schedule_get_by_id(int id) return result; } +schedule_t* +schedule_get_by_uid_or_off(uuid_t uid) +{ + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(global_database, "SELECT * FROM schedules WHERE uid = ?1;", -1, &stmt, NULL); + sqlite3_bind_blob(stmt, 1, uid, sizeof(uuid_t), SQLITE_STATIC); + + schedule_t **sql_result = schedule_db_select(stmt); + + schedule_t *result = sql_result[0]; + free(sql_result); + + if(result) + { + return result; + } + + uuid_t tmp_uuid; + memset(tmp_uuid, 0, sizeof(uuid_t)); + memcpy(tmp_uuid, "off", 3); + + return schedule_get_by_uid(tmp_uuid); +} + schedule_t* schedule_get_by_uid(uuid_t uid) { diff --git a/router.c b/router.c index 7ec9aa4..c386c98 100644 --- a/router.c +++ b/router.c @@ -63,6 +63,13 @@ router_init() router_register_endpoint("/api/v1/controllers/discover/", HTTP_METHOD_POST, api_v1_controllers_discover_POST); router_register_endpoint("/api/v1/controllers/", HTTP_METHOD_GET, api_v1_controllers_GET); + router_register_endpoint("/api/v1/controllers/{str}", HTTP_METHOD_GET, api_v1_controllers_STR_GET); + router_register_endpoint("/api/v1/controllers/{str}", HTTP_METHOD_PUT, api_v1_controllers_STR_PUT); + router_register_endpoint("/api/v1/controllers/{str}", HTTP_METHOD_DELETE, api_v1_controllers_STR_DELETE); + + router_register_endpoint("/api/v1/controllers/{str}/relays/", HTTP_METHOD_GET, api_v1_controllers_STR_relays_GET); + router_register_endpoint("/api/v1/controllers/{str}/relays/{int}", HTTP_METHOD_GET, api_v1_controllers_STR_relays_INT_GET); + router_register_endpoint("/api/v1/controllers/{str}/relays/{int}", HTTP_METHOD_PUT, api_v1_controllers_STR_relays_INT_PUT); router_register_endpoint("/api/v1/relays/", HTTP_METHOD_GET, api_v1_relays_GET); }