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-10 20:04:55 +00:00
|
|
|
#include "../settings/triggers/time.h"
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-10 20:04:55 +00:00
|
|
|
void handleGetTimeTriggers(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
_dln("API :: get time triggers");
|
|
|
|
|
|
|
|
uint8_t count = timeTriggerSettings->triggerCount();
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(count) + JSON_OBJECT_SIZE(2) + count*JSON_OBJECT_SIZE(5));
|
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
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["timeofDay"] = trigger->timeOfDay;
|
|
|
|
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(2) + 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->transitionTime(root["transitionTime"]);
|
|
|
|
timeTriggerSettings->beginSetTriggers(jsonTriggers.size());
|
|
|
|
|
|
|
|
TimeTrigger trigger;
|
|
|
|
for (uint8_t i = 0; i < jsonTriggers.size(); i++)
|
|
|
|
{
|
|
|
|
JsonObject& jsonTrigger = jsonTriggers[i];
|
|
|
|
|
|
|
|
trigger.timeOfDay = jsonTrigger["timeOfDay"];
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-10 20:04:55 +00:00
|
|
|
|
|
|
|
server->on("/api/triggers/time", HTTP_GET, handleGetTimeTriggers);
|
|
|
|
server->on("/api/triggers/time", HTTP_POST, devNullRequest, devNullFileUpload, handlePostTimeTriggers);
|
2018-01-01 18:56:07 +00:00
|
|
|
}
|