Adjust mqtt variables

This commit is contained in:
Terrence
2025-05-21 15:59:27 +08:00
parent 8eecdd1ffc
commit f142c5469c
5 changed files with 42 additions and 46 deletions

View File

@@ -38,13 +38,13 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
}
Settings settings("mqtt", false);
endpoint_ = settings.GetString("endpoint");
client_id_ = settings.GetString("client_id");
username_ = settings.GetString("username");
password_ = settings.GetString("password");
auto endpoint = settings.GetString("endpoint");
auto client_id = settings.GetString("client_id");
auto username = settings.GetString("username");
auto password = settings.GetString("password");
publish_topic_ = settings.GetString("publish_topic");
if (endpoint_.empty()) {
if (endpoint.empty()) {
ESP_LOGW(TAG, "MQTT endpoint is not specified");
if (report_error) {
SetError(Lang::Strings::SERVER_NOT_FOUND);
@@ -66,8 +66,8 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
return;
}
cJSON* type = cJSON_GetObjectItem(root, "type");
if (type == nullptr) {
ESP_LOGE(TAG, "Message type is not specified");
if (!cJSON_IsString(type)) {
ESP_LOGE(TAG, "Message type is invalid");
cJSON_Delete(root);
return;
}
@@ -89,17 +89,17 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
last_incoming_time_ = std::chrono::steady_clock::now();
});
ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str());
ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint.c_str());
std::string broker_address;
int broker_port = 8883;
size_t pos = endpoint_.find(':');
size_t pos = endpoint.find(':');
if (pos != std::string::npos) {
broker_address = endpoint_.substr(0, pos);
broker_port = std::stoi(endpoint_.substr(pos + 1));
broker_address = endpoint.substr(0, pos);
broker_port = std::stoi(endpoint.substr(pos + 1));
} else {
broker_address = endpoint_;
broker_address = endpoint;
}
if (!mqtt_->Connect(broker_address, broker_port, client_id_, username_, password_)) {
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;
@@ -270,26 +270,26 @@ void MqttProtocol::ParseServerHello(const cJSON* root) {
}
auto session_id = cJSON_GetObjectItem(root, "session_id");
if (session_id != nullptr) {
if (cJSON_IsString(session_id)) {
session_id_ = session_id->valuestring;
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
}
// Get sample rate from hello message
auto audio_params = cJSON_GetObjectItem(root, "audio_params");
if (audio_params != NULL) {
if (cJSON_IsObject(audio_params)) {
auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate");
if (sample_rate != NULL) {
if (cJSON_IsNumber(sample_rate)) {
server_sample_rate_ = sample_rate->valueint;
}
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
if (frame_duration != NULL) {
if (cJSON_IsNumber(frame_duration)) {
server_frame_duration_ = frame_duration->valueint;
}
}
auto udp = cJSON_GetObjectItem(root, "udp");
if (udp == nullptr) {
if (!cJSON_IsObject(udp)) {
ESP_LOGE(TAG, "UDP is not specified");
return;
}

View File

@@ -34,10 +34,6 @@ public:
private:
EventGroupHandle_t event_group_handle_;
std::string endpoint_;
std::string client_id_;
std::string username_;
std::string password_;
std::string publish_topic_;
std::mutex channel_mutex_;

View File

@@ -154,7 +154,7 @@ bool WebsocketProtocol::OpenAudioChannel() {
// Parse JSON data
auto root = cJSON_Parse(data);
auto type = cJSON_GetObjectItem(root, "type");
if (type != NULL) {
if (cJSON_IsString(type)) {
if (strcmp(type->valuestring, "hello") == 0) {
ParseServerHello(root);
} else {
@@ -223,19 +223,19 @@ void WebsocketProtocol::ParseServerHello(const cJSON* root) {
}
auto session_id = cJSON_GetObjectItem(root, "session_id");
if (session_id != nullptr) {
if (cJSON_IsString(session_id)) {
session_id_ = session_id->valuestring;
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
}
auto audio_params = cJSON_GetObjectItem(root, "audio_params");
if (audio_params != NULL) {
if (cJSON_IsObject(audio_params)) {
auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate");
if (sample_rate != NULL) {
if (cJSON_IsNumber(sample_rate)) {
server_sample_rate_ = sample_rate->valueint;
}
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
if (frame_duration != NULL) {
if (cJSON_IsNumber(frame_duration)) {
server_frame_duration_ = frame_duration->valueint;
}
}