Skip to content

Commit

Permalink
Merge pull request #3828 from willmmiles/0_15-asyncwebserver-2.2.0-fe…
Browse files Browse the repository at this point in the history
…atures

ESPAsyncWebServer 2.2.0 + features
  • Loading branch information
blazoncek committed Mar 20, 2024
2 parents 5d3f22e + 8b6bf08 commit f1987b9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 118 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ lib_deps =
fastled/FastLED @ 3.6.0
IRremoteESP8266 @ 2.8.2
makuna/NeoPixelBus @ 2.7.5
https://github.com/Aircoookie/ESPAsyncWebServer.git @ ^2.1.0
https://github.com/Aircoookie/ESPAsyncWebServer.git @ ^2.2.0
# for I2C interface
;Wire
# ESP-NOW library
Expand Down
1 change: 0 additions & 1 deletion wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ void handleSerial();
void updateBaudRate(uint32_t rate);

//wled_server.cpp
String getFileContentType(String &filename);
void createEditHandler(bool enable);
void initServer();
void serveMessage(AsyncWebServerRequest* request, uint16_t code, const String& headl, const String& subl="", byte optionT=255);
Expand Down
14 changes: 4 additions & 10 deletions wled00/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ void updateFSInfo() {
#endif
}


#if defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM)
// caching presets in PSRAM may prevent occasional flashes seen when HomeAssitant polls WLED
// original idea by @akaricchi (https://github.com/Akaricchi)
Expand Down Expand Up @@ -420,26 +421,19 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){
DEBUG_PRINT(F("WS FileRead: ")); DEBUG_PRINTLN(path);
if(path.endsWith("/")) path += "index.htm";
if(path.indexOf(F("sec")) > -1) return false;
String contentType = getFileContentType(path);
if(request->hasArg(F("download"))) contentType = F("application/octet-stream");
/*String pathWithGz = path + ".gz";
if(WLED_FS.exists(pathWithGz)){
request->send(WLED_FS, pathWithGz, contentType);
return true;
}*/
#if defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM)
if (path.endsWith(FPSTR(getPresetsFileName()))) {
size_t psize;
const uint8_t *presets = getPresetCache(psize);
if (presets) {
AsyncWebServerResponse *response = request->beginResponse_P(200, contentType, presets, psize);
AsyncWebServerResponse *response = request->beginResponse_P(200, FPSTR(CONTENT_TYPE_JSON), presets, psize);
request->send(response);
return true;
}
}
#endif
if(WLED_FS.exists(path)) {
request->send(WLED_FS, path, contentType);
if(WLED_FS.exists(path) || WLED_FS.exists(path + ".gz")) {
request->send(WLED_FS, path, String(), request->hasArg(F("download")));
return true;
}
return false;
Expand Down
34 changes: 16 additions & 18 deletions wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ void serveJson(AsyncWebServerRequest* request)
}
#endif
else if (url.indexOf("pal") > 0) {
request->send_P(200, "application/json", JSON_palette_names); // contentType defined in AsyncJson-v6.h
request->send_P(200, FPSTR(CONTENT_TYPE_JSON), JSON_palette_names);
return;
}
else if (url.indexOf(F("cfg")) > 0 && handleFileRead(request, F("/cfg.json"))) {
Expand Down Expand Up @@ -1150,10 +1150,10 @@ bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient)
}
#endif

char buffer[2048]; // shoud be enough for 256 LEDs [RRGGBB] + all other text (9+25)
strcpy_P(buffer, PSTR("{\"leds\":["));
obuf = buffer; // assign buffer for oappnd() functions
olen = 9;
DynamicBuffer buffer(9 + (9*(1+(used/n))) + 7 + 5 + 6 + 5 + 6 + 5 + 2);
char* buf = buffer.data(); // assign buffer for oappnd() functions
strncpy_P(buffer.data(), PSTR("{\"leds\":["), buffer.size());
buf += 9; // sizeof(PSTR()) from last line

for (size_t i = 0; i < used; i += n)
{
Expand All @@ -1168,29 +1168,27 @@ bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient)
r = scale8(qadd8(w, r), strip.getBrightness()); //R, add white channel to RGB channels as a simple RGBW -> RGB map
g = scale8(qadd8(w, g), strip.getBrightness()); //G
b = scale8(qadd8(w, b), strip.getBrightness()); //B
olen += sprintf_P(obuf + olen, PSTR("\"%06X\","), RGBW32(r,g,b,0));
buf += sprintf_P(buf, PSTR("\"%06X\","), RGBW32(r,g,b,0));
}
olen -= 1;
oappend((const char*)F("],\"n\":"));
oappendi(n);
buf--; // remove last comma
buf += sprintf_P(buf, PSTR("],\"n\":%d"), n);
#ifndef WLED_DISABLE_2D
if (strip.isMatrix) {
oappend((const char*)F(",\"w\":"));
oappendi(Segment::maxWidth/n);
oappend((const char*)F(",\"h\":"));
oappendi(Segment::maxHeight/n);
buf += sprintf_P(buf, PSTR(",\"w\":%d"), Segment::maxWidth/n);
buf += sprintf_P(buf, PSTR(",\"h\":%d"), Segment::maxHeight/n);
}
#endif
oappend("}");
(*buf++) = '}';
(*buf++) = 0;

if (request) {
request->send(200, "application/json", buffer); // contentType defined in AsyncJson-v6.h
request->send(200, FPSTR(CONTENT_TYPE_JSON), toString(std::move(buffer)));
}
#ifdef WLED_ENABLE_WEBSOCKETS
else {
wsc->text(obuf, olen);
wsc->text(toString(std::move(buffer)));
}
#endif
obuf = nullptr;
#endif
return true;
}
#endif
6 changes: 2 additions & 4 deletions wled00/src/dependencies/json/AsyncJson-v6.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#define DYNAMIC_JSON_DOCUMENT_SIZE 16384
#endif

constexpr const char* JSON_MIMETYPE = "application/json";

/*
* Json Response
* */
Expand Down Expand Up @@ -66,7 +64,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {

AsyncJsonResponse(JsonDocument *ref, bool isArray=false) : _jsonBuffer(1), _isValid{false} {
_code = 200;
_contentType = JSON_MIMETYPE;
_contentType = FPSTR(CONTENT_TYPE_JSON);
if(isArray)
_root = ref->to<JsonArray>();
else
Expand All @@ -75,7 +73,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {

AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
_code = 200;
_contentType = JSON_MIMETYPE;
_contentType = FPSTR(CONTENT_TYPE_JSON);
if(isArray)
_root = _jsonBuffer.createNestedArray();
else
Expand Down
Loading

0 comments on commit f1987b9

Please sign in to comment.