Support MQTT endpoint port

This commit is contained in:
Terrence
2025-04-21 06:54:50 +08:00
parent a6619dcdb9
commit c380617cca
6 changed files with 28 additions and 14 deletions

View File

@@ -490,7 +490,7 @@ void Application::Start() {
} }
} }
}); });
protocol_->Start(); bool protocol_started = protocol_->Start();
#if CONFIG_USE_AUDIO_PROCESSOR #if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Initialize(codec, realtime_chat_enabled_); audio_processor_.Initialize(codec, realtime_chat_enabled_);
@@ -556,12 +556,15 @@ void Application::Start() {
// Wait for the new version check to finish // Wait for the new version check to finish
xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY); xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
SetDeviceState(kDeviceStateIdle); SetDeviceState(kDeviceStateIdle);
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
display->ShowNotification(message.c_str()); if (protocol_started) {
display->SetChatMessage("system", ""); std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
// Play the success sound to indicate the device is ready display->ShowNotification(message.c_str());
ResetDecoder(); display->SetChatMessage("system", "");
PlaySound(Lang::Sounds::P3_SUCCESS); // Play the success sound to indicate the device is ready
ResetDecoder();
PlaySound(Lang::Sounds::P3_SUCCESS);
}
// Enter the main event loop // Enter the main event loop
MainEventLoop(); MainEventLoop();

View File

@@ -27,8 +27,8 @@ MqttProtocol::~MqttProtocol() {
vEventGroupDelete(event_group_handle_); vEventGroupDelete(event_group_handle_);
} }
void MqttProtocol::Start() { bool MqttProtocol::Start() {
StartMqttClient(false); return StartMqttClient(false);
} }
bool MqttProtocol::StartMqttClient(bool report_error) { 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()); 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"); ESP_LOGE(TAG, "Failed to connect to endpoint");
SetError(Lang::Strings::SERVER_NOT_CONNECTED); SetError(Lang::Strings::SERVER_NOT_CONNECTED);
return false; return false;

View File

@@ -25,7 +25,7 @@ public:
MqttProtocol(); MqttProtocol();
~MqttProtocol(); ~MqttProtocol();
void Start() override; bool Start() override;
void SendAudio(const std::vector<uint8_t>& data) override; void SendAudio(const std::vector<uint8_t>& data) override;
bool OpenAudioChannel() override; bool OpenAudioChannel() override;
void CloseAudioChannel() override; void CloseAudioChannel() override;

View File

@@ -44,7 +44,7 @@ public:
void OnAudioChannelClosed(std::function<void()> callback); void OnAudioChannelClosed(std::function<void()> callback);
void OnNetworkError(std::function<void(const std::string& message)> callback); void OnNetworkError(std::function<void(const std::string& message)> callback);
virtual void Start() = 0; virtual bool Start() = 0;
virtual bool OpenAudioChannel() = 0; virtual bool OpenAudioChannel() = 0;
virtual void CloseAudioChannel() = 0; virtual void CloseAudioChannel() = 0;
virtual bool IsAudioChannelOpened() const = 0; virtual bool IsAudioChannelOpened() const = 0;

View File

@@ -22,7 +22,9 @@ WebsocketProtocol::~WebsocketProtocol() {
vEventGroupDelete(event_group_handle_); 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<uint8_t>& data) { void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {

View File

@@ -15,7 +15,7 @@ public:
WebsocketProtocol(); WebsocketProtocol();
~WebsocketProtocol(); ~WebsocketProtocol();
void Start() override; bool Start() override;
void SendAudio(const std::vector<uint8_t>& data) override; void SendAudio(const std::vector<uint8_t>& data) override;
bool OpenAudioChannel() override; bool OpenAudioChannel() override;
void CloseAudioChannel() override; void CloseAudioChannel() override;