dashboard: update relay UI optimistically

This commit is contained in:
2026-06-09 23:06:22 -06:00
parent cc4495c1fa
commit 24cf07a997
@@ -10,7 +10,7 @@
using namespace esp_panel::drivers;
using namespace esp_panel::board;
static const char *DASHBOARD_VERSION = "0.0.6-immediate-relay-refresh";
static const char *DASHBOARD_VERSION = "0.0.7-optimistic-relay-ui";
// Update these if your Cargo ESP AP credentials are different.
static const char *CARGO_WIFI_SSID = "OverlandController";
@@ -24,6 +24,7 @@ static lv_obj_t *outputs_label = nullptr;
static lv_obj_t *relay_buttons[6] = {nullptr};
static lv_obj_t *relay_button_labels[6] = {nullptr};
static String relay_ids[6];
static String relay_names[6];
static bool relay_states[6] = {false};
static int relay_count = 0;
static lv_obj_t *system_label = nullptr;
@@ -127,7 +128,12 @@ static void relay_button_event_cb(lv_event_t *event)
}
String relay_id = relay_ids[index];
bool next_state = !relay_states[index];
bool previous_state = relay_states[index];
bool next_state = !previous_state;
relay_states[index] = next_state;
update_relay_buttons();
update_outputs_label_from_relays();
String pending_text = "Sending relay command...\n";
pending_text += relay_id;
@@ -138,9 +144,6 @@ static void relay_button_event_cb(lv_event_t *event)
bool ok = post_relay_state(relay_id, next_state);
if (ok) {
relay_states[index] = next_state;
update_relay_buttons();
String optimistic_text = "Relay command sent\n";
optimistic_text += relay_id;
optimistic_text += " -> ";
@@ -151,10 +154,35 @@ static void relay_button_event_cb(lv_event_t *event)
force_status_poll_now();
poll_status_api();
} 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");
}
}
static void update_outputs_label_from_relays()
{
String outputs = "";
for (int i = 0; i < relay_count && i < 6; i++) {
outputs += String(i + 1);
outputs += ". ";
outputs += relay_names[i].length() > 0 ? relay_names[i] : relay_ids[i];
outputs += ": ";
outputs += on_off(relay_states[i]);
outputs += "\n";
}
if (outputs.length() == 0) {
outputs = "No outputs reported";
}
status_model.outputs_text = outputs;
set_label_text_if_changed(outputs_label, last_outputs_text, outputs);
}
static void update_relay_buttons()
{
lvgl_port_lock(-1);
@@ -344,6 +372,7 @@ static void parse_status_json(const String &body)
if (relay_count < 6 && enabled && available) {
relay_ids[relay_count] = String(id);
relay_names[relay_count] = String(name);
relay_states[relay_count] = state;
relay_count++;
}