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

View File

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