dashboard: queue latest relay target states
This commit is contained in:
@@ -62,11 +62,9 @@ static bool metadata_refresh_requested = true;
|
||||
static bool was_wifi_connected = false;
|
||||
static bool overview_screen_created = 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 bool relay_command_pending[6] = {false};
|
||||
static bool pending_relay_state[6] = {false};
|
||||
static bool pending_relay_previous_state[6] = {false};
|
||||
|
||||
static String last_battery_current_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));
|
||||
int index = static_cast<int>(index_value);
|
||||
|
||||
if (index < 0 || index >= relay_count || relay_command_pending) {
|
||||
if (index < 0 || index >= relay_count) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -539,11 +537,13 @@ static void relay_button_event_cb(lv_event_t *event)
|
||||
relay_states[index] = next_state;
|
||||
update_relay_buttons();
|
||||
|
||||
pending_relay_id = relay_ids[index];
|
||||
pending_relay_state = next_state;
|
||||
pending_relay_index = index;
|
||||
pending_relay_previous_state = previous_state;
|
||||
relay_command_pending = true;
|
||||
if (!relay_command_pending[index]) {
|
||||
pending_relay_previous_state[index] = previous_state;
|
||||
}
|
||||
|
||||
// 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 ";
|
||||
text += relay_names[index];
|
||||
@@ -555,26 +555,37 @@ static void relay_button_event_cb(lv_event_t *event)
|
||||
|
||||
static void process_pending_relay_command()
|
||||
{
|
||||
if (!relay_command_pending || api_request_in_progress) {
|
||||
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;
|
||||
int index = -1;
|
||||
for (int i = 0; i < relay_count; i++) {
|
||||
if (relay_command_pending[i]) {
|
||||
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);
|
||||
|
||||
relay_command_pending = false;
|
||||
pending_relay_id = "";
|
||||
pending_relay_index = -1;
|
||||
|
||||
if (ok) {
|
||||
set_label_text_if_changed(system_status_label, last_system_status_text, "Relay command sent");
|
||||
request_fast_status_refresh();
|
||||
} 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;
|
||||
update_relay_buttons();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user