41 lines
1.1 KiB
C
41 lines
1.1 KiB
C
|
#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/tag.h>
|
||
|
|
||
|
void
|
||
|
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||
|
{
|
||
|
(void)args;
|
||
|
(void)hm;
|
||
|
controller_t** all_controllers = controller_get_all();
|
||
|
|
||
|
cJSON *json = cJSON_CreateArray();
|
||
|
|
||
|
for(int i = 0; all_controllers[i] != NULL; ++i)
|
||
|
{
|
||
|
cJSON *json_controller = controller_to_json(all_controllers[i]);
|
||
|
|
||
|
cJSON_AddItemToArray(json, 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_list(all_controllers);
|
||
|
}
|