mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2026-08-18 18:32:30 +02:00
Add backoff in PageNavigation for better handling with bad connection
This commit is contained in:
@@ -230,7 +230,7 @@ bool NetworkClient::httpGetGzip(const String& url, uint8_t*& outData, size_t& ou
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (millis() - lastData > READ_TIMEOUT) {
|
if (millis() - lastData > READ_TIMEOUT) {
|
||||||
if (DEBUGING) {Serial.println("TIMEOUT waiting for data!");}
|
Serial.println("TIMEOUT waiting for data!");
|
||||||
aborting = true; // NEW: mark abnormal exit
|
aborting = true; // NEW: mark abnormal exit
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,8 +374,21 @@ bool showValues = false; // Show values HDT, SOG, DBT in navigation map
|
|||||||
getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update
|
getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update
|
||||||
getdisplay().setTextColor(commonData->fgcolor);
|
getdisplay().setTextColor(commonData->fgcolor);
|
||||||
|
|
||||||
|
// NEW: simple exponential backoff for 1 Hz polling (prevents connection-refused storms)
|
||||||
|
static uint32_t nextAllowedMs = 0;
|
||||||
|
static uint8_t failCount = 0;
|
||||||
|
|
||||||
|
uint32_t now = millis();
|
||||||
|
|
||||||
|
// NEW: if we are in backoff window, skip network call and use backup immediately
|
||||||
|
bool allowFetch = ((int32_t)(now - nextAllowedMs) >= 0);
|
||||||
|
|
||||||
// If a network connection to URL then load the navigation map
|
// If a network connection to URL then load the navigation map
|
||||||
if (net.fetchAndDecompressJson(url)) {
|
if (allowFetch && net.fetchAndDecompressJson(url)) {
|
||||||
|
|
||||||
|
// NEW: reset backoff on success
|
||||||
|
failCount = 0;
|
||||||
|
nextAllowedMs = now + 1000; // keep 1 Hz on success
|
||||||
|
|
||||||
auto& json = net.json(); // Extract JSON content
|
auto& json = net.json(); // Extract JSON content
|
||||||
int numPix = json["number_pixels"] | 0; // Read number of pixels
|
int numPix = json["number_pixels"] | 0; // Read number of pixels
|
||||||
@@ -397,7 +410,7 @@ bool showValues = false; // Show values HDT, SOG, DBT in navigation map
|
|||||||
size_t imgSize = numPix; // Calculate image size
|
size_t imgSize = numPix; // Calculate image size
|
||||||
uint8_t* imageData = (uint8_t*) heap_caps_malloc(imgSize, MALLOC_CAP_SPIRAM); // Allocate PSRAM for image
|
uint8_t* imageData = (uint8_t*) heap_caps_malloc(imgSize, MALLOC_CAP_SPIRAM); // Allocate PSRAM for image
|
||||||
if (!imageData) {
|
if (!imageData) {
|
||||||
LOG_DEBUG(GwLog::ERROR,"Error PageNavigation: PPSRAM alloc image buffer failed");
|
LOG_DEBUG(GwLog::ERROR,"Error PageNavigation: PSRAM alloc image buffer failed");
|
||||||
free(b64);
|
free(b64);
|
||||||
return PAGE_UPDATE;
|
return PAGE_UPDATE;
|
||||||
}
|
}
|
||||||
@@ -426,12 +439,25 @@ bool showValues = false; // Show values HDT, SOG, DBT in navigation map
|
|||||||
}
|
}
|
||||||
// If no network connection then use backup navigation map
|
// If no network connection then use backup navigation map
|
||||||
else{
|
else{
|
||||||
|
|
||||||
|
// NEW: update backoff only if we actually attempted a fetch (not when skipping due to backoff)
|
||||||
|
if (allowFetch) {
|
||||||
|
// NEW: exponential backoff: 1s,2s,4s,8s,16s,30s (capped)
|
||||||
|
if (failCount < 6) failCount++;
|
||||||
|
uint32_t backoffMs = 1000u << failCount;
|
||||||
|
if (backoffMs > 30000u) backoffMs = 30000u;
|
||||||
|
nextAllowedMs = now + backoffMs;
|
||||||
|
} else {
|
||||||
|
// NEW: we are currently backing off; do not increase failCount further
|
||||||
|
// nextAllowedMs stays unchanged
|
||||||
|
}
|
||||||
|
|
||||||
// Show backup image (backup navigation map)
|
// Show backup image (backup navigation map)
|
||||||
if (hasImageBackup) {
|
if (hasImageBackup) {
|
||||||
getdisplay().drawBitmap(0, 25, imageBackupData, imageBackupWidth, imageBackupHeight, commonData->fgcolor);
|
getdisplay().drawBitmap(0, 25, imageBackupData, imageBackupWidth, imageBackupHeight, commonData->fgcolor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show info: Connection lost when 5 page refreshes has a connection lost to the map server
|
// Show connection lost info when 5 page refreshes has a connection lost to the map server
|
||||||
// Short connection losts are uncritical
|
// Short connection losts are uncritical
|
||||||
if(lostCounter >= 5){
|
if(lostCounter >= 5){
|
||||||
getdisplay().setFont(&Ubuntu_Bold12pt8b);
|
getdisplay().setFont(&Ubuntu_Bold12pt8b);
|
||||||
@@ -444,7 +470,6 @@ bool showValues = false; // Show values HDT, SOG, DBT in navigation map
|
|||||||
lostCounter++; // Increment lost counter
|
lostCounter++; // Increment lost counter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ############### Draw Values ################
|
// ############### Draw Values ################
|
||||||
getdisplay().setFont(&Ubuntu_Bold12pt8b);
|
getdisplay().setFont(&Ubuntu_Bold12pt8b);
|
||||||
|
|
||||||
|
|||||||
+4
-8
@@ -21,10 +21,8 @@ lib_deps =
|
|||||||
ttlappalainen_NMEA2000=https://github.com/wellenvogel/NMEA2000.git#20251126
|
ttlappalainen_NMEA2000=https://github.com/wellenvogel/NMEA2000.git#20251126
|
||||||
ttlappalainen/NMEA0183 @ 1.10.1
|
ttlappalainen/NMEA0183 @ 1.10.1
|
||||||
ArduinoJson @ 6.18.5
|
ArduinoJson @ 6.18.5
|
||||||
; AsyncTCP-esphome @ 2.0.1
|
AsyncTCP-esphome @ 2.0.1
|
||||||
; ottowinter/ESPAsyncWebServer-esphome@2.0.1
|
ottowinter/ESPAsyncWebServer-esphome@2.0.1
|
||||||
AsyncTCP-esphome @ 2.1.1
|
|
||||||
ottowinter/ESPAsyncWebServer-esphome@3.4.0
|
|
||||||
FS
|
FS
|
||||||
Preferences
|
Preferences
|
||||||
ESPmDNS
|
ESPmDNS
|
||||||
@@ -36,10 +34,8 @@ lib_deps=
|
|||||||
ttlappalainen_NMEA2000=symlink://../NMEA2000
|
ttlappalainen_NMEA2000=symlink://../NMEA2000
|
||||||
ttlappalainen/NMEA0183 @ 1.10.1
|
ttlappalainen/NMEA0183 @ 1.10.1
|
||||||
ArduinoJson @ 6.18.5
|
ArduinoJson @ 6.18.5
|
||||||
; AsyncTCP-esphome @ 2.0.1
|
AsyncTCP-esphome @ 2.0.1
|
||||||
; ottowinter/ESPAsyncWebServer-esphome@2.0.1
|
ottowinter/ESPAsyncWebServer-esphome@2.0.1
|
||||||
AsyncTCP-esphome @ 2.1.1
|
|
||||||
ottowinter/ESPAsyncWebServer-esphome@3.4.0
|
|
||||||
FS
|
FS
|
||||||
Preferences
|
Preferences
|
||||||
ESPmDNS
|
ESPmDNS
|
||||||
|
|||||||
Reference in New Issue
Block a user