Skip to content

Commit

Permalink
move to x-inkplate-next-refresh custom header
Browse files Browse the repository at this point in the history
Fixes issues while developing with Firefox not refreshing
the image, and simplifies parsing on the ESP.
  • Loading branch information
ugomeda committed Feb 11, 2025
1 parent a7272cb commit 9f7580c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
37 changes: 12 additions & 25 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#define MAX_ETAG_LENGTH 255

#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error \
"Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#define uS_TO_S_FACTOR 1000000 // Conversion factor for micro seconds to seconds
Expand Down Expand Up @@ -85,20 +84,6 @@ void display_status(const char *message, bool ignore_silent = false) {
status_displayed = true;
}

uint16_t extract_max_age(const char *header) {
const char *key = "max-age=";
char *found = strstr(header, key);
if (found) {
int max_age;
if (sscanf(found + strlen(key), "%d", &max_age) == 1) {
if (max_age >= 0 && max_age <= UINT16_MAX) {
return (uint16_t)max_age;
}
}
}
return 0;
}

void load_and_display_image() {
// FIXME Check content type
// FIXME Display errors if needed
Expand All @@ -117,7 +102,7 @@ void load_and_display_image() {
if (strlen(etag)) {
http.addHeader("If-None-Match", etag);
}
const char *headerkeys[] = {"content-type", "etag", "cache-control"};
const char *headerkeys[] = {"content-type", "etag", "x-inkplate-next-refresh"};
http.collectHeaders(headerkeys, 3);

// Launch request and check result
Expand All @@ -126,10 +111,11 @@ void load_and_display_image() {
Serial.println("[HTTP] Server returned 304, nothing to update");

// handle the delay
if (http.hasHeader("cache-control")) {
uint16_t max_age = extract_max_age(http.header("cache-control").c_str());
if (max_age > 0) {
sleep_duration = max_age;
if (http.hasHeader("x-inkplate-next-refresh")) {
String next_refresh_header = http.header("x-inkplate-next-refresh");
int next_refresh = atoi(next_refresh_header.c_str());
if (next_refresh > 0 && next_refresh <= UINT16_MAX) {
sleep_duration = next_refresh;
}
}
} else if (httpCode == HTTP_CODE_OK) {
Expand All @@ -150,10 +136,11 @@ void load_and_display_image() {
}

// handle the delay
if (http.hasHeader("cache-control")) {
uint16_t max_age = extract_max_age(http.header("cache-control").c_str());
if (max_age > 0) {
sleep_duration = max_age;
if (http.hasHeader("x-inkplate-next-refresh")) {
String next_refresh_header = http.header("x-inkplate-next-refresh");
int next_refresh = atoi(next_refresh_header.c_str());
if (next_refresh > 0 && next_refresh <= UINT16_MAX) {
sleep_duration = next_refresh;
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion inkplate_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def get(self, request: Request) -> Response:
)
headers = {
"etag": etag,
"Cache-Control": f"max-age={get_display(request).refresh_interval_sec}",
"x-inkplate-next-refresh": str(get_display(request).refresh_interval_sec),
}

# Tell the display to avoid a refresh if the image did not change
Expand Down

0 comments on commit 9f7580c

Please sign in to comment.