ESP8266-based RGBW controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

144 lines
3.7 KiB

/*
* ESP8266 RGBW controller
* Copyright 2020 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/RGBWifi
*/
#include "./api.h"
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include "../debug.h"
#include "../global.h"
void handleSetStatic(AsyncWebServerRequest *request)
{
_dln("API :: set/static");
AsyncWebParameter* rParam = request->getParam("r");
AsyncWebParameter* gParam = request->getParam("g");
AsyncWebParameter* bParam = request->getParam("b");
AsyncWebParameter* wParam = request->getParam("w");
if (rParam == nullptr || gParam == nullptr || bParam == nullptr || wParam == nullptr)
{
request->send(400);
return;
}
AsyncWebParameter* fadeTimeParameter = request->getParam("fadeTime");
uint16_t fadeTime = fadeTimeParameter == nullptr ? 0 : fadeTimeParameter->value().toInt();
RgbwColor color(rParam->value().toInt(), gParam->value().toInt(), bParam->value().toInt(), wParam->value().toInt());
strip->setStatic(color, fadeTime);
request->send(200);
}
void handleSetRainbow(AsyncWebServerRequest *request)
{
_dln("API :: set/rainbow");
AsyncWebParameter* speedParam = request->getParam("speed");
AsyncWebParameter* lightnessParam = request->getParam("lightness");
uint16_t speed = speedParam == nullptr ? 0 : speedParam->value().toInt();
uint8_t lightness = lightnessParam == nullptr ? 0 : lightnessParam->value().toInt();
strip->setRainbow(speed, lightness);
request->send(200);
}
void handleGetStatus(AsyncWebServerRequest *request)
{
_dln("API :: get strip status");
DynamicJsonDocument jsonDocument(256);
switch (strip->getSetting())
{
case StripSetting::Static:
jsonDocument["setting"] = "static";
break;
case StripSetting::Rainbow:
case StripSetting::RainbowMoving:
jsonDocument["setting"] = "rainbow";
break;
}
JsonObject staticSettings = jsonDocument.createNestedObject("static");
JsonObject staticColor = staticSettings.createNestedObject("color");
RgbwColor color = strip->getStaticColor();
staticColor["r"] = color.R;
staticColor["g"] = color.G;
staticColor["b"] = color.B;
staticColor["w"] = color.W;
JsonObject rainbowSettings = jsonDocument.createNestedObject("rainbow");
rainbowSettings["speed"] = strip->getRainbowSpeed();
rainbowSettings["lightness"] = strip->getRainbowLightness();
AsyncResponseStream *response = request->beginResponseStream("application/json");
serializeJson(jsonDocument, *response);
request->send(response);
}
void handleSetStatus(AsyncWebServerRequest *request, JsonVariant &json)
{
_dln("API :: set strip status");
const char* jsonSetting = json["setting"];
if (strcmp(jsonSetting, "static") == 0)
{
JsonObject jsonColor = json["static"]["color"];
if (jsonColor.isNull())
{
request->send(400);
return;
}
uint16_t fadeTime = json["static"]["fadeTime"];
RgbwColor color(jsonColor["r"], jsonColor["g"], jsonColor["b"], jsonColor["w"]);
strip->setStatic(color, fadeTime);
request->send(200);
}
else if (strcmp(jsonSetting, "rainbow") == 0)
{
JsonObject jsonRainbow = json["rainbow"];
if (jsonRainbow.isNull())
{
request->send(400);
return;
}
strip->setRainbow(jsonRainbow["speed"], jsonRainbow["lightness"]);
request->send(200);
}
else
request->send(400);
}
void registerAPIRoutes(AsyncWebServer* server)
{
server->on("/api/set/static", HTTP_GET, handleSetStatic);
server->on("/api/set/rainbow", HTTP_GET, handleSetRainbow);
server->on("/api/strip/status", HTTP_GET, handleGetStatus);
AsyncCallbackJsonWebHandler* statusPostHandler = new AsyncCallbackJsonWebHandler("/api/strip/status", handleSetStatus, 256);
statusPostHandler->setMethod(HTTP_POST);
server->addHandler(statusPostHandler);
}