dashboard: prevent overlapping status polls

This commit is contained in:
2026-06-09 23:22:19 -06:00
parent c83a84848b
commit f975f27f4f
2 changed files with 18 additions and 6 deletions
@@ -1,6 +1,6 @@
#pragma once
static const char *DASHBOARD_VERSION = "0.0.10-api-timing";
static const char *DASHBOARD_VERSION = "0.0.11-no-overlap-polling";
// Update these if your Cargo ESP AP credentials are different.
static const char *CARGO_WIFI_SSID = "OverlandController";
@@ -30,6 +30,8 @@ static unsigned long last_wifi_attempt_ms = 0;
static unsigned long last_wifi_label_update_ms = 0;
static unsigned long last_status_poll_ms = 0;
static unsigned long last_metadata_poll_ms = 0;
static bool api_request_in_progress = false;
static bool fast_status_refresh_requested = false;
static String last_battery_text;
static String last_temps_text;
@@ -44,9 +46,9 @@ static String on_off(bool value)
return value ? "ON" : "OFF";
}
static void force_status_poll_now()
static void request_fast_status_refresh()
{
last_status_poll_ms = 0;
fast_status_refresh_requested = true;
}
static bool post_relay_state(const String &relay_id, bool state)
@@ -122,8 +124,7 @@ static void relay_button_event_cb(lv_event_t *event)
optimistic_text += "\nRefreshing status...";
set_label_text_if_changed(system_label, last_system_text, optimistic_text);
force_status_poll_now();
poll_status_api();
request_fast_status_refresh();
} else {
relay_states[index] = previous_state;
update_relay_buttons();
@@ -463,11 +464,19 @@ static void update_wifi_label()
static bool fetch_status_fields(const char *url, const char *label)
{
if (api_request_in_progress) {
Serial.print("Skipping ");
Serial.print(label);
Serial.println(": request already in progress");
return false;
}
if (WiFi.status() != WL_CONNECTED) {
set_label_text_if_changed(system_label, last_system_text, "API: waiting for WiFi");
return false;
}
api_request_in_progress = true;
unsigned long start_ms = millis();
Serial.print("GET ");
@@ -521,6 +530,7 @@ static bool fetch_status_fields(const char *url, const char *label)
Serial.println(body.length());
http.end();
api_request_in_progress = false;
return true;
}
@@ -547,6 +557,7 @@ static bool fetch_status_fields(const char *url, const char *label)
set_label_text_if_changed(system_label, last_system_text, system);
http.end();
api_request_in_progress = false;
return false;
}
@@ -557,7 +568,8 @@ static void poll_status_api()
return;
}
if (millis() - last_status_poll_ms >= 2000) {
if (fast_status_refresh_requested || millis() - last_status_poll_ms >= 2000) {
fast_status_refresh_requested = false;
last_status_poll_ms = millis();
fetch_status_fields(CARGO_API_FAST_STATUS_URL, "fast status");
}