avoid double error alerts

This commit is contained in:
Terrence
2025-04-09 09:13:18 +08:00
parent 69540c6551
commit 652e5cbcdd
5 changed files with 18 additions and 9 deletions

View File

@@ -100,14 +100,16 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
return true; return true;
} }
void MqttProtocol::SendText(const std::string& text) { bool MqttProtocol::SendText(const std::string& text) {
if (publish_topic_.empty()) { if (publish_topic_.empty()) {
return; return false;
} }
if (!mqtt_->Publish(publish_topic_, text)) { if (!mqtt_->Publish(publish_topic_, text)) {
ESP_LOGE(TAG, "Failed to publish message: %s", text.c_str()); ESP_LOGE(TAG, "Failed to publish message: %s", text.c_str());
SetError(Lang::Strings::SERVER_ERROR); SetError(Lang::Strings::SERVER_ERROR);
return false;
} }
return true;
} }
void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) { void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) {
@@ -174,7 +176,9 @@ bool MqttProtocol::OpenAudioChannel() {
message += "\"audio_params\":{"; message += "\"audio_params\":{";
message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS); message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS);
message += "}}"; message += "}}";
SendText(message); if (!SendText(message)) {
return false;
}
// 等待服务器响应 // 等待服务器响应
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));

View File

@@ -54,7 +54,7 @@ private:
void ParseServerHello(const cJSON* root); void ParseServerHello(const cJSON* root);
std::string DecodeHexString(const std::string& hex_string); std::string DecodeHexString(const std::string& hex_string);
void SendText(const std::string& text) override; bool SendText(const std::string& text) override;
}; };

View File

@@ -69,7 +69,7 @@ protected:
std::string session_id_; std::string session_id_;
std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_; std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_;
virtual void SendText(const std::string& text) = 0; virtual bool SendText(const std::string& text) = 0;
virtual void SetError(const std::string& message); virtual void SetError(const std::string& message);
virtual bool IsTimeout() const; virtual bool IsTimeout() const;
}; };

View File

@@ -33,15 +33,18 @@ void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {
websocket_->Send(data.data(), data.size(), true); websocket_->Send(data.data(), data.size(), true);
} }
void WebsocketProtocol::SendText(const std::string& text) { bool WebsocketProtocol::SendText(const std::string& text) {
if (websocket_ == nullptr) { if (websocket_ == nullptr) {
return; return false;
} }
if (!websocket_->Send(text)) { if (!websocket_->Send(text)) {
ESP_LOGE(TAG, "Failed to send text: %s", text.c_str()); ESP_LOGE(TAG, "Failed to send text: %s", text.c_str());
SetError(Lang::Strings::SERVER_ERROR); SetError(Lang::Strings::SERVER_ERROR);
return false;
} }
return true;
} }
bool WebsocketProtocol::IsAudioChannelOpened() const { bool WebsocketProtocol::IsAudioChannelOpened() const {
@@ -116,7 +119,9 @@ bool WebsocketProtocol::OpenAudioChannel() {
message += "\"audio_params\":{"; message += "\"audio_params\":{";
message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS); message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS);
message += "}}"; message += "}}";
websocket_->Send(message); if (!SendText(message)) {
return false;
}
// Wait for server hello // Wait for server hello
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));

View File

@@ -26,7 +26,7 @@ private:
WebSocket* websocket_ = nullptr; WebSocket* websocket_ = nullptr;
void ParseServerHello(const cJSON* root); void ParseServerHello(const cJSON* root);
void SendText(const std::string& text) override; bool SendText(const std::string& text) override;
}; };
#endif #endif