172 lines
4.9 KiB
C
172 lines
4.9 KiB
C
#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;
|
|
}
|