2020-05-06 20:49:22 +00:00
|
|
|
#include <cJSON.h>
|
2020-05-20 23:33:18 +00:00
|
|
|
#include <macros.h>
|
2020-05-06 20:49:22 +00:00
|
|
|
#include <constants.h>
|
|
|
|
#include <endpoints/api_v1_controllers.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <models/controller.h>
|
|
|
|
#include <models/tag.h>
|
|
|
|
|
|
|
|
void
|
2020-05-20 23:33:18 +00:00
|
|
|
api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
2020-05-06 20:49:22 +00:00
|
|
|
{
|
|
|
|
(void)hm;
|
|
|
|
|
|
|
|
uuid_t target_uid;
|
|
|
|
if(uuid_parse(args[0].value.v_str, target_uid))
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("failed to unparse uid\n");
|
|
|
|
|
|
|
|
static const char content[] = "given id was invalid";
|
|
|
|
response->status_code = 400;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-06 20:49:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller_t* controller = controller_get_by_uid(target_uid);
|
|
|
|
|
|
|
|
if(!controller)
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
|
|
|
|
|
|
|
|
static const char content[] = "no controller for id found";
|
|
|
|
response->status_code = 404;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-06 20:49:22 +00:00
|
|
|
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");
|
2020-05-20 23:33:18 +00:00
|
|
|
|
|
|
|
static const char content[] = "failed to print json for relays";
|
|
|
|
response->status_code = 500;
|
|
|
|
response->content_type = "text/plain";
|
|
|
|
response->content_length = STRLEN(content);;
|
|
|
|
response->content = content;
|
|
|
|
response->alloced_content = false;
|
2020-05-06 20:49:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-20 23:33:18 +00:00
|
|
|
response->status_code = 200;
|
|
|
|
response->content_type = "application/json";
|
|
|
|
response->content_length = strlen(json_str);
|
|
|
|
response->content = json_str;
|
|
|
|
response->alloced_content = true;
|
2020-05-06 20:49:22 +00:00
|
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
|
|
relay_free_list(all_relays);
|
|
|
|
controller_free(controller);
|
|
|
|
}
|