dashboard: improve relay command responsiveness
This commit is contained in:
@@ -479,21 +479,42 @@ static bool post_relay_state(const String &relay_id, bool state)
|
|||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.setTimeout(5000);
|
http.setTimeout(1200);
|
||||||
http.begin(CARGO_API_RELAY_SET_URL);
|
http.begin(CARGO_API_RELAY_SET_URL);
|
||||||
http.addHeader("Content-Type", "application/json");
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
unsigned long start_ms = millis();
|
||||||
int code = http.POST(payload);
|
int code = http.POST(payload);
|
||||||
String body = http.getString();
|
String body = http.getString();
|
||||||
|
|
||||||
Serial.print("Relay HTTP code: ");
|
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);
|
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();
|
http.end();
|
||||||
api_request_in_progress = false;
|
api_request_in_progress = false;
|
||||||
|
|
||||||
return code >= 200 && code < 300;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void relay_button_event_cb(lv_event_t *event)
|
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();
|
unsigned long start_ms = millis();
|
||||||
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.setTimeout(5000);
|
http.setTimeout(900);
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
int code = http.GET();
|
int code = http.GET();
|
||||||
|
|
||||||
@@ -1066,7 +1087,7 @@ static void poll_status_api()
|
|||||||
return;
|
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;
|
fast_status_refresh_requested = false;
|
||||||
last_status_poll_ms = millis();
|
last_status_poll_ms = millis();
|
||||||
fetch_status_fields(CARGO_API_FAST_STATUS_URL, "fast status");
|
fetch_status_fields(CARGO_API_FAST_STATUS_URL, "fast status");
|
||||||
@@ -1444,5 +1465,5 @@ void loop()
|
|||||||
process_pending_relay_command();
|
process_pending_relay_command();
|
||||||
poll_status_api();
|
poll_status_api();
|
||||||
|
|
||||||
delay(250);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3829,6 +3829,8 @@ void handleSelectBms() {
|
|||||||
|
|
||||||
|
|
||||||
void handleSetRelayPost() {
|
void handleSetRelayPost() {
|
||||||
|
unsigned long startMs = millis();
|
||||||
|
|
||||||
DynamicJsonDocument doc(512);
|
DynamicJsonDocument doc(512);
|
||||||
DeserializationError error = deserializeJson(doc, server.arg("plain"));
|
DeserializationError error = deserializeJson(doc, server.arg("plain"));
|
||||||
|
|
||||||
@@ -3840,20 +3842,34 @@ void handleSetRelayPost() {
|
|||||||
String relayId = doc["id"] | "";
|
String relayId = doc["id"] | "";
|
||||||
bool state = doc["state"] | false;
|
bool state = doc["state"] | false;
|
||||||
|
|
||||||
|
unsigned long applyStartMs = millis();
|
||||||
if (!setRelayById(relayId, state)) {
|
if (!setRelayById(relayId, state)) {
|
||||||
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
|
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
unsigned long appliedMs = millis() - applyStartMs;
|
||||||
|
|
||||||
DynamicJsonDocument response(512);
|
DynamicJsonDocument response(512);
|
||||||
response["type"] = "relay_response";
|
response["type"] = "relay_response";
|
||||||
response["ok"] = true;
|
response["ok"] = true;
|
||||||
response["id"] = relayId;
|
response["id"] = relayId;
|
||||||
response["state"] = state;
|
response["state"] = state;
|
||||||
|
response["applied_ms"] = appliedMs;
|
||||||
|
response["total_ms"] = millis() - startMs;
|
||||||
|
|
||||||
String output;
|
String output;
|
||||||
serializeJson(response, output);
|
serializeJson(response, output);
|
||||||
server.send(200, "application/json", 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() {
|
void handleGenericRelayRoute() {
|
||||||
|
|||||||
Reference in New Issue
Block a user