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
				
			
		
							
								
								
									
										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;
 | 
			
		||||
                        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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue