From ec687138ff107a3c6f26019ced5f7d3e030c7175 Mon Sep 17 00:00:00 2001 From: Tero Marttila Date: Thu, 26 Dec 2024 20:38:50 +0200 Subject: [PATCH] main: leds status api --- main/http_routes.c | 1 + main/http_routes.h | 1 + main/leds_http.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/main/http_routes.c b/main/http_routes.c index 1defa041..41c77d84 100644 --- a/main/http_routes.c +++ b/main/http_routes.c @@ -35,6 +35,7 @@ const struct http_route http_routes[] = { /* leds_http.c */ { "GET", "api/leds", leds_api_get, NULL }, + { "GET", "api/leds/status", leds_api_get_status, NULL }, { "POST", "api/leds", leds_api_post, NULL }, { "GET", "api/leds/test", leds_api_test_get, NULL }, diff --git a/main/http_routes.h b/main/http_routes.h index 17caa5c5..e1d9b0df 100644 --- a/main/http_routes.h +++ b/main/http_routes.h @@ -39,6 +39,7 @@ int artnet_api_outputs_handler(struct http_request *request, struct http_respons /* leds_http.c */ int leds_api_get(struct http_request *request, struct http_response *response, void *ctx); +int leds_api_get_status(struct http_request *request, struct http_response *response, void *ctx); int leds_api_post(struct http_request *request, struct http_response *response, void *ctx); int leds_api_test_get(struct http_request *request, struct http_response *response, void *ctx); diff --git a/main/leds_http.c b/main/leds_http.c index e2589b06..c289b5af 100644 --- a/main/leds_http.c +++ b/main/leds_http.c @@ -141,6 +141,71 @@ int leds_api_get(struct http_request *request, struct http_response *response, v return 0; } +struct leds_api_query { + struct leds_state *state; +}; + +int leds_api_query(struct http_request *request, struct leds_api_query *query) +{ + char *key, *value; + int index; + int err; + + while (!(err = http_request_query(request, &key, &value))) { + if (strcmp(key, "leds") == 0) { + if (sscanf(value, "leds%d", &index) <= 0) { + LOG_WARN("invalid ?leds=%s", value); + return HTTP_UNPROCESSABLE_ENTITY; + } else if (index > 0 && index <= LEDS_COUNT) { + query->state = &leds_states[index - 1]; + } else { + LOG_WARN("invalid ?leds=%s", value); + return HTTP_UNPROCESSABLE_ENTITY; + } + + return 0; + } + } + + if (!query->state) { + LOG_WARN("missing ?leds=..."); + return HTTP_UNPROCESSABLE_ENTITY; + } + + + return 0; +} + +static int leds_api_write_status(struct json_writer *w, void *ctx) +{ + struct leds_api_query *query = ctx; + + return JSON_WRITE_OBJECT(w, leds_api_write_object_status(w, query->state)); +} + +int leds_api_get_status(struct http_request *request, struct http_response *response, void *ctx) +{ + struct leds_api_query query = {}; + int err; + + if ((err = http_request_headers(request, NULL))) { + LOG_WARN("http_request_headers"); + return err; + } + + if ((err = leds_api_query(request, &query))) { + LOG_WARN("leds_api_query"); + return err; + } + + if ((err = write_http_response_json(response, leds_api_write_status, &query))) { + LOG_WARN("write_http_response_json -> leds_api_write_status"); + return err; + } + + return 0; +} + /* GET /api/leds/test */ static int leds_api_write_test_array(struct json_writer *w) {