2018-01-01 18:56:07 +00:00
|
|
|
/*
|
|
|
|
* Stairs
|
|
|
|
* Copyright 2017 (c) Mark van Renswoude
|
|
|
|
*
|
|
|
|
* https://git.x2software.net/pub/Stairs
|
|
|
|
*/
|
|
|
|
#include "api.h"
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <IPAddress.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
2018-01-04 21:53:16 +00:00
|
|
|
#include "shared.h"
|
2018-01-01 18:56:07 +00:00
|
|
|
#include "../assets/version.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../global.h"
|
|
|
|
#include "../settings/connection.h"
|
|
|
|
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
void handleGetSteps(AsyncWebServerRequest *request)
|
2018-01-01 18:56:07 +00:00
|
|
|
{
|
2018-01-04 21:53:16 +00:00
|
|
|
_dln("API :: get steps");
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(17));
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-05 19:02:30 +00:00
|
|
|
bool target = !request->hasParam("current");
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
JsonArray& root = jsonBuffer.createArray();
|
|
|
|
for (uint8_t step = 0; step < stepsSettings->count(); step++)
|
2018-01-05 19:02:30 +00:00
|
|
|
root.add(stairs->get(step, target));
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
root.printTo(*response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
void handlePostSteps(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
|
2018-01-01 18:56:07 +00:00
|
|
|
{
|
2018-01-04 21:53:16 +00:00
|
|
|
_dln("API :: post steps");
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
uint16_t transitionTime = root["transitionTime"];
|
|
|
|
JsonArray& values = root["values"];
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
JsonArray& startTime = root["startTime"];
|
|
|
|
size_t startTimeCount = startTime.size();
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
size_t valueCount = values.size();
|
|
|
|
if (valueCount > stepsSettings->count())
|
|
|
|
valueCount = stepsSettings->count();
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
for (uint8_t step = 0; step < valueCount; step++)
|
2018-01-05 19:02:30 +00:00
|
|
|
stairs->set(step, values[step], transitionTime, step < startTimeCount ? startTime[step] : 0);
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
request->send(200);
|
2018-01-01 18:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerAPIRoutes(AsyncWebServer* server)
|
|
|
|
{
|
2018-01-04 21:53:16 +00:00
|
|
|
server->on("/api/steps", HTTP_GET, handleGetSteps);
|
|
|
|
server->on("/api/steps", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSteps);
|
2018-01-01 18:56:07 +00:00
|
|
|
}
|