dashboard: improve relay command responsiveness

This commit is contained in:
2026-06-15 14:50:27 -06:00
parent e2d713f8c4
commit b93edc248d
2 changed files with 43 additions and 6 deletions
@@ -479,21 +479,42 @@ static bool post_relay_state(const String &relay_id, bool state)
Serial.println(payload);
HTTPClient http;
http.setTimeout(5000);
http.setTimeout(1200);
http.begin(CARGO_API_RELAY_SET_URL);
http.addHeader("Content-Type", "application/json");
unsigned long start_ms = millis();
int code = http.POST(payload);
String body = http.getString();
Serial.print("Relay HTTP code: ");
Serial.println(code);
Serial.print(code);
Serial.print(" total=");
Serial.print(millis() - start_ms);
Serial.println("ms");
Serial.println(body);
bool ok = code >= 200 && code < 300;
if (ok && body.length() > 0) {
DynamicJsonDocument response(512);
DeserializationError error = deserializeJson(response, body);
if (!error && response["state"].is<bool>()) {
bool applied_state = response["state"].as<bool>();
const char *applied_id = response["id"] | "";
for (int i = 0; i < relay_count; i++) {
if (relay_ids[i] == String(applied_id)) {
relay_states[i] = applied_state;
break;
}
}
}
}
http.end();
api_request_in_progress = false;
return code >= 200 && code < 300;
return ok;
}
static void relay_button_event_cb(lv_event_t *event)
@@ -1030,7 +1051,7 @@ static bool fetch_status_fields(const char *url, const char *label)
unsigned long start_ms = millis();
HTTPClient http;
http.setTimeout(5000);
http.setTimeout(900);
http.begin(url);
int code = http.GET();
@@ -1066,7 +1087,7 @@ static void poll_status_api()
return;
}
if (fast_status_refresh_requested || millis() - last_status_poll_ms >= 2000) {
if (fast_status_refresh_requested || millis() - last_status_poll_ms >= 1500) {
fast_status_refresh_requested = false;
last_status_poll_ms = millis();
fetch_status_fields(CARGO_API_FAST_STATUS_URL, "fast status");
@@ -1444,5 +1465,5 @@ void loop()
process_pending_relay_command();
poll_status_api();
delay(250);
delay(50);
}
@@ -3829,6 +3829,8 @@ void handleSelectBms() {
void handleSetRelayPost() {
unsigned long startMs = millis();
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, server.arg("plain"));
@@ -3840,20 +3842,34 @@ void handleSetRelayPost() {
String relayId = doc["id"] | "";
bool state = doc["state"] | false;
unsigned long applyStartMs = millis();
if (!setRelayById(relayId, state)) {
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
return;
}
unsigned long appliedMs = millis() - applyStartMs;
DynamicJsonDocument response(512);
response["type"] = "relay_response";
response["ok"] = true;
response["id"] = relayId;
response["state"] = state;
response["applied_ms"] = appliedMs;
response["total_ms"] = millis() - startMs;
String output;
serializeJson(response, output);
server.send(200, "application/json", output);
Serial.print("Relay ");
Serial.print(relayId);
Serial.print(" -> ");
Serial.print(state ? "ON" : "OFF");
Serial.print(" applied=");
Serial.print(appliedMs);
Serial.print("ms total=");
Serial.print(millis() - startMs);
Serial.println("ms");
}
void handleGenericRelayRoute() {