Browse Source

Fixed #2: Get status API

develop
Mark van Renswoude 1 year ago
parent
commit
d1bf5cde56
  1. 1509
      src/assets/js.h
  2. 6
      src/assets/version.h
  3. 92
      src/server/api.cpp
  4. 5
      src/strip.cpp
  5. 9
      src/strip.h
  6. 1
      web/src/i18n/en.js
  7. 3
      web/src/i18n/nl.js
  8. 23
      web/src/views/Status.vue

1509
src/assets/js.h
File diff suppressed because it is too large
View File

6
src/assets/version.h

@ -6,8 +6,8 @@ const uint8_t VersionMinor = 1;
const uint8_t VersionPatch = 0;
const uint8_t VersionMetadata = 0;
const char VersionBranch[] = "develop";
const char VersionSemVer[] = "0.1.0-develop.10";
const char VersionFullSemVer[] = "0.1.0-develop.10";
const char VersionCommitDate[] = "2020-10-09";
const char VersionSemVer[] = "0.1.0-develop.11";
const char VersionFullSemVer[] = "0.1.0-develop.11";
const char VersionCommitDate[] = "2020-10-14";
#endif

92
src/server/api.cpp

@ -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)
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)
}
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);
}

5
src/strip.cpp

@ -54,7 +54,8 @@ void Strip::setStatic(const RgbwColor color, uint16_t fadeTime)
_d("Strip :: setStatic, gamma corrected = "); _d(corrected.R); _d(","); _d(corrected.G); _d(","); _d(corrected.B); _d(","); _dln(corrected.W);
if (mSetting == StripSetting::Static && fadeTime > 0)
{
RgbwColor startColor = mStaticColor;
// Make sure we use the current color as a start, which is corrected as well
RgbwColor startColor = mBus->GetPixelColor(0);
mAnimator->StartAnimation(0, fadeTime,
[this, startColor, corrected](AnimationParam param)
@ -70,7 +71,7 @@ void Strip::setStatic(const RgbwColor color, uint16_t fadeTime)
mBus->ClearTo(corrected);
}
mStaticColor = corrected;
mStaticColor = color;
}

9
src/strip.h

@ -32,8 +32,8 @@ class Strip
RgbwColor mStaticColor;
uint16_t mRainbowSpeed;
uint8_t mRainbowLightness;
uint16_t mRainbowSpeed = 0;
uint8_t mRainbowLightness = 128;
uint16_t mRainbowShift;
void tickRainbow();
@ -43,6 +43,11 @@ class Strip
void setStatic(const RgbwColor color, uint16_t fadeTime);
void setRainbow(uint16_t speed, uint8_t lightness);
StripSetting getSetting() { return mSetting; }
RgbwColor getStaticColor() { return mStaticColor; }
uint16_t getRainbowSpeed() { return mRainbowSpeed; }
uint8_t getRainbowLightness() { return mRainbowLightness; }
};
#endif

1
web/src/i18n/en.js

@ -91,6 +91,7 @@ export default {
uploadFirmware: 'Error while uploading firmware',
setColor: 'Could not set color',
getStripStatus: 'Could not retrieve the strip status',
resetError: 'The system reports that it has been reset unexpectedly. The last power up status is:',
resetReason: {

3
web/src/i18n/nl.js

@ -4,7 +4,7 @@ export default {
firmwareVersion: 'Firmware versie: ',
copyright: 'Copyright © 2020 Mark van Renswoude',
loading: 'Een ogenblik geduld, bezig met laden...',
rebootPending: 'Het systeem wordt opnieuw opgestart, ververse deze pagina nadien',
rebootPending: 'Het systeem wordt opnieuw opgestart, ververs deze pagina nadien',
applyButton: 'Opslaan',
applyButtonSaving: 'Bezig met opslaan...',
@ -91,6 +91,7 @@ export default {
uploadFirmware: 'Fout tijdens bijwerken van firmware',
setColor: 'Kan kleur niet zetten',
getStripStatus: 'Kan de status van de strip niet ophalen',
resetError: 'Het systeem is onverwachts herstart. De laatste status is:',
resetReason: {

23
web/src/views/Status.vue

@ -97,20 +97,14 @@ export default {
{
const self = this;
// TODO load current settings (no API for it yet)
self.static = {
r: 0,
g: 0,
b: 0,
w: 0
};
self.rainbow = {
speed: 0,
lightness: 128
};
return Promise.resolve(true);
return axios.get('/api/strip/status')
.then(response =>
{
self.type = response.data.setting;
self.static = response.data.static.color;
self.rainbow = response.data.rainbow;
})
.catch(e => self.handleAPIError('error.getStripStatus', e));
},
@ -139,7 +133,6 @@ export default {
update()
{
console.log(this.type);
const self = this;
if (self.updating)

Loading…
Cancel
Save