/* * Stairs * Copyright 2017 (c) Mark van Renswoude * * https://git.x2software.net/pub/Stairs */ #include "api.h" #include #include #include #include "shared.h" #include "../assets/version.h" #include "../debug.h" #include "../global.h" #include "../settings/connection.h" void handleGetSteps(AsyncWebServerRequest *request) { _dln("API :: get steps"); DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(17)); JsonArray& root = jsonBuffer.createArray(); for (uint8_t step = 0; step < stepsSettings->count(); step++) root.add(stairs->get(step)); AsyncResponseStream *response = request->beginResponseStream("application/json"); root.printTo(*response); request->send(response); } void handlePostSteps(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) { _dln("API :: post steps"); DynamicJsonBuffer jsonBuffer(2*JSON_ARRAY_SIZE(17) + JSON_OBJECT_SIZE(3) + 130); JsonObject& root = jsonBuffer.parseObject((char*)data); if (!root.success()) { request->send(400); return; } uint16_t transitionTime = root["transitionTime"]; JsonArray& values = root["values"]; JsonArray& startTime = root["startTime"]; size_t startTimeCount = startTime.size(); size_t valueCount = values.size(); if (valueCount > stepsSettings->count()) valueCount = stepsSettings->count(); for (uint8_t step = 0; step < valueCount; step++) stairs->set(step, values[step], transitionTime, step < startTimeCount ? 1 : 0); request->send(200); } void registerAPIRoutes(AsyncWebServer* server) { server->on("/api/steps", HTTP_GET, handleGetSteps); server->on("/api/steps", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSteps); }