Skip to content

Commit

Permalink
main: leds status api
Browse files Browse the repository at this point in the history
  • Loading branch information
SpComb committed Dec 26, 2024
1 parent a4a2e32 commit ec68713
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions main/http_routes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions main/http_routes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 65 additions & 0 deletions main/leds_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit ec68713

Please sign in to comment.