add network error callback

This commit is contained in:
Terrence
2024-11-20 03:28:52 +08:00
parent 874adc80b8
commit c79d6cf4d8
8 changed files with 26 additions and 3 deletions

View File

@@ -284,6 +284,9 @@ void Application::Start() {
#else #else
protocol_ = new MqttProtocol(); protocol_ = new MqttProtocol();
#endif #endif
protocol_->OnNetworkError([this](const std::string& message) {
Alert("Error", std::move(message));
});
protocol_->OnIncomingAudio([this](const std::string& data) { protocol_->OnIncomingAudio([this](const std::string& data) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
audio_decode_queue_.emplace_back(std::move(data)); audio_decode_queue_.emplace_back(std::move(data));

View File

@@ -4,7 +4,7 @@
#include <esp_log.h> #include <esp_log.h>
static const char *TAG = "Axp2101"; #define TAG "Axp2101"
Axp2101::Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) { Axp2101::Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
// ** EFUSE defaults ** // ** EFUSE defaults **

View File

@@ -85,6 +85,9 @@ bool MqttProtocol::StartMqttClient() {
ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str()); ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str());
if (!mqtt_->Connect(endpoint_, 8883, client_id_, username_, password_)) { if (!mqtt_->Connect(endpoint_, 8883, client_id_, username_, password_)) {
ESP_LOGE(TAG, "Failed to connect to endpoint"); ESP_LOGE(TAG, "Failed to connect to endpoint");
if (on_network_error_ != nullptr) {
on_network_error_("无法连接服务");
}
return false; return false;
} }
@@ -167,7 +170,6 @@ bool MqttProtocol::OpenAudioChannel() {
if (mqtt_ == nullptr || !mqtt_->IsConnected()) { if (mqtt_ == nullptr || !mqtt_->IsConnected()) {
ESP_LOGI(TAG, "MQTT is not connected, try to connect now"); ESP_LOGI(TAG, "MQTT is not connected, try to connect now");
if (!StartMqttClient()) { if (!StartMqttClient()) {
ESP_LOGE(TAG, "Failed to connect to MQTT");
return false; 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)); 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)) { if (!(bits & MQTT_PROTOCOL_SERVER_HELLO_EVENT)) {
ESP_LOGE(TAG, "Failed to receive server hello"); ESP_LOGE(TAG, "Failed to receive server hello");
if (on_network_error_ != nullptr) {
on_network_error_("等待响应超时");
}
return false; return false;
} }

View File

@@ -19,3 +19,7 @@ void Protocol::OnAudioChannelOpened(std::function<void()> callback) {
void Protocol::OnAudioChannelClosed(std::function<void()> callback) { void Protocol::OnAudioChannelClosed(std::function<void()> callback) {
on_audio_channel_closed_ = callback; on_audio_channel_closed_ = callback;
} }
void Protocol::OnNetworkError(std::function<void(const std::string& message)> callback) {
on_network_error_ = callback;
}

View File

@@ -25,6 +25,7 @@ public:
void OnIncomingJson(std::function<void(const cJSON* root)> callback); void OnIncomingJson(std::function<void(const cJSON* root)> callback);
void OnAudioChannelOpened(std::function<void()> callback); void OnAudioChannelOpened(std::function<void()> callback);
void OnAudioChannelClosed(std::function<void()> callback); void OnAudioChannelClosed(std::function<void()> callback);
void OnNetworkError(std::function<void(const std::string& message)> callback);
virtual void SendAudio(const std::string& data) = 0; virtual void SendAudio(const std::string& data) = 0;
virtual void SendText(const std::string& text) = 0; virtual void SendText(const std::string& text) = 0;
@@ -39,6 +40,7 @@ protected:
std::function<void(const std::string& data)> on_incoming_audio_; std::function<void(const std::string& data)> on_incoming_audio_;
std::function<void()> on_audio_channel_opened_; std::function<void()> on_audio_channel_opened_;
std::function<void()> on_audio_channel_closed_; std::function<void()> on_audio_channel_closed_;
std::function<void(const std::string& message)> on_network_error_;
int server_sample_rate_ = 16000; int server_sample_rate_ = 16000;
}; };

View File

@@ -110,6 +110,9 @@ bool WebsocketProtocol::OpenAudioChannel() {
if (!websocket_->Connect(url.c_str())) { if (!websocket_->Connect(url.c_str())) {
ESP_LOGE(TAG, "Failed to connect to websocket server"); ESP_LOGE(TAG, "Failed to connect to websocket server");
if (on_network_error_ != nullptr) {
on_network_error_("无法连接服务");
}
return false; 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)); 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)) { if (!(bits & WEBSOCKET_PROTOCOL_SERVER_HELLO_EVENT)) {
ESP_LOGE(TAG, "Failed to receive server hello"); ESP_LOGE(TAG, "Failed to receive server hello");
if (on_network_error_ != nullptr) {
on_network_error_("等待响应超时");
}
return false; return false;
} }

View File

@@ -11,7 +11,7 @@ Settings::Settings(const std::string& ns, bool read_write) : ns_(ns), read_write
Settings::~Settings() { Settings::~Settings() {
if (nvs_handle_ != 0) { if (nvs_handle_ != 0) {
if (read_write_) { if (read_write_ && dirty_) {
ESP_ERROR_CHECK(nvs_commit(nvs_handle_)); ESP_ERROR_CHECK(nvs_commit(nvs_handle_));
} }
nvs_close(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) { void Settings::SetString(const std::string& key, const std::string& value) {
if (read_write_) { if (read_write_) {
ESP_ERROR_CHECK(nvs_set_str(nvs_handle_, key.c_str(), value.c_str())); ESP_ERROR_CHECK(nvs_set_str(nvs_handle_, key.c_str(), value.c_str()));
dirty_ = true;
} else { } else {
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str()); 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) { void Settings::SetInt(const std::string& key, int32_t value) {
if (read_write_) { if (read_write_) {
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle_, key.c_str(), value)); ESP_ERROR_CHECK(nvs_set_i32(nvs_handle_, key.c_str(), value));
dirty_ = true;
} else { } else {
ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str()); ESP_LOGW(TAG, "Namespace %s is not open for writing", ns_.c_str());
} }

View File

@@ -18,6 +18,7 @@ private:
std::string ns_; std::string ns_;
nvs_handle_t nvs_handle_ = 0; nvs_handle_t nvs_handle_ = 0;
bool read_write_ = false; bool read_write_ = false;
bool dirty_ = false;
}; };
#endif #endif