diff --git a/main/application.cc b/main/application.cc index c223054f..a0cab784 100644 --- a/main/application.cc +++ b/main/application.cc @@ -490,7 +490,7 @@ void Application::Start() { } } }); - protocol_->Start(); + bool protocol_started = protocol_->Start(); #if CONFIG_USE_AUDIO_PROCESSOR audio_processor_.Initialize(codec, realtime_chat_enabled_); @@ -556,12 +556,15 @@ void Application::Start() { // Wait for the new version check to finish xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY); SetDeviceState(kDeviceStateIdle); - std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion(); - display->ShowNotification(message.c_str()); - display->SetChatMessage("system", ""); - // Play the success sound to indicate the device is ready - ResetDecoder(); - PlaySound(Lang::Sounds::P3_SUCCESS); + + if (protocol_started) { + std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion(); + display->ShowNotification(message.c_str()); + display->SetChatMessage("system", ""); + // Play the success sound to indicate the device is ready + ResetDecoder(); + PlaySound(Lang::Sounds::P3_SUCCESS); + } // Enter the main event loop MainEventLoop(); diff --git a/main/protocols/mqtt_protocol.cc b/main/protocols/mqtt_protocol.cc index d5ac5635..3cda2e79 100644 --- a/main/protocols/mqtt_protocol.cc +++ b/main/protocols/mqtt_protocol.cc @@ -27,8 +27,8 @@ MqttProtocol::~MqttProtocol() { vEventGroupDelete(event_group_handle_); } -void MqttProtocol::Start() { - StartMqttClient(false); +bool MqttProtocol::Start() { + return StartMqttClient(false); } bool MqttProtocol::StartMqttClient(bool report_error) { @@ -90,7 +90,16 @@ bool MqttProtocol::StartMqttClient(bool report_error) { }); ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str()); - if (!mqtt_->Connect(endpoint_, 8883, client_id_, username_, password_)) { + std::string broker_address; + int broker_port = 8883; + size_t pos = endpoint_.find(':'); + if (pos != std::string::npos) { + broker_address = endpoint_.substr(0, pos); + broker_port = std::stoi(endpoint_.substr(pos + 1)); + } else { + broker_address = endpoint_; + } + if (!mqtt_->Connect(broker_address, broker_port, client_id_, username_, password_)) { ESP_LOGE(TAG, "Failed to connect to endpoint"); SetError(Lang::Strings::SERVER_NOT_CONNECTED); return false; diff --git a/main/protocols/mqtt_protocol.h b/main/protocols/mqtt_protocol.h index 8c75cd7b..e531b00a 100644 --- a/main/protocols/mqtt_protocol.h +++ b/main/protocols/mqtt_protocol.h @@ -25,7 +25,7 @@ public: MqttProtocol(); ~MqttProtocol(); - void Start() override; + bool Start() override; void SendAudio(const std::vector& data) override; bool OpenAudioChannel() override; void CloseAudioChannel() override; diff --git a/main/protocols/protocol.h b/main/protocols/protocol.h index 1dbd268c..5fdd5751 100644 --- a/main/protocols/protocol.h +++ b/main/protocols/protocol.h @@ -44,7 +44,7 @@ public: void OnAudioChannelClosed(std::function callback); void OnNetworkError(std::function callback); - virtual void Start() = 0; + virtual bool Start() = 0; virtual bool OpenAudioChannel() = 0; virtual void CloseAudioChannel() = 0; virtual bool IsAudioChannelOpened() const = 0; diff --git a/main/protocols/websocket_protocol.cc b/main/protocols/websocket_protocol.cc index 64e0740a..50b234a4 100644 --- a/main/protocols/websocket_protocol.cc +++ b/main/protocols/websocket_protocol.cc @@ -22,7 +22,9 @@ WebsocketProtocol::~WebsocketProtocol() { vEventGroupDelete(event_group_handle_); } -void WebsocketProtocol::Start() { +bool WebsocketProtocol::Start() { + // Only connect to server when audio channel is needed + return true; } void WebsocketProtocol::SendAudio(const std::vector& data) { diff --git a/main/protocols/websocket_protocol.h b/main/protocols/websocket_protocol.h index 3f32e726..5e96083f 100644 --- a/main/protocols/websocket_protocol.h +++ b/main/protocols/websocket_protocol.h @@ -15,7 +15,7 @@ public: WebsocketProtocol(); ~WebsocketProtocol(); - void Start() override; + bool Start() override; void SendAudio(const std::vector& data) override; bool OpenAudioChannel() override; void CloseAudioChannel() override;