dashboard: queue relay commands outside touch callback

This commit is contained in:
2026-06-09 23:28:24 -06:00
parent f975f27f4f
commit efdded605e
2 changed files with 79 additions and 19 deletions
@@ -1,6 +1,6 @@
#pragma once
static const char *DASHBOARD_VERSION = "0.0.11-no-overlap-polling";
static const char *DASHBOARD_VERSION = "0.0.12-queued-relay-commands";
// Update these if your Cargo ESP AP credentials are different.
static const char *CARGO_WIFI_SSID = "OverlandController";
@@ -33,6 +33,12 @@ static unsigned long last_metadata_poll_ms = 0;
static bool api_request_in_progress = false;
static bool fast_status_refresh_requested = false;
static bool relay_command_pending = false;
static String pending_relay_id;
static bool pending_relay_state = false;
static int pending_relay_index = -1;
static bool pending_relay_previous_state = false;
static String last_battery_text;
static String last_temps_text;
static String last_outputs_text;
@@ -53,11 +59,18 @@ static void request_fast_status_refresh()
static bool post_relay_state(const String &relay_id, bool state)
{
if (api_request_in_progress) {
Serial.println("Relay command skipped: API request already in progress");
return false;
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Relay command skipped: WiFi not connected");
return false;
}
api_request_in_progress = true;
DynamicJsonDocument request(256);
request["id"] = relay_id;
request["state"] = state;
@@ -82,6 +95,7 @@ static bool post_relay_state(const String &relay_id, bool state)
Serial.println(body);
http.end();
api_request_in_progress = false;
return code >= 200 && code < 300;
}
@@ -100,6 +114,12 @@ static void relay_button_event_cb(lv_event_t *event)
return;
}
if (relay_command_pending) {
set_label_text_if_changed(system_label, last_system_text, "Relay command already pending");
Serial.println("Relay tap ignored: command already pending");
return;
}
String relay_id = relay_ids[index];
bool previous_state = relay_states[index];
bool next_state = !previous_state;
@@ -108,29 +128,22 @@ static void relay_button_event_cb(lv_event_t *event)
update_relay_buttons();
update_outputs_label_from_relays();
String pending_text = "Sending relay command...\n";
pending_relay_id = relay_id;
pending_relay_state = next_state;
pending_relay_index = index;
pending_relay_previous_state = previous_state;
relay_command_pending = true;
String pending_text = "Queued relay command\n";
pending_text += relay_id;
pending_text += " -> ";
pending_text += on_off(next_state);
set_label_text_if_changed(system_label, last_system_text, pending_text);
bool ok = post_relay_state(relay_id, next_state);
if (ok) {
String optimistic_text = "Relay command sent\n";
optimistic_text += relay_id;
optimistic_text += " -> ";
optimistic_text += on_off(next_state);
optimistic_text += "\nRefreshing status...";
set_label_text_if_changed(system_label, last_system_text, optimistic_text);
request_fast_status_refresh();
} else {
relay_states[index] = previous_state;
update_relay_buttons();
update_outputs_label_from_relays();
set_label_text_if_changed(system_label, last_system_text, "Relay command failed");
}
Serial.print("Queued relay command: ");
Serial.print(relay_id);
Serial.print(" -> ");
Serial.println(on_off(next_state));
}
@@ -417,6 +430,52 @@ static void touch_test_event_cb(lv_event_t *event)
Serial.println(touch_count);
}
static void process_pending_relay_command()
{
if (!relay_command_pending) {
return;
}
if (api_request_in_progress) {
return;
}
String relay_id = pending_relay_id;
bool next_state = pending_relay_state;
int index = pending_relay_index;
bool previous_state = pending_relay_previous_state;
String sending_text = "Sending relay command\n";
sending_text += relay_id;
sending_text += " -> ";
sending_text += on_off(next_state);
set_label_text_if_changed(system_label, last_system_text, sending_text);
bool ok = post_relay_state(relay_id, next_state);
relay_command_pending = false;
pending_relay_id = "";
pending_relay_index = -1;
if (ok) {
String sent_text = "Relay command sent\n";
sent_text += relay_id;
sent_text += " -> ";
sent_text += on_off(next_state);
sent_text += "\nRefreshing status...";
set_label_text_if_changed(system_label, last_system_text, sent_text);
request_fast_status_refresh();
} else {
if (index >= 0 && index < relay_count) {
relay_states[index] = previous_state;
update_relay_buttons();
update_outputs_label_from_relays();
}
set_label_text_if_changed(system_label, last_system_text, "Relay command failed");
}
}
static void connect_wifi_if_needed()
{
if (WiFi.status() == WL_CONNECTED) {
@@ -743,6 +802,7 @@ void loop()
{
connect_wifi_if_needed();
update_wifi_label();
process_pending_relay_command();
poll_status_api();
delay(500);