add: controller/..../relays endpoints
add: relay functions remove: drivers
This commit is contained in:
		
							parent
							
								
									f040cd8b21
								
							
						
					
					
						commit
						0edb16a2d5
					
				
					 15 changed files with 587 additions and 44 deletions
				
			
		| 
						 | 
					@ -1,14 +0,0 @@
 | 
				
			||||||
#include <wiringPi.h>
 | 
					 | 
				
			||||||
#include <piFace.h>
 | 
					 | 
				
			||||||
#include <wiring_debug.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include <drivers.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void
 | 
					 | 
				
			||||||
driver_gpio_set(int pin, int value)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    // disable "unused parameter" warning (happens when using wiring_debug)
 | 
					 | 
				
			||||||
    (void)pin;
 | 
					 | 
				
			||||||
    (void)value;
 | 
					 | 
				
			||||||
    digitalWrite(pin, value);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,14 +0,0 @@
 | 
				
			||||||
#include <wiringPi.h>
 | 
					 | 
				
			||||||
#include <piFace.h>
 | 
					 | 
				
			||||||
#include <wiring_debug.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include <drivers.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										172
									
								
								endpoints/api_v1_controllers_STR.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										172
									
								
								endpoints/api_v1_controllers_STR.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,172 @@
 | 
				
			||||||
 | 
					#include <cJSON.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 *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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										59
									
								
								endpoints/api_v1_controllers_STR_relays.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								endpoints/api_v1_controllers_STR_relays.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					#include <cJSON.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 *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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										239
									
								
								endpoints/api_v1_controllers_STR_relays_INT.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										239
									
								
								endpoints/api_v1_controllers_STR_relays_INT.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,239 @@
 | 
				
			||||||
 | 
					#include <cJSON.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 *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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -214,7 +214,7 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        known_controllers[i]->active = 1;
 | 
					                        known_controllers[i]->active = 1;
 | 
				
			||||||
                        strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
 | 
					                        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]->port = discovered_command_port;
 | 
				
			||||||
                        known_controllers[i]->relay_count = discovered_relay_count;
 | 
					                        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;
 | 
					                discovered_controller->id = 0;
 | 
				
			||||||
                strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
 | 
					                strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
 | 
				
			||||||
                memcpy(discovered_controller->uid, discovered_id, sizeof(uuid_t));
 | 
					                memcpy(discovered_controller->uid, discovered_id, sizeof(uuid_t));
 | 
				
			||||||
                strncpy(discovered_controller->name, discovered_name, MAX_NAME_LENGTH);
 | 
					                strncpy(discovered_controller->name, discovered_name, discovered_name_len);
 | 
				
			||||||
                discovered_controller->name[MAX_NAME_LENGTH] = '\0';
 | 
					                discovered_controller->name[discovered_name_len] = '\0';
 | 
				
			||||||
                discovered_controller->relay_count = discovered_relay_count;
 | 
					                discovered_controller->relay_count = discovered_relay_count;
 | 
				
			||||||
                discovered_controller->port = discovered_command_port;
 | 
					                discovered_controller->port = discovered_command_port;
 | 
				
			||||||
                discovered_controller->active = 1;
 | 
					                discovered_controller->active = 1;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -150,9 +150,12 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    junction_tag_remove_for_schedule(schedule->id);
 | 
					 | 
				
			||||||
    cJSON *json_tag;
 | 
					    cJSON *json_tag;
 | 
				
			||||||
    cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
 | 
					    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)
 | 
					    cJSON_ArrayForEach(json_tag, json_tags)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
 | 
					        if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,4 +9,22 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					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 */
 | 
					#endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,12 +9,14 @@
 | 
				
			||||||
#include <helpers.h>
 | 
					#include <helpers.h>
 | 
				
			||||||
#include <models/relay.h>
 | 
					#include <models/relay.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define IP_LENGTH 16
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct
 | 
					typedef struct
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int id;
 | 
					    int id;
 | 
				
			||||||
    uuid_t uid;
 | 
					    uuid_t uid;
 | 
				
			||||||
    char name[MAX_NAME_LENGTH + 1];
 | 
					    char name[MAX_NAME_LENGTH + 1];
 | 
				
			||||||
    char ip[17];
 | 
					    char ip[IP_LENGTH + 1];
 | 
				
			||||||
    int active;
 | 
					    int active;
 | 
				
			||||||
    int port;
 | 
					    int port;
 | 
				
			||||||
    int relay_count;
 | 
					    int relay_count;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,9 +27,15 @@ relay_save();
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
relay_remove();
 | 
					relay_remove();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					relay_reload_active_schedule(relay_t *relay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cJSON*
 | 
					cJSON*
 | 
				
			||||||
relay_to_json();
 | 
					relay_to_json();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					relay_free(relay_t *relay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
relay_free_list(relay_t **relays_list);
 | 
					relay_free_list(relay_t **relays_list);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -40,10 +46,7 @@ relay_t*
 | 
				
			||||||
relay_get_by_id(int id);
 | 
					relay_get_by_id(int id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
relay_t*
 | 
					relay_t*
 | 
				
			||||||
relay_get_relay_for_controller(uuid_t controller_id, int relay_num);
 | 
					relay_get_for_controller(int controller_id, int relay_num);
 | 
				
			||||||
 | 
					 | 
				
			||||||
bool
 | 
					 | 
				
			||||||
relay_valid_num_is_for_controller(uuid_t controller_id, int relay_num);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
relay_t**
 | 
					relay_t**
 | 
				
			||||||
relay_get_all();
 | 
					relay_get_all();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,9 @@ schedule_get_by_id_or_off(int id);
 | 
				
			||||||
schedule_t*
 | 
					schedule_t*
 | 
				
			||||||
schedule_get_by_id(int id);
 | 
					schedule_get_by_id(int id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					schedule_t*
 | 
				
			||||||
 | 
					schedule_get_by_uid_or_off(uuid_t uid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
schedule_t*
 | 
					schedule_t*
 | 
				
			||||||
schedule_get_by_uid(uuid_t uid);
 | 
					schedule_get_by_uid(uuid_t uid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -280,5 +280,4 @@ controller_get_all()
 | 
				
			||||||
    sqlite3_prepare_v2(global_database, "SELECT * FROM controllers;", -1, &stmt, NULL);
 | 
					    sqlite3_prepare_v2(global_database, "SELECT * FROM controllers;", -1, &stmt, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return controller_db_select(stmt);
 | 
					    return controller_db_select(stmt);
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -71,6 +71,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
 | 
				
			||||||
        int schedule_id = junction_relay_schedule_get_schedule_id(i, new_relay->id);
 | 
					        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);
 | 
					        new_relay->schedules[i] = schedule_get_by_id_or_off(schedule_id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    relay_reload_active_schedule(new_relay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return new_relay;
 | 
					    return new_relay;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -165,10 +166,15 @@ relay_remove(relay_t *relay)
 | 
				
			||||||
    return rc != SQLITE_DONE;
 | 
					    return rc != SQLITE_DONE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					relay_reload_active_schedule(relay_t *relay)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    relay->active_schedule = relay->schedules[helper_get_weekday(time(NULL))];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
relay_free(relay_t *relay)
 | 
					relay_free(relay_t *relay)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    schedule_free(relay->active_schedule);
 | 
					 | 
				
			||||||
    for(int i = 0; i < 7; ++i)
 | 
					    for(int i = 0; i < 7; ++i)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        schedule_free(relay->schedules[i]);
 | 
					        schedule_free(relay->schedules[i]);
 | 
				
			||||||
| 
						 | 
					@ -189,11 +195,14 @@ relay_free_list(relay_t **relays)
 | 
				
			||||||
cJSON*
 | 
					cJSON*
 | 
				
			||||||
relay_to_json(relay_t *relay)
 | 
					relay_to_json(relay_t *relay)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    relay_reload_active_schedule(relay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON *json = cJSON_CreateObject();
 | 
					    cJSON *json = cJSON_CreateObject();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON *json_number = cJSON_CreateNumber(relay->number);
 | 
					    cJSON *json_number = cJSON_CreateNumber(relay->number);
 | 
				
			||||||
    if(json_number == NULL)
 | 
					    if(json_number == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        LOG_DEBUG("failed to make number\n");
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -202,27 +211,41 @@ relay_to_json(relay_t *relay)
 | 
				
			||||||
    cJSON *json_name = cJSON_CreateString(relay->name);
 | 
					    cJSON *json_name = cJSON_CreateString(relay->name);
 | 
				
			||||||
    if(json_name == NULL)
 | 
					    if(json_name == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        LOG_DEBUG("failed to make name\n");
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_AddItemToObject(json, "name", json_name);
 | 
					    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)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        LOG_DEBUG("failed to get controller\n");
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char uuid_str[UUID_STR_LEN];
 | 
					    char uuid_str[UUID_STR_LEN];
 | 
				
			||||||
    uuid_unparse(controller->uid, uuid_str);
 | 
					    uuid_unparse(controller->uid, uuid_str);
 | 
				
			||||||
    cJSON *json_id = cJSON_CreateString(uuid_str);
 | 
					    cJSON *json_controller_id = cJSON_CreateString(uuid_str);
 | 
				
			||||||
    if(json_name == NULL)
 | 
					    if(json_controller_id == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        LOG_DEBUG("failed to make controller id\n");
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        return NULL;
 | 
					        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();
 | 
					    cJSON *json_tags = cJSON_CreateArray();
 | 
				
			||||||
    int *tags_ids = junction_tag_get_tags_for_relay_id(relay->id);
 | 
					    int *tags_ids = junction_tag_get_tags_for_relay_id(relay->id);
 | 
				
			||||||
| 
						 | 
					@ -295,12 +318,30 @@ relay_get_all()
 | 
				
			||||||
    return relay_db_select(stmt);
 | 
					    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_t**
 | 
				
			||||||
relay_get_by_controller_id(int controller_id)
 | 
					relay_get_by_controller_id(int controller_id)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    sqlite3_stmt *stmt;
 | 
					    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);
 | 
					    sqlite3_bind_int(stmt, 1, controller_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return relay_db_select(stmt);
 | 
					    return relay_db_select(stmt);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -347,6 +347,31 @@ schedule_get_by_id(int id)
 | 
				
			||||||
    return result;
 | 
					    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_t*
 | 
				
			||||||
schedule_get_by_uid(uuid_t uid)
 | 
					schedule_get_by_uid(uuid_t uid)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										7
									
								
								router.c
									
										
									
									
									
								
							
							
						
						
									
										7
									
								
								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/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/", 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);
 | 
					    router_register_endpoint("/api/v1/relays/", HTTP_METHOD_GET, api_v1_relays_GET);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue