Skip to content

Commit

Permalink
Cache, setSuppressOnChange, URL
Browse files Browse the repository at this point in the history
  • Loading branch information
fashberg committed Oct 11, 2020
1 parent b9cfe3d commit 134e3ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions WAdapter/WHtmlPages.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const static char HTTP_HEAD_BEGIN[] PROGMEM = R"=====(<!DOCTYPE html>
<title>%s - %s</title>
)=====";

const static char HTTP_HEAD_STYLE[] PROGMEM = "<link rel=\"stylesheet\" href=\"/css\" />";
const static char HTTP_HEAD_STYLE[] PROGMEM = "<link rel=\"stylesheet\" href=\"/css?%s\" />";

const static char HTTP_HEAD_SCRIPT[] PROGMEM = "<script src=\"/js\"></script>";
const static char HTTP_HEAD_SCRIPT[] PROGMEM = "<script src=\"/js?%s\"></script>";

const static char HTTP_HEAD_END[] PROGMEM = R"=====(
</head>
Expand Down
31 changes: 24 additions & 7 deletions WAdapter/WNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const char* PARAM_BODY PROGMEM = "body";
const char* HEADER_CT_ENCODING PROGMEM = "Content-Encoding";
const char* HEADER_CT_ENCODING_GZ PROGMEM = "gzip";

const char* HEADER_CACHECONTROL PROGMEM = "Cache-Control";
const char* HEADER_CACHECONTROL_900 PROGMEM = "max-age=900";

const char* DEFAULT_TOPIC_STATE PROGMEM = "properties";
const char* DEFAULT_TOPIC_SET PROGMEM = "set";

Expand Down Expand Up @@ -796,6 +799,22 @@ true ||
return;
}

void replyStaticHeader(AsyncWebServerRequest *request, AsyncWebServerResponse *response, bool gzip){
if (gzip) response->addHeader(HEADER_CT_ENCODING, HEADER_CT_ENCODING_GZ);
response->addHeader(HEADER_CACHECONTROL, HEADER_CACHECONTROL_900);
request->send(response);

}

void replyStatic(AsyncWebServerRequest *request, const String &contentType, const uint8_t * content, size_t len, bool gzip=false){
AsyncWebServerResponse *response = request->beginResponse_P(200, contentType, content, len);
replyStaticHeader(request, response, gzip);
}
void replyStatic(AsyncWebServerRequest *request, const String &contentType, PGM_P content, bool gzip=false){
AsyncWebServerResponse *response = request->beginResponse_P(200, contentType, content);
replyStaticHeader(request, response, gzip);
}

void handleOnRoot(AsyncWebServerRequest *request){
String url=request->url();
if (!checkAndLogWebAccess(request)) return;
Expand All @@ -821,14 +840,12 @@ true ||
} else if (url.equals(URI_FIRMWARE)){
handleHttpFirmwareUpdate(request);
} else if (url.equals(URI_CSS)){
request->send_P(200, CT_TEXT_CSS, PAGE_CSS);
replyStatic(request, CT_TEXT_CSS, PAGE_CSS);
} else if (url.equals(URI_JS)){
request->send_P(200, CT_TEXT_JS, PAGE_JS);
replyStatic(request, CT_TEXT_JS, PAGE_JS);
#ifndef MINIMAL
} else if (url.equals(URI_FAVICON)){
AsyncWebServerResponse *response = request->beginResponse_P(200, CT_IMAGE_ICON, favicon_ico_gz, favicon_ico_gz_len);
response->addHeader(HEADER_CT_ENCODING, HEADER_CT_ENCODING_GZ);
request->send(response);
replyStatic(request, CT_IMAGE_ICON, favicon_ico_gz, favicon_ico_gz_len, true);
#endif
} else if (url.startsWith(URI_THINGS)){
handleOnThings(request);
Expand Down Expand Up @@ -1454,8 +1471,8 @@ void handleHttpFirmwareUpdateFinished(AsyncWebServerRequest *request) {
if (!isWebServerRunning()) return nullptr;
page = request->beginResponseStream(CT_TEXT_HTML, 3096U);
page->printf_P(HTTP_HEAD_BEGIN, getIdx(), title);
page->print(FPSTR(HTTP_HEAD_SCRIPT));
page->print(FPSTR(HTTP_HEAD_STYLE));
page->printf_P(HTTP_HEAD_SCRIPT, getFirmwareVersion().c_str());
page->printf_P(HTTP_HEAD_STYLE, getFirmwareVersion().c_str());
if (HeaderAdditional!=nullptr) page->print(FPSTR(HeaderAdditional));
page->print(FPSTR(HTTP_HEAD_END));
return page;
Expand Down
16 changes: 8 additions & 8 deletions WAdapter/WProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ class WProperty {
return this->mqttSendChangedValues;
}

void setSilentChange(void) {
this->silentChange=true;
void setSuppressOnChange(bool val=true) {
this->suppressOnChange=val;
}

protected:
Expand All @@ -585,7 +585,7 @@ class WProperty {
this->changed = true;
this->requested = false;
this->valueRequesting = false;
this->silentChange = false;
this->suppressOnChange = false;
this->notifying = false;
this->readOnly = false;
this->atType = nullptr;
Expand Down Expand Up @@ -654,15 +654,15 @@ class WProperty {
bool changed;
bool requested;
bool valueRequesting;
bool silentChange;
bool suppressOnChange;
bool notifying;

WConstStringProperty* firstEnum = nullptr;

void notify() {
if (!valueRequesting && !silentChange) {
if (!valueRequesting) {
notifying = true;
if (onChange) {
if (onChange && !suppressOnChange) {
onChange(this);
}
if (deviceNotification) {
Expand All @@ -673,12 +673,12 @@ class WProperty {
}
notifying = false;
}
if (silentChange) silentChange=false;
if (suppressOnChange) suppressOnChange=false;
}


void afterSet() {
if (silentChange) silentChange=false;
if (suppressOnChange) suppressOnChange=false;
}


Expand Down

0 comments on commit 134e3ab

Please sign in to comment.