feat(travel): Enhance MTR arrival handling with retry logic and improve UI update efficiency

This commit is contained in:
GW_MC
2026-02-03 20:49:42 +08:00
parent 3617a206ff
commit 6c4050e9d4
4 changed files with 100 additions and 32 deletions

View File

@@ -116,30 +116,57 @@ MtrArrivalErrorCode MTRNextTrainHandler::get_next_arrival_info(
// Create HTTP client configuration
esp_http_client_config_t http_config = {};
http_config.url = url_str.c_str();
http_config.timeout_ms = 10000;
http_config.timeout_ms = 15000;
http_config.transport_type = HTTP_TRANSPORT_OVER_SSL;
http_config.crt_bundle_attach = esp_crt_bundle_attach;
// Get HTTP handler and perform request
auto http_handler = network_handler->get_http_handler(std::move(http_config));
if (!http_handler) {
ESP_LOGE(TAG, "Failed to create HTTP handler");
return MtrArrivalErrorCode::UNKNOWN;
}
esp_err_t err = http_handler->perform_request();
if (err != ESP_OK) {
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
return MtrArrivalErrorCode::NO_ARRIVAL_INFO;
}
// Get response body
// Retry logic for connection failures
constexpr int MAX_RETRIES = 2;
esp_err_t err = ESP_FAIL;
char* buffer = nullptr;
int total_len = 0;
http_handler->get_body(buffer, total_len);
if (!buffer || total_len <= 0) {
ESP_LOGE(TAG, "Empty response from MTR API");
for (int retry = 0; retry <= MAX_RETRIES; retry++) {
if (retry > 0) {
ESP_LOGW(TAG, "Retrying HTTP request (%d/%d)", retry, MAX_RETRIES);
vTaskDelay(pdMS_TO_TICKS(500));
}
// Create HTTP client configuration for each attempt
esp_http_client_config_t http_config = {};
http_config.url = url_str.c_str();
http_config.timeout_ms = 15000;
http_config.transport_type = HTTP_TRANSPORT_OVER_SSL;
http_config.crt_bundle_attach = esp_crt_bundle_attach;
// Get HTTP handler and perform request
auto http_handler = network_handler->get_http_handler(std::move(http_config));
if (!http_handler) {
ESP_LOGE(TAG, "Failed to create HTTP handler");
continue;
}
err = http_handler->perform_request();
if (err != ESP_OK) {
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
continue;
}
// Get response body
http_handler->get_body(buffer, total_len);
if (buffer && total_len > 0) {
break;
}
if (buffer) {
free(buffer);
buffer = nullptr;
}
}
if (err != ESP_OK || !buffer || total_len <= 0) {
ESP_LOGE(TAG, "Failed to get response after retries");
if (buffer) {
free(buffer);
}