MQTT会话超时与网络错误处理

This commit is contained in:
Terrence
2025-03-04 05:32:11 +08:00
parent c60f134093
commit 5a71e1bdd6
6 changed files with 52 additions and 30 deletions

View File

@ -28,10 +28,10 @@ MqttProtocol::~MqttProtocol() {
}
void MqttProtocol::Start() {
StartMqttClient();
StartMqttClient(false);
}
bool MqttProtocol::StartMqttClient() {
bool MqttProtocol::StartMqttClient(bool report_error) {
if (mqtt_ != nullptr) {
ESP_LOGW(TAG, "Mqtt client already started");
delete mqtt_;
@ -45,9 +45,9 @@ bool MqttProtocol::StartMqttClient() {
publish_topic_ = settings.GetString("publish_topic");
if (endpoint_.empty()) {
ESP_LOGE(TAG, "MQTT endpoint is not specified");
if (on_network_error_ != nullptr) {
on_network_error_(Lang::Strings::SERVER_NOT_FOUND);
ESP_LOGW(TAG, "MQTT endpoint is not specified");
if (report_error) {
SetError(Lang::Strings::SERVER_NOT_FOUND);
}
return false;
}
@ -76,6 +76,7 @@ bool MqttProtocol::StartMqttClient() {
ParseServerHello(root);
} else if (strcmp(type->valuestring, "goodbye") == 0) {
auto session_id = cJSON_GetObjectItem(root, "session_id");
ESP_LOGI(TAG, "Received goodbye message, session_id: %s", session_id ? session_id->valuestring : "null");
if (session_id == nullptr || session_id_ == session_id->valuestring) {
Application::GetInstance().Schedule([this]() {
CloseAudioChannel();
@ -85,14 +86,13 @@ bool MqttProtocol::StartMqttClient() {
on_incoming_json_(root);
}
cJSON_Delete(root);
last_incoming_time_ = std::chrono::steady_clock::now();
});
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_(Lang::Strings::SERVER_NOT_CONNECTED);
}
SetError(Lang::Strings::SERVER_NOT_CONNECTED);
return false;
}
@ -105,10 +105,8 @@ void MqttProtocol::SendText(const std::string& text) {
return;
}
if (!mqtt_->Publish(publish_topic_, text)) {
ESP_LOGE(TAG, "Failed to publish message");
if (on_network_error_ != nullptr) {
on_network_error_(Lang::Strings::SERVER_ERROR);
}
ESP_LOGE(TAG, "Failed to publish message: %s", text.c_str());
SetError(Lang::Strings::SERVER_ERROR);
}
}
@ -159,11 +157,12 @@ void MqttProtocol::CloseAudioChannel() {
bool MqttProtocol::OpenAudioChannel() {
if (mqtt_ == nullptr || !mqtt_->IsConnected()) {
ESP_LOGI(TAG, "MQTT is not connected, try to connect now");
if (!StartMqttClient()) {
if (!StartMqttClient(true)) {
return false;
}
}
error_occurred_ = false;
session_id_ = "";
xEventGroupClearBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT);
@ -181,9 +180,7 @@ 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_(Lang::Strings::SERVER_TIMEOUT);
}
SetError(Lang::Strings::SERVER_TIMEOUT);
return false;
}
@ -226,6 +223,7 @@ bool MqttProtocol::OpenAudioChannel() {
on_incoming_audio_(std::move(decrypted));
}
remote_sequence_ = sequence;
last_incoming_time_ = std::chrono::steady_clock::now();
});
udp_->Connect(udp_server_, udp_port_);
@ -298,5 +296,5 @@ std::string MqttProtocol::DecodeHexString(const std::string& hex_string) {
}
bool MqttProtocol::IsAudioChannelOpened() const {
return udp_ != nullptr;
return udp_ != nullptr && !error_occurred_ && !IsTimeout();
}