diff --git a/main/application.cc b/main/application.cc index bd7de61c..5b76d64c 100644 --- a/main/application.cc +++ b/main/application.cc @@ -284,6 +284,9 @@ void Application::Start() { #else protocol_ = new MqttProtocol(); #endif + protocol_->OnNetworkError([this](const std::string& message) { + Alert("Error", std::move(message)); + }); protocol_->OnIncomingAudio([this](const std::string& data) { std::lock_guard lock(mutex_); audio_decode_queue_.emplace_back(std::move(data)); diff --git a/main/boards/kevin-box-2/axp2101.cc b/main/boards/kevin-box-2/axp2101.cc index a07da194..daaeaf1d 100644 --- a/main/boards/kevin-box-2/axp2101.cc +++ b/main/boards/kevin-box-2/axp2101.cc @@ -4,7 +4,7 @@ #include -static const char *TAG = "Axp2101"; +#define TAG "Axp2101" Axp2101::Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { // ** EFUSE defaults ** diff --git a/main/protocols/mqtt_protocol.cc b/main/protocols/mqtt_protocol.cc index c17880a7..cbf4b580 100644 --- a/main/protocols/mqtt_protocol.cc +++ b/main/protocols/mqtt_protocol.cc @@ -85,6 +85,9 @@ bool MqttProtocol::StartMqttClient() { ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str()); if (!mqtt_->Connect(endpoint_, 8883, client_id_, username_, password_)) { ESP_LOGE(TAG, "Failed to connect to endpoint"); + if (on_network_error_ != nullptr) { + on_network_error_("无法连接服务"); + } return false; } @@ -167,7 +170,6 @@ bool MqttProtocol::OpenAudioChannel() { if (mqtt_ == nullptr || !mqtt_->IsConnected()) { ESP_LOGI(TAG, "MQTT is not connected, try to connect now"); if (!StartMqttClient()) { - ESP_LOGE(TAG, "Failed to connect to MQTT"); return false; } } @@ -188,6 +190,9 @@ bool MqttProtocol::OpenAudioChannel() { EventBits_t bits = xEventGroupWaitBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000)); if (!(bits & MQTT_PROTOCOL_SERVER_HELLO_EVENT)) { ESP_LOGE(TAG, "Failed to receive server hello"); + if (on_network_error_ != nullptr) { + on_network_error_("等待响应超时"); + } return false; } diff --git a/main/protocols/protocol.cc b/main/protocols/protocol.cc index aeff91fd..d9906c2a 100644 --- a/main/protocols/protocol.cc +++ b/main/protocols/protocol.cc @@ -19,3 +19,7 @@ void Protocol::OnAudioChannelOpened(std::function callback) { void Protocol::OnAudioChannelClosed(std::function callback) { on_audio_channel_closed_ = callback; } + +void Protocol::OnNetworkError(std::function callback) { + on_network_error_ = callback; +} diff --git a/main/protocols/protocol.h b/main/protocols/protocol.h index 063405f3..6261b9ca 100644 --- a/main/protocols/protocol.h +++ b/main/protocols/protocol.h @@ -25,6 +25,7 @@ public: void OnIncomingJson(std::function callback); void OnAudioChannelOpened(std::function callback); void OnAudioChannelClosed(std::function callback); + void OnNetworkError(std::function callback); virtual void SendAudio(const std::string& data) = 0; virtual void SendText(const std::string& text) = 0; @@ -39,6 +40,7 @@ protected: std::function on_incoming_audio_; std::function on_audio_channel_opened_; std::function on_audio_channel_closed_; + std::function on_network_error_; int server_sample_rate_ = 16000; }; diff --git a/main/protocols/websocket_protocol.cc b/main/protocols/websocket_protocol.cc index 70e433d6..078185ad 100644 --- a/main/protocols/websocket_protocol.cc +++ b/main/protocols/websocket_protocol.cc @@ -110,6 +110,9 @@ bool WebsocketProtocol::OpenAudioChannel() { if (!websocket_->Connect(url.c_str())) { ESP_LOGE(TAG, "Failed to connect to websocket server"); + if (on_network_error_ != nullptr) { + on_network_error_("无法连接服务"); + } return false; } @@ -128,6 +131,9 @@ bool WebsocketProtocol::OpenAudioChannel() { EventBits_t bits = xEventGroupWaitBits(event_group_handle_, WEBSOCKET_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000)); if (!(bits & WEBSOCKET_PROTOCOL_SERVER_HELLO_EVENT)) { ESP_LOGE(TAG, "Failed to receive server hello"); + if (on_network_error_ != nullptr) { + on_network_error_("等待响应超时"); + } return false; } diff --git a/main/settings.cc b/main/settings.cc index 70daf8ae..ec27ffb2 100644 --- a/main/settings.cc +++ b/main/settings.cc @@ -11,7 +11,7 @@ Settings::Settings(const std::string& ns, bool read_write) : ns_(ns), read_write Settings::~Settings() { if (nvs_handle_ != 0) { - if (read_write_) { + if (read_write_ && dirty_) { ESP_ERROR_CHECK(nvs_commit(nvs_handle_)); } nvs_close(nvs_handle_); @@ -37,6 +37,7 @@ std::string Settings::GetString(const std::string& key, const std::string& defau void Settings::SetString(const std::string& key, const std::string& value) { if (read_write_) { ESP_ERROR_CHECK(nvs_set_str(nvs_handle_, key.c_str(), value.c_str())); + dirty_ = true; } else { ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str()); } @@ -57,6 +58,7 @@ int32_t Settings::GetInt(const std::string& key, int32_t default_value) { void Settings::SetInt(const std::string& key, int32_t value) { if (read_write_) { ESP_ERROR_CHECK(nvs_set_i32(nvs_handle_, key.c_str(), value)); + dirty_ = true; } else { ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str()); } diff --git a/main/settings.h b/main/settings.h index bc4c4fe4..2273b5a8 100644 --- a/main/settings.h +++ b/main/settings.h @@ -18,6 +18,7 @@ private: std::string ns_; nvs_handle_t nvs_handle_ = 0; bool read_write_ = false; + bool dirty_ = false; }; #endif