|
|
|
@ -5,6 +5,8 @@
@@ -5,6 +5,8 @@
|
|
|
|
|
* https://git.x2software.net/pub/RGBWifi
|
|
|
|
|
*/ |
|
|
|
|
#include "./api.h" |
|
|
|
|
#include <ArduinoJson.h> |
|
|
|
|
#include <AsyncJson.h> |
|
|
|
|
#include "../debug.h" |
|
|
|
|
#include "../global.h" |
|
|
|
|
|
|
|
|
@ -28,9 +30,8 @@ void handleSetStatic(AsyncWebServerRequest *request)
@@ -28,9 +30,8 @@ void handleSetStatic(AsyncWebServerRequest *request)
|
|
|
|
|
uint16_t fadeTime = fadeTimeParameter == nullptr ? 0 : fadeTimeParameter->value().toInt(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RgbwColor* color = new RgbwColor(rParam->value().toInt(), gParam->value().toInt(), bParam->value().toInt(), wParam->value().toInt()); |
|
|
|
|
strip->setStatic(*color, fadeTime); |
|
|
|
|
delete color; |
|
|
|
|
RgbwColor color(rParam->value().toInt(), gParam->value().toInt(), bParam->value().toInt(), wParam->value().toInt()); |
|
|
|
|
strip->setStatic(color, fadeTime); |
|
|
|
|
|
|
|
|
|
request->send(200); |
|
|
|
|
} |
|
|
|
@ -51,8 +52,93 @@ void handleSetRainbow(AsyncWebServerRequest *request)
@@ -51,8 +52,93 @@ void handleSetRainbow(AsyncWebServerRequest *request)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
} |