forked from xiaozhi/xiaozhi-esp32
Adjust mqtt variables
This commit is contained in:
@@ -11,7 +11,7 @@ dependencies:
|
|||||||
78/esp_lcd_nv3023: ~1.0.0
|
78/esp_lcd_nv3023: ~1.0.0
|
||||||
78/esp-wifi-connect: ~2.4.2
|
78/esp-wifi-connect: ~2.4.2
|
||||||
78/esp-opus-encoder: ~2.3.2
|
78/esp-opus-encoder: ~2.3.2
|
||||||
78/esp-ml307: ~2.0.0
|
78/esp-ml307: ~2.0.1
|
||||||
78/xiaozhi-fonts: ~1.3.2
|
78/xiaozhi-fonts: ~1.3.2
|
||||||
espressif/led_strip: ^2.5.5
|
espressif/led_strip: ^2.5.5
|
||||||
espressif/esp_codec_dev: ~1.3.2
|
espressif/esp_codec_dev: ~1.3.2
|
||||||
|
|||||||
36
main/ota.cc
36
main/ota.cc
@@ -110,34 +110,34 @@ bool Ota::CheckVersion() {
|
|||||||
has_activation_code_ = false;
|
has_activation_code_ = false;
|
||||||
has_activation_challenge_ = false;
|
has_activation_challenge_ = false;
|
||||||
cJSON *activation = cJSON_GetObjectItem(root, "activation");
|
cJSON *activation = cJSON_GetObjectItem(root, "activation");
|
||||||
if (activation != NULL) {
|
if (cJSON_IsObject(activation)) {
|
||||||
cJSON* message = cJSON_GetObjectItem(activation, "message");
|
cJSON* message = cJSON_GetObjectItem(activation, "message");
|
||||||
if (message != NULL) {
|
if (cJSON_IsString(message)) {
|
||||||
activation_message_ = message->valuestring;
|
activation_message_ = message->valuestring;
|
||||||
}
|
}
|
||||||
cJSON* code = cJSON_GetObjectItem(activation, "code");
|
cJSON* code = cJSON_GetObjectItem(activation, "code");
|
||||||
if (code != NULL) {
|
if (cJSON_IsString(code)) {
|
||||||
activation_code_ = code->valuestring;
|
activation_code_ = code->valuestring;
|
||||||
has_activation_code_ = true;
|
has_activation_code_ = true;
|
||||||
}
|
}
|
||||||
cJSON* challenge = cJSON_GetObjectItem(activation, "challenge");
|
cJSON* challenge = cJSON_GetObjectItem(activation, "challenge");
|
||||||
if (challenge != NULL) {
|
if (cJSON_IsString(challenge)) {
|
||||||
activation_challenge_ = challenge->valuestring;
|
activation_challenge_ = challenge->valuestring;
|
||||||
has_activation_challenge_ = true;
|
has_activation_challenge_ = true;
|
||||||
}
|
}
|
||||||
cJSON* timeout_ms = cJSON_GetObjectItem(activation, "timeout_ms");
|
cJSON* timeout_ms = cJSON_GetObjectItem(activation, "timeout_ms");
|
||||||
if (timeout_ms != NULL) {
|
if (cJSON_IsNumber(timeout_ms)) {
|
||||||
activation_timeout_ms_ = timeout_ms->valueint;
|
activation_timeout_ms_ = timeout_ms->valueint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
has_mqtt_config_ = false;
|
has_mqtt_config_ = false;
|
||||||
cJSON *mqtt = cJSON_GetObjectItem(root, "mqtt");
|
cJSON *mqtt = cJSON_GetObjectItem(root, "mqtt");
|
||||||
if (mqtt != NULL) {
|
if (cJSON_IsObject(mqtt)) {
|
||||||
Settings settings("mqtt", true);
|
Settings settings("mqtt", true);
|
||||||
cJSON *item = NULL;
|
cJSON *item = NULL;
|
||||||
cJSON_ArrayForEach(item, mqtt) {
|
cJSON_ArrayForEach(item, mqtt) {
|
||||||
if (item->type == cJSON_String) {
|
if (cJSON_IsString(item)) {
|
||||||
if (settings.GetString(item->string) != item->valuestring) {
|
if (settings.GetString(item->string) != item->valuestring) {
|
||||||
settings.SetString(item->string, item->valuestring);
|
settings.SetString(item->string, item->valuestring);
|
||||||
}
|
}
|
||||||
@@ -150,13 +150,13 @@ bool Ota::CheckVersion() {
|
|||||||
|
|
||||||
has_websocket_config_ = false;
|
has_websocket_config_ = false;
|
||||||
cJSON *websocket = cJSON_GetObjectItem(root, "websocket");
|
cJSON *websocket = cJSON_GetObjectItem(root, "websocket");
|
||||||
if (websocket != NULL) {
|
if (cJSON_IsObject(websocket)) {
|
||||||
Settings settings("websocket", true);
|
Settings settings("websocket", true);
|
||||||
cJSON *item = NULL;
|
cJSON *item = NULL;
|
||||||
cJSON_ArrayForEach(item, websocket) {
|
cJSON_ArrayForEach(item, websocket) {
|
||||||
if (item->type == cJSON_String) {
|
if (cJSON_IsString(item)) {
|
||||||
settings.SetString(item->string, item->valuestring);
|
settings.SetString(item->string, item->valuestring);
|
||||||
} else if (item->type == cJSON_Number) {
|
} else if (cJSON_IsNumber(item)) {
|
||||||
settings.SetInt(item->string, item->valueint);
|
settings.SetInt(item->string, item->valueint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,17 +167,17 @@ bool Ota::CheckVersion() {
|
|||||||
|
|
||||||
has_server_time_ = false;
|
has_server_time_ = false;
|
||||||
cJSON *server_time = cJSON_GetObjectItem(root, "server_time");
|
cJSON *server_time = cJSON_GetObjectItem(root, "server_time");
|
||||||
if (server_time != NULL) {
|
if (cJSON_IsObject(server_time)) {
|
||||||
cJSON *timestamp = cJSON_GetObjectItem(server_time, "timestamp");
|
cJSON *timestamp = cJSON_GetObjectItem(server_time, "timestamp");
|
||||||
cJSON *timezone_offset = cJSON_GetObjectItem(server_time, "timezone_offset");
|
cJSON *timezone_offset = cJSON_GetObjectItem(server_time, "timezone_offset");
|
||||||
|
|
||||||
if (timestamp != NULL) {
|
if (cJSON_IsNumber(timestamp)) {
|
||||||
// 设置系统时间
|
// 设置系统时间
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
double ts = timestamp->valuedouble;
|
double ts = timestamp->valuedouble;
|
||||||
|
|
||||||
// 如果有时区偏移,计算本地时间
|
// 如果有时区偏移,计算本地时间
|
||||||
if (timezone_offset != NULL) {
|
if (cJSON_IsNumber(timezone_offset)) {
|
||||||
ts += (timezone_offset->valueint * 60 * 1000); // 转换分钟为毫秒
|
ts += (timezone_offset->valueint * 60 * 1000); // 转换分钟为毫秒
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,17 +192,17 @@ bool Ota::CheckVersion() {
|
|||||||
|
|
||||||
has_new_version_ = false;
|
has_new_version_ = false;
|
||||||
cJSON *firmware = cJSON_GetObjectItem(root, "firmware");
|
cJSON *firmware = cJSON_GetObjectItem(root, "firmware");
|
||||||
if (firmware != NULL) {
|
if (cJSON_IsObject(firmware)) {
|
||||||
cJSON *version = cJSON_GetObjectItem(firmware, "version");
|
cJSON *version = cJSON_GetObjectItem(firmware, "version");
|
||||||
if (version != NULL) {
|
if (cJSON_IsString(version)) {
|
||||||
firmware_version_ = version->valuestring;
|
firmware_version_ = version->valuestring;
|
||||||
}
|
}
|
||||||
cJSON *url = cJSON_GetObjectItem(firmware, "url");
|
cJSON *url = cJSON_GetObjectItem(firmware, "url");
|
||||||
if (url != NULL) {
|
if (cJSON_IsString(url)) {
|
||||||
firmware_url_ = url->valuestring;
|
firmware_url_ = url->valuestring;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version != NULL && url != NULL) {
|
if (cJSON_IsString(version) && cJSON_IsString(url)) {
|
||||||
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
|
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
|
||||||
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
|
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
|
||||||
if (has_new_version_) {
|
if (has_new_version_) {
|
||||||
@@ -212,7 +212,7 @@ bool Ota::CheckVersion() {
|
|||||||
}
|
}
|
||||||
// If the force flag is set to 1, the given version is forced to be installed
|
// If the force flag is set to 1, the given version is forced to be installed
|
||||||
cJSON *force = cJSON_GetObjectItem(firmware, "force");
|
cJSON *force = cJSON_GetObjectItem(firmware, "force");
|
||||||
if (force != NULL && force->valueint == 1) {
|
if (cJSON_IsNumber(force) && force->valueint == 1) {
|
||||||
has_new_version_ = true;
|
has_new_version_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,13 +38,13 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Settings settings("mqtt", false);
|
Settings settings("mqtt", false);
|
||||||
endpoint_ = settings.GetString("endpoint");
|
auto endpoint = settings.GetString("endpoint");
|
||||||
client_id_ = settings.GetString("client_id");
|
auto client_id = settings.GetString("client_id");
|
||||||
username_ = settings.GetString("username");
|
auto username = settings.GetString("username");
|
||||||
password_ = settings.GetString("password");
|
auto password = settings.GetString("password");
|
||||||
publish_topic_ = settings.GetString("publish_topic");
|
publish_topic_ = settings.GetString("publish_topic");
|
||||||
|
|
||||||
if (endpoint_.empty()) {
|
if (endpoint.empty()) {
|
||||||
ESP_LOGW(TAG, "MQTT endpoint is not specified");
|
ESP_LOGW(TAG, "MQTT endpoint is not specified");
|
||||||
if (report_error) {
|
if (report_error) {
|
||||||
SetError(Lang::Strings::SERVER_NOT_FOUND);
|
SetError(Lang::Strings::SERVER_NOT_FOUND);
|
||||||
@@ -66,8 +66,8 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cJSON* type = cJSON_GetObjectItem(root, "type");
|
cJSON* type = cJSON_GetObjectItem(root, "type");
|
||||||
if (type == nullptr) {
|
if (!cJSON_IsString(type)) {
|
||||||
ESP_LOGE(TAG, "Message type is not specified");
|
ESP_LOGE(TAG, "Message type is invalid");
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -89,17 +89,17 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
|
|||||||
last_incoming_time_ = std::chrono::steady_clock::now();
|
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;
|
std::string broker_address;
|
||||||
int broker_port = 8883;
|
int broker_port = 8883;
|
||||||
size_t pos = endpoint_.find(':');
|
size_t pos = endpoint.find(':');
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
broker_address = endpoint_.substr(0, pos);
|
broker_address = endpoint.substr(0, pos);
|
||||||
broker_port = std::stoi(endpoint_.substr(pos + 1));
|
broker_port = std::stoi(endpoint.substr(pos + 1));
|
||||||
} else {
|
} 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");
|
ESP_LOGE(TAG, "Failed to connect to endpoint");
|
||||||
SetError(Lang::Strings::SERVER_NOT_CONNECTED);
|
SetError(Lang::Strings::SERVER_NOT_CONNECTED);
|
||||||
return false;
|
return false;
|
||||||
@@ -270,26 +270,26 @@ void MqttProtocol::ParseServerHello(const cJSON* root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto session_id = cJSON_GetObjectItem(root, "session_id");
|
auto session_id = cJSON_GetObjectItem(root, "session_id");
|
||||||
if (session_id != nullptr) {
|
if (cJSON_IsString(session_id)) {
|
||||||
session_id_ = session_id->valuestring;
|
session_id_ = session_id->valuestring;
|
||||||
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
|
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get sample rate from hello message
|
// Get sample rate from hello message
|
||||||
auto audio_params = cJSON_GetObjectItem(root, "audio_params");
|
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");
|
auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate");
|
||||||
if (sample_rate != NULL) {
|
if (cJSON_IsNumber(sample_rate)) {
|
||||||
server_sample_rate_ = sample_rate->valueint;
|
server_sample_rate_ = sample_rate->valueint;
|
||||||
}
|
}
|
||||||
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
|
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
|
||||||
if (frame_duration != NULL) {
|
if (cJSON_IsNumber(frame_duration)) {
|
||||||
server_frame_duration_ = frame_duration->valueint;
|
server_frame_duration_ = frame_duration->valueint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto udp = cJSON_GetObjectItem(root, "udp");
|
auto udp = cJSON_GetObjectItem(root, "udp");
|
||||||
if (udp == nullptr) {
|
if (!cJSON_IsObject(udp)) {
|
||||||
ESP_LOGE(TAG, "UDP is not specified");
|
ESP_LOGE(TAG, "UDP is not specified");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,10 +34,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
EventGroupHandle_t event_group_handle_;
|
EventGroupHandle_t event_group_handle_;
|
||||||
|
|
||||||
std::string endpoint_;
|
|
||||||
std::string client_id_;
|
|
||||||
std::string username_;
|
|
||||||
std::string password_;
|
|
||||||
std::string publish_topic_;
|
std::string publish_topic_;
|
||||||
|
|
||||||
std::mutex channel_mutex_;
|
std::mutex channel_mutex_;
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ bool WebsocketProtocol::OpenAudioChannel() {
|
|||||||
// Parse JSON data
|
// Parse JSON data
|
||||||
auto root = cJSON_Parse(data);
|
auto root = cJSON_Parse(data);
|
||||||
auto type = cJSON_GetObjectItem(root, "type");
|
auto type = cJSON_GetObjectItem(root, "type");
|
||||||
if (type != NULL) {
|
if (cJSON_IsString(type)) {
|
||||||
if (strcmp(type->valuestring, "hello") == 0) {
|
if (strcmp(type->valuestring, "hello") == 0) {
|
||||||
ParseServerHello(root);
|
ParseServerHello(root);
|
||||||
} else {
|
} else {
|
||||||
@@ -223,19 +223,19 @@ void WebsocketProtocol::ParseServerHello(const cJSON* root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto session_id = cJSON_GetObjectItem(root, "session_id");
|
auto session_id = cJSON_GetObjectItem(root, "session_id");
|
||||||
if (session_id != nullptr) {
|
if (cJSON_IsString(session_id)) {
|
||||||
session_id_ = session_id->valuestring;
|
session_id_ = session_id->valuestring;
|
||||||
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
|
ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto audio_params = cJSON_GetObjectItem(root, "audio_params");
|
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");
|
auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate");
|
||||||
if (sample_rate != NULL) {
|
if (cJSON_IsNumber(sample_rate)) {
|
||||||
server_sample_rate_ = sample_rate->valueint;
|
server_sample_rate_ = sample_rate->valueint;
|
||||||
}
|
}
|
||||||
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
|
auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration");
|
||||||
if (frame_duration != NULL) {
|
if (cJSON_IsNumber(frame_duration)) {
|
||||||
server_frame_duration_ = frame_duration->valueint;
|
server_frame_duration_ = frame_duration->valueint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user