Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define PREFERENCES_LAST_SLEEP_TIME "last_sleep"
#define PREFERENCES_CONNECT_API_RETRY_COUNT "retry_count"
#define PREFERENCES_CONNECT_WIFI_RETRY_COUNT "wifi_retry"
#define PREFERENCES_ETAG_KEY "etag"

#define WIFI_CONNECTION_RSSI (-100)

Expand Down
34 changes: 34 additions & 0 deletions src/bl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ void bl_init(void)
need_to_refresh_display = 1;
preferences.putBool(PREFERENCES_DEVICE_REGISTRED_KEY, false);
Log.info("%s [%d]: Display TRMNL logo end\r\n", __FILE__, __LINE__);

// We're displaying the logo on the screen, so we reset the
// two preferences specifying the previous filename and the previous ETag
// to ensure we refresh this time to remove the logo.
preferences.putString(PREFERENCES_FILENAME_KEY, "");
preferences.putString(PREFERENCES_ETAG_KEY, "");
}

// Mount SPIFFS
Expand Down Expand Up @@ -1058,8 +1063,20 @@ static https_request_err_e downloadAndShow()

return HTTPS_UNABLE_TO_CONNECT;
}

// If we have an ETag, add it to the request.
String currentEtag = preferences.getString(PREFERENCES_ETAG_KEY, "");
if (currentEtag.length() > 0) {
https.addHeader("If-None-Match", currentEtag);
}

Log.info("%s [%d]: [HTTPS] GET..\r\n", __FILE__, __LINE__);
Log.info("%s [%d]: RSSI: %d\r\n", __FILE__, __LINE__, WiFi.RSSI());

// Ensure we look for the ETag header in the response.
const char* headerKeys[] = {"ETag", "Content-Type"};
https.collectHeaders(headerKeys, 2);

// start connection and send HTTP header
int httpCode = https.GET();

Expand All @@ -1076,6 +1093,23 @@ static https_request_err_e downloadAndShow()
// HTTP header has been send and Server response header has been handled
Log.error("%s [%d]: [HTTPS] GET... code: %d\r\n", __FILE__, __LINE__, httpCode);
Log.info("%s [%d]: RSSI: %d\r\n", __FILE__, __LINE__, WiFi.RSSI());

if (httpCode == HTTP_CODE_NOT_MODIFIED)
{
// The server used the ETag to determine that the content hasn't changed
// for this device and so returned a 304 Not Modified response.
// We don't need to update the screen.
Log.info("%s [%d]: Not modified. No need to download\r\n", __FILE__, __LINE__);
return HTTPS_SUCCES;
}

if (https.hasHeader("ETag")) {
// The server sent back an ETag header. Save it to include in the next request.
String etag = https.header("ETag");
Log.info("%s [%d]: ETag: %s\r\n", __FILE__, __LINE__, etag.c_str());
preferences.putString(PREFERENCES_ETAG_KEY, etag);
}

// file found at server
if (httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY)
{
Expand Down