Skip to content

Commit c380617

Browse files
committed
Support MQTT endpoint port
1 parent a6619dc commit c380617

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

main/application.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ void Application::Start() {
490490
}
491491
}
492492
});
493-
protocol_->Start();
493+
bool protocol_started = protocol_->Start();
494494

495495
#if CONFIG_USE_AUDIO_PROCESSOR
496496
audio_processor_.Initialize(codec, realtime_chat_enabled_);
@@ -556,12 +556,15 @@ void Application::Start() {
556556
// Wait for the new version check to finish
557557
xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
558558
SetDeviceState(kDeviceStateIdle);
559-
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
560-
display->ShowNotification(message.c_str());
561-
display->SetChatMessage("system", "");
562-
// Play the success sound to indicate the device is ready
563-
ResetDecoder();
564-
PlaySound(Lang::Sounds::P3_SUCCESS);
559+
560+
if (protocol_started) {
561+
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
562+
display->ShowNotification(message.c_str());
563+
display->SetChatMessage("system", "");
564+
// Play the success sound to indicate the device is ready
565+
ResetDecoder();
566+
PlaySound(Lang::Sounds::P3_SUCCESS);
567+
}
565568

566569
// Enter the main event loop
567570
MainEventLoop();

main/protocols/mqtt_protocol.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ MqttProtocol::~MqttProtocol() {
2727
vEventGroupDelete(event_group_handle_);
2828
}
2929

30-
void MqttProtocol::Start() {
31-
StartMqttClient(false);
30+
bool MqttProtocol::Start() {
31+
return StartMqttClient(false);
3232
}
3333

3434
bool MqttProtocol::StartMqttClient(bool report_error) {
@@ -90,7 +90,16 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
9090
});
9191

9292
ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str());
93-
if (!mqtt_->Connect(endpoint_, 8883, client_id_, username_, password_)) {
93+
std::string broker_address;
94+
int broker_port = 8883;
95+
size_t pos = endpoint_.find(':');
96+
if (pos != std::string::npos) {
97+
broker_address = endpoint_.substr(0, pos);
98+
broker_port = std::stoi(endpoint_.substr(pos + 1));
99+
} else {
100+
broker_address = endpoint_;
101+
}
102+
if (!mqtt_->Connect(broker_address, broker_port, client_id_, username_, password_)) {
94103
ESP_LOGE(TAG, "Failed to connect to endpoint");
95104
SetError(Lang::Strings::SERVER_NOT_CONNECTED);
96105
return false;

main/protocols/mqtt_protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MqttProtocol : public Protocol {
2525
MqttProtocol();
2626
~MqttProtocol();
2727

28-
void Start() override;
28+
bool Start() override;
2929
void SendAudio(const std::vector<uint8_t>& data) override;
3030
bool OpenAudioChannel() override;
3131
void CloseAudioChannel() override;

main/protocols/protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Protocol {
4444
void OnAudioChannelClosed(std::function<void()> callback);
4545
void OnNetworkError(std::function<void(const std::string& message)> callback);
4646

47-
virtual void Start() = 0;
47+
virtual bool Start() = 0;
4848
virtual bool OpenAudioChannel() = 0;
4949
virtual void CloseAudioChannel() = 0;
5050
virtual bool IsAudioChannelOpened() const = 0;

main/protocols/websocket_protocol.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ WebsocketProtocol::~WebsocketProtocol() {
2222
vEventGroupDelete(event_group_handle_);
2323
}
2424

25-
void WebsocketProtocol::Start() {
25+
bool WebsocketProtocol::Start() {
26+
// Only connect to server when audio channel is needed
27+
return true;
2628
}
2729

2830
void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {

main/protocols/websocket_protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class WebsocketProtocol : public Protocol {
1515
WebsocketProtocol();
1616
~WebsocketProtocol();
1717

18-
void Start() override;
18+
bool Start() override;
1919
void SendAudio(const std::vector<uint8_t>& data) override;
2020
bool OpenAudioChannel() override;
2121
void CloseAudioChannel() override;

0 commit comments

Comments
 (0)