/* * 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" #include "../settings/triggers/time.h" void handleGetSteps(AsyncWebServerRequest *request) { _dln("API :: get steps"); DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(17)); bool target = !request->hasParam("current"); JsonArray& root = jsonBuffer.createArray(); for (uint8_t step = 0; step < stepsSettings->count(); step++) root.add(stairs->get(step, target)); 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 ? startTime[step] : 0); request->send(200); } void handleGetTimeTriggers(AsyncWebServerRequest *request) { _dln("API :: get time triggers"); uint8_t count = timeTriggerSettings->triggerCount(); DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(count) + JSON_OBJECT_SIZE(3) + count*JSON_OBJECT_SIZE(5)); JsonObject& root = jsonBuffer.createObject(); root["enabled"] = timeTriggerSettings->enabled(); root["transitionTime"] = timeTriggerSettings->transitionTime(); JsonArray& jsonTriggers = root.createNestedArray("triggers"); for (uint8_t i = 0; i < count; i++) { TimeTrigger* trigger = timeTriggerSettings->trigger(i); JsonObject& jsonTrigger = jsonTriggers.createNestedObject(); jsonTrigger["time"] = trigger->time; jsonTrigger["daysOfWeek"] = trigger->daysOfWeek; jsonTrigger["brightness"] = trigger->brightness; jsonTrigger["triggerType"] = (uint8_t)trigger->triggerType; jsonTrigger["enabled"] = trigger->enabled; } AsyncResponseStream *response = request->beginResponseStream("application/json"); root.printTo(*response); request->send(response); } void handlePostTimeTriggers(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) { _dln("API :: post time triggers"); DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(10) + JSON_OBJECT_SIZE(3) + 10*JSON_OBJECT_SIZE(5) + 510); JsonObject& root = jsonBuffer.parseObject((char*)data); if (!root.success()) { request->send(400); return; } JsonArray& jsonTriggers = root["triggers"]; if (jsonTriggers.size() > 255) { request->send(400); return; } timeTriggerSettings->enabled(root["enabled"]); timeTriggerSettings->transitionTime(root["transitionTime"]); timeTriggerSettings->beginSetTriggers(jsonTriggers.size()); TimeTrigger trigger; for (uint8_t i = 0; i < jsonTriggers.size(); i++) { JsonObject& jsonTrigger = jsonTriggers[i]; trigger.time = jsonTrigger["time"]; trigger.daysOfWeek = jsonTrigger["daysOfWeek"]; trigger.brightness = jsonTrigger["brightness"]; trigger.triggerType = (TimeTriggerType)(uint8_t)jsonTrigger["triggerType"]; trigger.enabled = jsonTrigger["enabled"]; timeTriggerSettings->setTrigger(i, &trigger); } timeTriggerSettings->endSetTriggers(); timeTriggerSettings->write(); timeTriggerSettingsChanged = true; request->send(200); } void registerAPIRoutes(AsyncWebServer* server) { server->on("/api/steps", HTTP_GET, handleGetSteps); server->on("/api/steps", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSteps); server->on("/api/triggers/time", HTTP_GET, handleGetTimeTriggers); server->on("/api/triggers/time", HTTP_POST, devNullRequest, devNullFileUpload, handlePostTimeTriggers); }