add: models for controller and relay
add: first controller endpoints
This commit is contained in:
parent
b5a8523ae0
commit
1171ef22be
11 changed files with 951 additions and 20 deletions
endpoints
40
endpoints/api_v1_controllers.c
Normal file
40
endpoints/api_v1_controllers.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue