60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
// GET /api/devices/screen?mac=XX — Return the image URL for a device's current screen
|
|
|
|
#include "cJSON.h"
|
|
#include "esp_http_server.h"
|
|
|
|
#include "types.hpp"
|
|
#include "device.hpp"
|
|
|
|
internal esp_err_t api_devices_screen_handler(httpd_req_t *req)
|
|
{
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
|
|
// Extract mac query parameter
|
|
char mac[18] = {};
|
|
size_t buf_len = httpd_req_get_url_query_len(req) + 1;
|
|
if (buf_len > 1)
|
|
{
|
|
char query[64] = {};
|
|
if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK)
|
|
{
|
|
httpd_query_key_value(query, "mac", mac, sizeof(mac));
|
|
}
|
|
}
|
|
|
|
if (mac[0] == '\0')
|
|
{
|
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing 'mac' query param");
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
device_t *dev = find_device(mac);
|
|
if (!dev)
|
|
{
|
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Device not registered");
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
// Build image_url: /api/devices/screen.png?mac=XX
|
|
char image_url[64];
|
|
snprintf(image_url, sizeof(image_url), "/api/devices/screen.png?mac=%s", mac);
|
|
|
|
cJSON *resp = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(resp, "image_url", image_url);
|
|
|
|
const char *json = cJSON_PrintUnformatted(resp);
|
|
httpd_resp_sendstr(req, json);
|
|
|
|
free((void *)json);
|
|
cJSON_Delete(resp);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
internal const httpd_uri_t api_devices_screen_info_uri = {
|
|
.uri = "/api/devices/screen",
|
|
.method = HTTP_GET,
|
|
.handler = api_devices_screen_handler,
|
|
.user_ctx = NULL};
|