diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index 619fb5c7..63849c58 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -102,7 +102,7 @@ async def main(): await asyncio.wait( [stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task], timeout=3.0, - return_when=asyncio.ALL_COMPLETED + return_when=asyncio.ALL_COMPLETED, ) print("服务器已关闭,程序退出。") diff --git a/main/xiaozhi-server/core/handle/functionHandler.py b/main/xiaozhi-server/core/handle/functionHandler.py index b09a369c..33e950b4 100644 --- a/main/xiaozhi-server/core/handle/functionHandler.py +++ b/main/xiaozhi-server/core/handle/functionHandler.py @@ -1,6 +1,12 @@ from config.logger import setup_logging import json -from plugins_func.register import FunctionRegistry, ActionResponse, Action, ToolType +from plugins_func.register import ( + FunctionRegistry, + ActionResponse, + Action, + ToolType, + DeviceTypeRegistry, +) from plugins_func.functions.hass_init import append_devices_to_prompt TAG = __name__ @@ -10,6 +16,7 @@ class FunctionHandler: def __init__(self, conn): self.conn = conn self.config = conn.config + self.device_type_registry = DeviceTypeRegistry() self.function_registry = FunctionRegistry() self.register_nessary_functions() self.register_config_functions() diff --git a/main/xiaozhi-server/core/handle/iotHandle.py b/main/xiaozhi-server/core/handle/iotHandle.py index 5b1bb9bb..5cf2d204 100644 --- a/main/xiaozhi-server/core/handle/iotHandle.py +++ b/main/xiaozhi-server/core/handle/iotHandle.py @@ -1,9 +1,8 @@ import json import asyncio -from config.logger import setup_logging from plugins_func.register import ( - device_type_registry, - register_function, + FunctionItem, + register_device_function, ActionResponse, Action, ToolType, @@ -177,7 +176,7 @@ class IotDescriptor: self.methods.append(method) -def register_device_type(descriptor): +def register_device_type(descriptor, device_type_registry): """注册设备类型及其功能""" device_name = descriptor["name"] type_id = device_type_registry.generate_device_type_id(descriptor) @@ -213,10 +212,12 @@ def register_device_type(descriptor): }, } query_func = create_iot_query_function(device_name, prop_name, prop_info) - decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)( - query_func + decorated_func = register_device_function( + func_name, func_desc, ToolType.IOT_CTL + )(query_func) + functions[func_name] = FunctionItem( + func_name, func_desc, decorated_func, ToolType.IOT_CTL ) - functions[func_name] = decorated_func # 为每个方法创建控制函数 for method_name, method_info in descriptor["methods"].items(): @@ -267,10 +268,12 @@ def register_device_type(descriptor): }, } control_func = create_iot_function(device_name, method_name, method_info) - decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)( - control_func + decorated_func = register_device_function( + func_name, func_desc, ToolType.IOT_CTL + )(control_func) + functions[func_name] = FunctionItem( + func_name, func_desc, decorated_func, ToolType.IOT_CTL ) - functions[func_name] = decorated_func device_type_registry.register_device_type(type_id, functions) return type_id @@ -289,7 +292,6 @@ async def handleIotDescriptors(conn, descriptors): functions_changed = False for descriptor in descriptors: - # 如果descriptor没有properties和methods,则直接跳过 if "properties" not in descriptor and "methods" not in descriptor: continue @@ -319,13 +321,16 @@ async def handleIotDescriptors(conn, descriptors): if conn.load_function_plugin: # 注册或获取设备类型 - type_id = register_device_type(descriptor) + device_type_registry = conn.func_handler.device_type_registry + type_id = register_device_type(descriptor, device_type_registry) device_functions = device_type_registry.get_device_functions(type_id) # 在连接级注册设备函数 if hasattr(conn, "func_handler"): - for func_name in device_functions: - conn.func_handler.function_registry.register_function(func_name) + for func_name, func_item in device_functions.items(): + conn.func_handler.function_registry.register_function( + func_name, func_item + ) conn.logger.bind(tag=TAG).info( f"注册IOT函数到function handler: {func_name}" ) diff --git a/main/xiaozhi-server/plugins_func/register.py b/main/xiaozhi-server/plugins_func/register.py index b96f2f29..873c61e1 100644 --- a/main/xiaozhi-server/plugins_func/register.py +++ b/main/xiaozhi-server/plugins_func/register.py @@ -77,7 +77,6 @@ class DeviceTypeRegistry: # 初始化函数注册字典 all_function_registry = {} -device_type_registry = DeviceTypeRegistry() def register_function(name, desc, type=None): @@ -91,13 +90,29 @@ def register_function(name, desc, type=None): return decorator +def register_device_function(name, desc, type=None): + """注册设备级别的函数到函数注册字典的装饰器""" + + def decorator(func): + logger.bind(tag=TAG).debug(f"设备函数 '{name}' 已加载") + return func + + return decorator + + class FunctionRegistry: def __init__(self): self.function_registry = {} self.logger = setup_logging() - def register_function(self, name): - # 查找all_function_registry中是否有对应的函数 + def register_function(self, name, func_item=None): + # 如果提供了func_item,直接注册 + if func_item: + self.function_registry[name] = func_item + self.logger.bind(tag=TAG).debug(f"函数 '{name}' 直接注册成功") + return func_item + + # 否则从all_function_registry中查找 func = all_function_registry.get(name) if not func: self.logger.bind(tag=TAG).error(f"函数 '{name}' 未找到")