forked from xiaozhi/xiaozhi-esp32
使用原始字符串字面量代替转义,提高可读性 (#861)
* 使用原始字符串字面量代替转义,提高可读性 * 使用原始字符串字面量代替转义,提高可读性 * 增加一个使用ESP-IDF Monitor作为输出显示内容的类 * 修改代码风格
This commit is contained in:
@@ -106,62 +106,57 @@ std::string Board::GetJson() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
std::string json = "{";
|
std::string json = R"({"version":2,"language":")" + std::string(Lang::CODE) + R"(",)";
|
||||||
json += "\"version\":2,";
|
json += R"("flash_size":)" + std::to_string(SystemInfo::GetFlashSize()) + R"(,)";
|
||||||
json += "\"language\":\"" + std::string(Lang::CODE) + "\",";
|
json += R"("minimum_free_heap_size":")" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + R"(",)";
|
||||||
json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
|
json += R"("mac_address":")" + SystemInfo::GetMacAddress() + R"(",)";
|
||||||
json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
|
json += R"("uuid":")" + uuid_ + R"(",)";
|
||||||
json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
|
json += R"("chip_model_name":")" + SystemInfo::GetChipModelName() + R"(",)";
|
||||||
json += "\"uuid\":\"" + uuid_ + "\",";
|
|
||||||
json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
|
|
||||||
json += "\"chip_info\":{";
|
|
||||||
|
|
||||||
esp_chip_info_t chip_info;
|
esp_chip_info_t chip_info;
|
||||||
esp_chip_info(&chip_info);
|
esp_chip_info(&chip_info);
|
||||||
json += "\"model\":" + std::to_string(chip_info.model) + ",";
|
json += R"("chip_info":{)";
|
||||||
json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
|
json += R"("model":)" + std::to_string(chip_info.model) + R"(,)";
|
||||||
json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
|
json += R"("cores":)" + std::to_string(chip_info.cores) + R"(,)";
|
||||||
json += "\"features\":" + std::to_string(chip_info.features);
|
json += R"("revision":)" + std::to_string(chip_info.revision) + R"(,)";
|
||||||
json += "},";
|
json += R"("features":)" + std::to_string(chip_info.features) + R"(},)";
|
||||||
|
|
||||||
json += "\"application\":{";
|
|
||||||
auto app_desc = esp_app_get_description();
|
auto app_desc = esp_app_get_description();
|
||||||
json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
|
json += R"("application":{)";
|
||||||
json += "\"version\":\"" + std::string(app_desc->version) + "\",";
|
json += R"("name":")" + std::string(app_desc->project_name) + R"(",)";
|
||||||
json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
|
json += R"("version":")" + std::string(app_desc->version) + R"(",)";
|
||||||
json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
|
json += R"("compile_time":")" + std::string(app_desc->date) + R"(T)" + std::string(app_desc->time) + R"(Z",)";
|
||||||
|
json += R"("idf_version":")" + std::string(app_desc->idf_ver) + R"(",)";
|
||||||
char sha256_str[65];
|
char sha256_str[65];
|
||||||
for (int i = 0; i < 32; i++) {
|
for (int i = 0; i < 32; i++) {
|
||||||
snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
|
snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
|
||||||
}
|
}
|
||||||
json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
|
json += R"("elf_sha256":")" + std::string(sha256_str) + R"(")";
|
||||||
json += "},";
|
json += R"(},)";
|
||||||
|
|
||||||
json += "\"partition_table\": [";
|
json += R"("partition_table": [)";
|
||||||
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
|
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
|
||||||
while (it) {
|
while (it) {
|
||||||
const esp_partition_t *partition = esp_partition_get(it);
|
const esp_partition_t *partition = esp_partition_get(it);
|
||||||
json += "{";
|
json += R"({)";
|
||||||
json += "\"label\":\"" + std::string(partition->label) + "\",";
|
json += R"("label":")" + std::string(partition->label) + R"(",)";
|
||||||
json += "\"type\":" + std::to_string(partition->type) + ",";
|
json += R"("type":)" + std::to_string(partition->type) + R"(,)";
|
||||||
json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
|
json += R"("subtype":)" + std::to_string(partition->subtype) + R"(,)";
|
||||||
json += "\"address\":" + std::to_string(partition->address) + ",";
|
json += R"("address":)" + std::to_string(partition->address) + R"(,)";
|
||||||
json += "\"size\":" + std::to_string(partition->size);
|
json += R"("size":)" + std::to_string(partition->size) + R"(},)";;
|
||||||
json += "},";
|
|
||||||
it = esp_partition_next(it);
|
it = esp_partition_next(it);
|
||||||
}
|
}
|
||||||
json.pop_back(); // Remove the last comma
|
json.pop_back(); // Remove the last comma
|
||||||
json += "],";
|
json += R"(],)";
|
||||||
|
|
||||||
json += "\"ota\":{";
|
json += R"("ota":{)";
|
||||||
auto ota_partition = esp_ota_get_running_partition();
|
auto ota_partition = esp_ota_get_running_partition();
|
||||||
json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
|
json += R"("label":")" + std::string(ota_partition->label) + R"(")";
|
||||||
json += "},";
|
json += R"(},)";
|
||||||
|
|
||||||
json += "\"board\":" + GetBoardJson();
|
json += R"("board":)" + GetBoardJson();
|
||||||
|
|
||||||
// Close the JSON object
|
// Close the JSON object
|
||||||
json += "}";
|
json += R"(})";
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@@ -158,15 +158,17 @@ const char* WifiBoard::GetNetworkStateIcon() {
|
|||||||
std::string WifiBoard::GetBoardJson() {
|
std::string WifiBoard::GetBoardJson() {
|
||||||
// Set the board type for OTA
|
// Set the board type for OTA
|
||||||
auto& wifi_station = WifiStation::GetInstance();
|
auto& wifi_station = WifiStation::GetInstance();
|
||||||
std::string board_json = std::string("{\"type\":\"" BOARD_TYPE "\",");
|
std::string board_json = R"({)";
|
||||||
board_json += "\"name\":\"" BOARD_NAME "\",";
|
board_json += R"("type":")" + std::string(BOARD_TYPE) + R"(",)";
|
||||||
|
board_json += R"("name":")" + std::string(BOARD_NAME) + R"(",)";
|
||||||
if (!wifi_config_mode_) {
|
if (!wifi_config_mode_) {
|
||||||
board_json += "\"ssid\":\"" + wifi_station.GetSsid() + "\",";
|
board_json += R"("ssid":")" + wifi_station.GetSsid() + R"(",)";
|
||||||
board_json += "\"rssi\":" + std::to_string(wifi_station.GetRssi()) + ",";
|
board_json += R"("rssi":)" + std::to_string(wifi_station.GetRssi()) + R"(,)";
|
||||||
board_json += "\"channel\":" + std::to_string(wifi_station.GetChannel()) + ",";
|
board_json += R"("channel":)" + std::to_string(wifi_station.GetChannel()) + R"(,)";
|
||||||
board_json += "\"ip\":\"" + wifi_station.GetIpAddress() + "\",";
|
board_json += R"("ip":")" + wifi_station.GetIpAddress() + R"(",)";
|
||||||
}
|
}
|
||||||
board_json += "\"mac\":\"" + SystemInfo::GetMacAddress() + "\"}";
|
board_json += R"("mac":")" + SystemInfo::GetMacAddress() + R"(")";
|
||||||
|
board_json += R"(})";
|
||||||
return board_json;
|
return board_json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
44
main/display/esplog_display.cc
Normal file
44
main/display/esplog_display.cc
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "esplog_display.h"
|
||||||
|
|
||||||
|
#include "esp_log.h"
|
||||||
|
|
||||||
|
#define TAG "EspLogDisplay"
|
||||||
|
|
||||||
|
|
||||||
|
EspLogDisplay::EspLogDisplay()
|
||||||
|
{}
|
||||||
|
|
||||||
|
EspLogDisplay::~EspLogDisplay()
|
||||||
|
{}
|
||||||
|
|
||||||
|
void EspLogDisplay::SetStatus(const char* status)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "SetStatus: %s", status);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EspLogDisplay::ShowNotification(const char* notification, int duration_ms)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "ShowNotification: %s", notification);
|
||||||
|
}
|
||||||
|
void EspLogDisplay::ShowNotification(const std::string ¬ification, int duration_ms)
|
||||||
|
{
|
||||||
|
ShowNotification(notification.c_str(), duration_ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void EspLogDisplay::SetEmotion(const char* emotion)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "SetEmotion: %s", emotion);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EspLogDisplay::SetIcon(const char* icon)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "SetIcon: %s", icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EspLogDisplay::SetChatMessage(const char* role, const char* content)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "Role:%s", role);
|
||||||
|
ESP_LOGW(TAG, " %s", content);
|
||||||
|
}
|
||||||
|
|
||||||
28
main/display/esplog_display.h
Normal file
28
main/display/esplog_display.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef ESPLOG_DISPLAY_H_
|
||||||
|
#define ESPLOG_DISPLAY_H_
|
||||||
|
|
||||||
|
#include "display.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class EspLogDisplay : public Display {
|
||||||
|
public:
|
||||||
|
EspLogDisplay();
|
||||||
|
~EspLogDisplay();
|
||||||
|
|
||||||
|
virtual void SetStatus(const char* status);
|
||||||
|
virtual void ShowNotification(const char* notification, int duration_ms = 3000);
|
||||||
|
virtual void ShowNotification(const std::string ¬ification, int duration_ms = 3000);
|
||||||
|
virtual void SetEmotion(const char* emotion) override;
|
||||||
|
virtual void SetChatMessage(const char* role, const char* content) override;
|
||||||
|
virtual void SetIcon(const char* icon) override;
|
||||||
|
virtual inline void SetPreviewImage(const lv_img_dsc_t* image) override {}
|
||||||
|
virtual inline void SetTheme(const std::string& theme_name) override {}
|
||||||
|
virtual inline void UpdateStatusBAR(bool update_all = false) override {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual inline bool Lock(int timeout_ms = 0) override { return true; }
|
||||||
|
virtual inline void Unlock() override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user