Files
Calendink/Provider/main/api/devices/list.cpp

46 lines
1.2 KiB
C++

// GET /api/devices — List all registered devices
#include "cJSON.h"
#include "esp_http_server.h"
#include "types.hpp"
#include "device.hpp"
internal esp_err_t api_devices_get_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "default_layout", kDefaultLayoutXml);
cJSON *arr = cJSON_CreateArray();
cJSON_AddItemToObject(root, "devices", arr);
for (int i = 0; i < MAX_DEVICES; i++)
{
if (g_Devices[i].active)
{
cJSON *obj = cJSON_CreateObject();
cJSON_AddStringToObject(obj, "mac", g_Devices[i].mac);
cJSON_AddBoolToObject(obj, "has_layout", g_Devices[i].xml_layout[0] != '\0');
cJSON_AddStringToObject(obj, "xml_layout", g_Devices[i].xml_layout);
cJSON_AddItemToArray(arr, obj);
}
}
const char *json = cJSON_PrintUnformatted(root);
httpd_resp_sendstr(req, json);
free((void *)json);
cJSON_Delete(root);
return ESP_OK;
}
internal const httpd_uri_t api_devices_get_uri = {
.uri = "/api/devices",
.method = HTTP_GET,
.handler = api_devices_get_handler,
.user_ctx = NULL};