Fix polling error

This commit is contained in:
GW_MC
2026-02-02 21:12:54 +08:00
parent e467951b8c
commit 1dff88ed1a
6 changed files with 141 additions and 93 deletions

View File

@@ -137,6 +137,7 @@ void IotDisBridge::poll_task_(void* param) {
while (!bridge->stop_polling_) {
ESP_LOGI(TAG, "Polling for status update...");
bridge->poll_status_();
ESP_LOGI(TAG, "poll_status_() returned");
// Yield to allow display updates to complete
taskYIELD();
@@ -150,6 +151,7 @@ void IotDisBridge::poll_task_(void* param) {
}
ESP_LOGI(TAG, "Polling task stopped");
bridge->poll_task_handle_ = nullptr;
vTaskDelete(nullptr);
}
@@ -176,38 +178,28 @@ void IotDisBridge::poll_status_() {
event_data.state = StatusUpdateEventData::VoiceState::UNMUTED;
}
// Reset failure counter on successful push update
if (event_data.state != StatusUpdateEventData::VoiceState::UNKNOWN) {
consecutive_failures_ = 0;
}
if (on_status_update_callback_) {
on_status_update_callback_(event_data, status_event_user_data_);
}
// VoiceState new_state = VoiceState::UNKNOWN;
// if (push_message == MUTED_RESPONSE) {
// new_state = VoiceState::MUTED;
// } else if (push_message == UNMUTED_RESPONSE) {
// new_state = VoiceState::UNMUTED;
// }
// if (new_state != VoiceState::UNKNOWN) {
// consecutive_failures_ = 0;
// if (main_ui_ && current_page_ == Page::MAIN) {
// main_ui_->show_error_notification(false);
// }
// if (state_mutex_ && xSemaphoreTake(state_mutex_, pdMS_TO_TICKS(100)) == pdTRUE) {
// current_state_ = new_state;
// xSemaphoreGive(state_mutex_);
// }
// update_main_ui();
// return; // Got push update, skip polling
// }
// Got push update, skip polling
if (event_data.state != StatusUpdateEventData::VoiceState::UNKNOWN) {
return;
}
}
// Send STATUS command for polling
ESP_LOGI(TAG, "Sending STATUS command for polling");
err = udp_client_.send_command(STATUS_COMMAND);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Failed to send STATUS command");
consecutive_failures_++;
ESP_LOGW(TAG, "Failed to send STATUS command for polling. Consecutive failures: %d",
consecutive_failures_);
if (consecutive_failures_ >= MAX_FAILURES_BEFORE_ERROR) {
if (on_status_update_callback_) {
@@ -221,12 +213,15 @@ void IotDisBridge::poll_status_() {
}
// Wait for response to STATUS command
ESP_LOGI(TAG, "Waiting for response to STATUS command (timeout: %dms)", RESPONSE_TIMEOUT_MS);
std::string response;
err = udp_client_.receive_response(response, RESPONSE_TIMEOUT_MS);
ESP_LOGI(TAG, "Received response from STATUS command: err=%d", err);
if (err == ESP_OK) {
// Success - reset failure counter
consecutive_failures_ = 0;
ESP_LOGI(TAG, "STATUS response: %s", response.c_str());
StatusUpdateEventData event_data {
.state = StatusUpdateEventData::VoiceState::UNKNOWN
@@ -236,15 +231,19 @@ void IotDisBridge::poll_status_() {
} else if (response == UNMUTED_RESPONSE) {
event_data.state = StatusUpdateEventData::VoiceState::UNMUTED;
}
ESP_LOGI(TAG, "Invoking status update callback with state: %d", event_data.state);
if (on_status_update_callback_) {
on_status_update_callback_(event_data, status_event_user_data_);
ESP_LOGI(TAG, "Status update callback returned");
}
} else {
// Timeout or error
consecutive_failures_++;
ESP_LOGW(TAG, "No response to STATUS (failures: %d)", consecutive_failures_);
ESP_LOGW(TAG, "No response to STATUS (failures: %d, error: %d)", consecutive_failures_, err);
if (consecutive_failures_ >= MAX_FAILURES_BEFORE_ERROR) {
ESP_LOGW(TAG, "Max failures reached, sending ERROR state");
if (on_status_update_callback_) {
on_status_update_callback_(
StatusUpdateEventData { .state = StatusUpdateEventData::VoiceState::ERROR },
@@ -253,4 +252,6 @@ void IotDisBridge::poll_status_() {
}
}
}
ESP_LOGI(TAG, "poll_status_ complete");
}

View File

@@ -45,8 +45,8 @@ public:
}
private:
static constexpr int POLL_INTERVAL_MS = 2000;
static constexpr int ERROR_POLL_INTERVAL_MS = 5000;
static constexpr int POLL_INTERVAL_MS = 10000;
static constexpr int ERROR_POLL_INTERVAL_MS = 20000;
static constexpr int RESPONSE_TIMEOUT_MS = 1000;
static constexpr int MAX_FAILURES_BEFORE_ERROR = 3;