dashboard: queue latest relay target states
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
static const char *DASHBOARD_VERSION = "0.1.73-relay-command-priority";
|
static const char *DASHBOARD_VERSION = "0.1.74-relay-latest-state-queue";
|
||||||
|
|
||||||
// Update these if your Cargo ESP AP credentials are different.
|
// Update these if your Cargo ESP AP credentials are different.
|
||||||
static const char *CARGO_WIFI_SSID = "OverlandController";
|
static const char *CARGO_WIFI_SSID = "OverlandController";
|
||||||
|
|||||||
@@ -62,11 +62,9 @@ static bool metadata_refresh_requested = true;
|
|||||||
static bool was_wifi_connected = false;
|
static bool was_wifi_connected = false;
|
||||||
static bool overview_screen_created = false;
|
static bool overview_screen_created = false;
|
||||||
|
|
||||||
static bool relay_command_pending = false;
|
static bool relay_command_pending[6] = {false};
|
||||||
static String pending_relay_id;
|
static bool pending_relay_state[6] = {false};
|
||||||
static bool pending_relay_state = false;
|
static bool pending_relay_previous_state[6] = {false};
|
||||||
static int pending_relay_index = -1;
|
|
||||||
static bool pending_relay_previous_state = false;
|
|
||||||
|
|
||||||
static String last_battery_current_text;
|
static String last_battery_current_text;
|
||||||
static String last_battery_estimate_text;
|
static String last_battery_estimate_text;
|
||||||
@@ -529,7 +527,7 @@ static void relay_button_event_cb(lv_event_t *event)
|
|||||||
intptr_t index_value = reinterpret_cast<intptr_t>(lv_event_get_user_data(event));
|
intptr_t index_value = reinterpret_cast<intptr_t>(lv_event_get_user_data(event));
|
||||||
int index = static_cast<int>(index_value);
|
int index = static_cast<int>(index_value);
|
||||||
|
|
||||||
if (index < 0 || index >= relay_count || relay_command_pending) {
|
if (index < 0 || index >= relay_count) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,11 +537,13 @@ static void relay_button_event_cb(lv_event_t *event)
|
|||||||
relay_states[index] = next_state;
|
relay_states[index] = next_state;
|
||||||
update_relay_buttons();
|
update_relay_buttons();
|
||||||
|
|
||||||
pending_relay_id = relay_ids[index];
|
if (!relay_command_pending[index]) {
|
||||||
pending_relay_state = next_state;
|
pending_relay_previous_state[index] = previous_state;
|
||||||
pending_relay_index = index;
|
}
|
||||||
pending_relay_previous_state = previous_state;
|
|
||||||
relay_command_pending = true;
|
// Latest-state queue: rapid taps overwrite this relay's desired target.
|
||||||
|
pending_relay_state[index] = next_state;
|
||||||
|
relay_command_pending[index] = true;
|
||||||
|
|
||||||
String text = "Queued ";
|
String text = "Queued ";
|
||||||
text += relay_names[index];
|
text += relay_names[index];
|
||||||
@@ -555,26 +555,37 @@ static void relay_button_event_cb(lv_event_t *event)
|
|||||||
|
|
||||||
static void process_pending_relay_command()
|
static void process_pending_relay_command()
|
||||||
{
|
{
|
||||||
if (!relay_command_pending || api_request_in_progress) {
|
if (api_request_in_progress) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String relay_id = pending_relay_id;
|
int index = -1;
|
||||||
bool next_state = pending_relay_state;
|
for (int i = 0; i < relay_count; i++) {
|
||||||
int index = pending_relay_index;
|
if (relay_command_pending[i]) {
|
||||||
bool previous_state = pending_relay_previous_state;
|
index = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String relay_id = relay_ids[index];
|
||||||
|
bool next_state = pending_relay_state[index];
|
||||||
|
bool previous_state = pending_relay_previous_state[index];
|
||||||
|
|
||||||
|
// Clear before posting. If the user taps again after this command returns,
|
||||||
|
// the new desired state will be queued as a fresh command.
|
||||||
|
relay_command_pending[index] = false;
|
||||||
|
|
||||||
bool ok = post_relay_state(relay_id, next_state);
|
bool ok = post_relay_state(relay_id, next_state);
|
||||||
|
|
||||||
relay_command_pending = false;
|
|
||||||
pending_relay_id = "";
|
|
||||||
pending_relay_index = -1;
|
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
set_label_text_if_changed(system_status_label, last_system_status_text, "Relay command sent");
|
set_label_text_if_changed(system_status_label, last_system_status_text, "Relay command sent");
|
||||||
request_fast_status_refresh();
|
|
||||||
} else {
|
} else {
|
||||||
if (index >= 0 && index < relay_count) {
|
// Only revert if no newer desired state was queued for this relay.
|
||||||
|
if (!relay_command_pending[index] && index >= 0 && index < relay_count) {
|
||||||
relay_states[index] = previous_state;
|
relay_states[index] = previous_state;
|
||||||
update_relay_buttons();
|
update_relay_buttons();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user