mirror of
https://github.com/wled/WLED.git
synced 2025-04-25 23:37:18 +00:00
Adding backwards compatibility.
This commit is contained in:
parent
77d8a8e43d
commit
e70e1b8ad7
@ -97,7 +97,7 @@ void handleIR();
|
|||||||
#include "FX.h"
|
#include "FX.h"
|
||||||
|
|
||||||
void deserializeSegment(JsonObject elem, byte it);
|
void deserializeSegment(JsonObject elem, byte it);
|
||||||
uint8_t deserializeState(JsonObject root);
|
bool deserializeState(JsonObject root);
|
||||||
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset = false, bool segmentBounds = true, uint8_t versionAPI = 1);
|
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset = false, bool segmentBounds = true, uint8_t versionAPI = 1);
|
||||||
void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true);
|
void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true);
|
||||||
void serializeInfo(JsonObject root);
|
void serializeInfo(JsonObject root);
|
||||||
|
@ -165,11 +165,10 @@ void deserializeSegment(JsonObject elem, byte it)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t deserializeState(JsonObject root)
|
bool deserializeState(JsonObject root)
|
||||||
{
|
{
|
||||||
strip.applyToAllSelected = false;
|
strip.applyToAllSelected = false;
|
||||||
bool stateResponse = root[F("v")] | false;
|
bool stateResponse = root[F("v")] | false;
|
||||||
uint8_t versionAPI = root["rev"] | 1;
|
|
||||||
|
|
||||||
bri = root["bri"] | bri;
|
bri = root["bri"] | bri;
|
||||||
|
|
||||||
@ -279,7 +278,7 @@ uint8_t deserializeState(JsonObject root)
|
|||||||
deletePreset(ps);
|
deletePreset(ps);
|
||||||
}
|
}
|
||||||
ps = root["ps"] | -1; //load preset (clears state request!)
|
ps = root["ps"] | -1; //load preset (clears state request!)
|
||||||
if (ps >= 0) {applyPreset(ps); return stateResponse ? versionAPI : 0;}
|
if (ps >= 0) {applyPreset(ps); return stateResponse;}
|
||||||
|
|
||||||
//HTTP API commands
|
//HTTP API commands
|
||||||
const char* httpwin = root["win"];
|
const char* httpwin = root["win"];
|
||||||
@ -298,7 +297,7 @@ uint8_t deserializeState(JsonObject root)
|
|||||||
|
|
||||||
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);
|
colorUpdated(noNotification ? NOTIFIER_CALL_MODE_NO_NOTIFY : NOTIFIER_CALL_MODE_DIRECT_CHANGE);
|
||||||
|
|
||||||
return stateResponse ? versionAPI : 0;
|
return stateResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset, bool segmentBounds, uint8_t versionAPI)
|
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset, bool segmentBounds, uint8_t versionAPI)
|
||||||
|
@ -83,7 +83,8 @@ void initServer()
|
|||||||
});
|
});
|
||||||
|
|
||||||
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request) {
|
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request) {
|
||||||
uint8_t vAPI = 0;
|
bool verboseResponse = false;
|
||||||
|
uint8_t vAPI = 1;
|
||||||
{ //scope JsonDocument so it releases its buffer
|
{ //scope JsonDocument so it releases its buffer
|
||||||
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
|
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
|
||||||
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
||||||
@ -91,11 +92,15 @@ void initServer()
|
|||||||
if (error || root.isNull()) {
|
if (error || root.isNull()) {
|
||||||
request->send(400, "application/json", F("{\"error\":9}")); return;
|
request->send(400, "application/json", F("{\"error\":9}")); return;
|
||||||
}
|
}
|
||||||
|
if (root.containsKey("rev"))
|
||||||
|
{
|
||||||
|
vAPI = root["rev"] | 1;
|
||||||
|
}
|
||||||
fileDoc = &jsonBuffer; // used for applying presets (presets.cpp)
|
fileDoc = &jsonBuffer; // used for applying presets (presets.cpp)
|
||||||
vAPI = deserializeState(root);
|
verboseResponse = deserializeState(root);
|
||||||
fileDoc = nullptr;
|
fileDoc = nullptr;
|
||||||
}
|
}
|
||||||
if (vAPI>0) { //if JSON contains "v"
|
if (verboseResponse) { //if JSON contains "v"
|
||||||
serveJson(request,vAPI); return;
|
serveJson(request,vAPI); return;
|
||||||
}
|
}
|
||||||
request->send(200, "application/json", F("{\"success\":true}"));
|
request->send(200, "application/json", F("{\"success\":true}"));
|
||||||
|
@ -9,6 +9,10 @@ uint16_t wsLiveClientId = 0;
|
|||||||
unsigned long wsLastLiveTime = 0;
|
unsigned long wsLastLiveTime = 0;
|
||||||
//uint8_t* wsFrameBuffer = nullptr;
|
//uint8_t* wsFrameBuffer = nullptr;
|
||||||
uint8_t vAPI = 2;
|
uint8_t vAPI = 2;
|
||||||
|
struct client_api {
|
||||||
|
uint32_t c = 0;
|
||||||
|
uint8_t vAPI = 1;
|
||||||
|
} ClientApis[DEFAULT_MAX_WS_CLIENTS];
|
||||||
|
|
||||||
#define WS_LIVE_INTERVAL 40
|
#define WS_LIVE_INTERVAL 40
|
||||||
|
|
||||||
@ -16,11 +20,25 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
|
|||||||
{
|
{
|
||||||
if(type == WS_EVT_CONNECT){
|
if(type == WS_EVT_CONNECT){
|
||||||
//client connected
|
//client connected
|
||||||
|
for (uint8_t i=0; i<DEFAULT_MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ClientApis[i].c) continue; // used slot
|
||||||
|
ClientApis[i].c = client->id();
|
||||||
|
ClientApis[i].vAPI = 1;
|
||||||
|
DEBUG_PRINTF("New WS client [%d]: %ld\n", (int)i, client->id());
|
||||||
|
break;
|
||||||
|
}
|
||||||
sendDataWs(client);
|
sendDataWs(client);
|
||||||
//client->ping();
|
//client->ping();
|
||||||
} else if(type == WS_EVT_DISCONNECT){
|
} else if(type == WS_EVT_DISCONNECT){
|
||||||
//client disconnected
|
//client disconnected
|
||||||
if (client->id() == wsLiveClientId) wsLiveClientId = 0;
|
if (client->id() == wsLiveClientId) wsLiveClientId = 0;
|
||||||
|
for (uint8_t i=0; i<DEFAULT_MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ClientApis[i].c != client->id()) continue;
|
||||||
|
ClientApis[i].c = 0; // clear slot
|
||||||
|
ClientApis[i].vAPI = 1;
|
||||||
|
DEBUG_PRINTF("Removed WS client [%d]: %ld\n", (int)i, client->id());
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else if(type == WS_EVT_DATA){
|
} else if(type == WS_EVT_DATA){
|
||||||
//data packet
|
//data packet
|
||||||
AwsFrameInfo * info = (AwsFrameInfo*)arg;
|
AwsFrameInfo * info = (AwsFrameInfo*)arg;
|
||||||
@ -28,7 +46,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
|
|||||||
//the whole message is in a single frame and we got all of it's data (max. 1450byte)
|
//the whole message is in a single frame and we got all of it's data (max. 1450byte)
|
||||||
if(info->opcode == WS_TEXT)
|
if(info->opcode == WS_TEXT)
|
||||||
{
|
{
|
||||||
uint8_t verboseResponse = 0;
|
bool verboseResponse = false;
|
||||||
{ //scope JsonDocument so it releases its buffer
|
{ //scope JsonDocument so it releases its buffer
|
||||||
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
|
DynamicJsonDocument jsonBuffer(JSON_BUFFER_SIZE);
|
||||||
DeserializationError error = deserializeJson(jsonBuffer, data, len);
|
DeserializationError error = deserializeJson(jsonBuffer, data, len);
|
||||||
@ -39,9 +57,16 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
|
|||||||
{
|
{
|
||||||
wsLiveClientId = root["lv"] ? client->id() : 0;
|
wsLiveClientId = root["lv"] ? client->id() : 0;
|
||||||
}
|
}
|
||||||
|
if (root.containsKey("rev"))
|
||||||
|
{
|
||||||
|
for (uint8_t i=0; i<DEFAULT_MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ClientApis[i].c != client->id()) continue;
|
||||||
|
ClientApis[i].vAPI = root["rev"];
|
||||||
|
DEBUG_PRINTF("API for WS client [%d]: %d\n", (int)i, (int)ClientApis[i].vAPI);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
verboseResponse = deserializeState(root);
|
verboseResponse = deserializeState(root);
|
||||||
if (verboseResponse) vAPI = verboseResponse;
|
|
||||||
}
|
}
|
||||||
if (verboseResponse || millis() - lastInterfaceUpdate < 1900) sendDataWs(client); //update if it takes longer than 100ms until next "broadcast"
|
if (verboseResponse || millis() - lastInterfaceUpdate < 1900) sendDataWs(client); //update if it takes longer than 100ms until next "broadcast"
|
||||||
}
|
}
|
||||||
@ -81,7 +106,14 @@ void sendDataWs(AsyncWebSocketClient * client)
|
|||||||
{ //scope JsonDocument so it releases its buffer
|
{ //scope JsonDocument so it releases its buffer
|
||||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||||
JsonObject state = doc.createNestedObject("state");
|
JsonObject state = doc.createNestedObject("state");
|
||||||
if (vAPI>1) state["rev"] = 2;
|
if (client) {
|
||||||
|
for (uint8_t i=0; i<DEFAULT_MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ClientApis[i].c != client->id()) continue;
|
||||||
|
state["rev"] = ClientApis[i].vAPI;
|
||||||
|
DEBUG_PRINTF("Actual API used [%d]: %d\n", (int)i, (int)ClientApis[i].vAPI);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
serializeState(state);
|
serializeState(state);
|
||||||
JsonObject info = doc.createNestedObject("info");
|
JsonObject info = doc.createNestedObject("info");
|
||||||
serializeInfo(info);
|
serializeInfo(info);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user