do not allocate memory if old IoT protocol is not used

This commit is contained in:
Terrence
2025-05-23 04:26:47 +08:00
parent 5da66773d5
commit 55a1032779
2 changed files with 9 additions and 1 deletions

View File

@@ -8,22 +8,30 @@
namespace iot { namespace iot {
#if CONFIG_IOT_PROTOCOL_XIAOZHI
static std::map<std::string, std::function<Thing*()>>* thing_creators = nullptr; static std::map<std::string, std::function<Thing*()>>* thing_creators = nullptr;
#endif
void RegisterThing(const std::string& type, std::function<Thing*()> creator) { void RegisterThing(const std::string& type, std::function<Thing*()> creator) {
#if CONFIG_IOT_PROTOCOL_XIAOZHI
if (thing_creators == nullptr) { if (thing_creators == nullptr) {
thing_creators = new std::map<std::string, std::function<Thing*()>>(); thing_creators = new std::map<std::string, std::function<Thing*()>>();
} }
(*thing_creators)[type] = creator; (*thing_creators)[type] = creator;
#endif
} }
Thing* CreateThing(const std::string& type) { Thing* CreateThing(const std::string& type) {
#if CONFIG_IOT_PROTOCOL_XIAOZHI
auto creator = thing_creators->find(type); auto creator = thing_creators->find(type);
if (creator == thing_creators->end()) { if (creator == thing_creators->end()) {
ESP_LOGE(TAG, "Thing type not found: %s", type.c_str()); ESP_LOGE(TAG, "Thing type not found: %s", type.c_str());
return nullptr; return nullptr;
} }
return creator->second(); return creator->second();
#else
return nullptr;
#endif
} }
std::string Thing::GetDescriptorJson() { std::string Thing::GetDescriptorJson() {

View File

@@ -30,7 +30,7 @@ void McpServer::AddCommonTools() {
return Board::GetInstance().GetDeviceStatusJson(); return Board::GetInstance().GetDeviceStatusJson();
}); });
AddTool("self.speaker.set_volume", AddTool("self.audio_speaker.set_volume",
"Set the volume of the audio speaker. If the current volume is unknown, you must call `self.get_device_status` tool first and then call this tool.", "Set the volume of the audio speaker. If the current volume is unknown, you must call `self.get_device_status` tool first and then call this tool.",
PropertyList({ PropertyList({
Property("volume", kPropertyTypeInteger, 0, 100) Property("volume", kPropertyTypeInteger, 0, 100)