mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 00:53:52 +08:00
fix:修复全局变量all_function_registry导致iot描述无法更新bug
This commit is contained in:
@@ -102,7 +102,7 @@ async def main():
|
|||||||
await asyncio.wait(
|
await asyncio.wait(
|
||||||
[stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task],
|
[stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task],
|
||||||
timeout=3.0,
|
timeout=3.0,
|
||||||
return_when=asyncio.ALL_COMPLETED
|
return_when=asyncio.ALL_COMPLETED,
|
||||||
)
|
)
|
||||||
print("服务器已关闭,程序退出。")
|
print("服务器已关闭,程序退出。")
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
import json
|
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
|
from plugins_func.functions.hass_init import append_devices_to_prompt
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
@@ -10,6 +16,7 @@ class FunctionHandler:
|
|||||||
def __init__(self, conn):
|
def __init__(self, conn):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
self.config = conn.config
|
self.config = conn.config
|
||||||
|
self.device_type_registry = DeviceTypeRegistry()
|
||||||
self.function_registry = FunctionRegistry()
|
self.function_registry = FunctionRegistry()
|
||||||
self.register_nessary_functions()
|
self.register_nessary_functions()
|
||||||
self.register_config_functions()
|
self.register_config_functions()
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
from config.logger import setup_logging
|
|
||||||
from plugins_func.register import (
|
from plugins_func.register import (
|
||||||
device_type_registry,
|
FunctionItem,
|
||||||
register_function,
|
register_device_function,
|
||||||
ActionResponse,
|
ActionResponse,
|
||||||
Action,
|
Action,
|
||||||
ToolType,
|
ToolType,
|
||||||
@@ -177,7 +176,7 @@ class IotDescriptor:
|
|||||||
self.methods.append(method)
|
self.methods.append(method)
|
||||||
|
|
||||||
|
|
||||||
def register_device_type(descriptor):
|
def register_device_type(descriptor, device_type_registry):
|
||||||
"""注册设备类型及其功能"""
|
"""注册设备类型及其功能"""
|
||||||
device_name = descriptor["name"]
|
device_name = descriptor["name"]
|
||||||
type_id = device_type_registry.generate_device_type_id(descriptor)
|
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)
|
query_func = create_iot_query_function(device_name, prop_name, prop_info)
|
||||||
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(
|
decorated_func = register_device_function(
|
||||||
query_func
|
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():
|
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)
|
control_func = create_iot_function(device_name, method_name, method_info)
|
||||||
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(
|
decorated_func = register_device_function(
|
||||||
control_func
|
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)
|
device_type_registry.register_device_type(type_id, functions)
|
||||||
return type_id
|
return type_id
|
||||||
@@ -289,7 +292,6 @@ async def handleIotDescriptors(conn, descriptors):
|
|||||||
functions_changed = False
|
functions_changed = False
|
||||||
|
|
||||||
for descriptor in descriptors:
|
for descriptor in descriptors:
|
||||||
|
|
||||||
# 如果descriptor没有properties和methods,则直接跳过
|
# 如果descriptor没有properties和methods,则直接跳过
|
||||||
if "properties" not in descriptor and "methods" not in descriptor:
|
if "properties" not in descriptor and "methods" not in descriptor:
|
||||||
continue
|
continue
|
||||||
@@ -319,13 +321,16 @@ async def handleIotDescriptors(conn, descriptors):
|
|||||||
|
|
||||||
if conn.load_function_plugin:
|
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)
|
device_functions = device_type_registry.get_device_functions(type_id)
|
||||||
|
|
||||||
# 在连接级注册设备函数
|
# 在连接级注册设备函数
|
||||||
if hasattr(conn, "func_handler"):
|
if hasattr(conn, "func_handler"):
|
||||||
for func_name in device_functions:
|
for func_name, func_item in device_functions.items():
|
||||||
conn.func_handler.function_registry.register_function(func_name)
|
conn.func_handler.function_registry.register_function(
|
||||||
|
func_name, func_item
|
||||||
|
)
|
||||||
conn.logger.bind(tag=TAG).info(
|
conn.logger.bind(tag=TAG).info(
|
||||||
f"注册IOT函数到function handler: {func_name}"
|
f"注册IOT函数到function handler: {func_name}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ class DeviceTypeRegistry:
|
|||||||
|
|
||||||
# 初始化函数注册字典
|
# 初始化函数注册字典
|
||||||
all_function_registry = {}
|
all_function_registry = {}
|
||||||
device_type_registry = DeviceTypeRegistry()
|
|
||||||
|
|
||||||
|
|
||||||
def register_function(name, desc, type=None):
|
def register_function(name, desc, type=None):
|
||||||
@@ -91,13 +90,29 @@ def register_function(name, desc, type=None):
|
|||||||
return decorator
|
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:
|
class FunctionRegistry:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.function_registry = {}
|
self.function_registry = {}
|
||||||
self.logger = setup_logging()
|
self.logger = setup_logging()
|
||||||
|
|
||||||
def register_function(self, name):
|
def register_function(self, name, func_item=None):
|
||||||
# 查找all_function_registry中是否有对应的函数
|
# 如果提供了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)
|
func = all_function_registry.get(name)
|
||||||
if not func:
|
if not func:
|
||||||
self.logger.bind(tag=TAG).error(f"函数 '{name}' 未找到")
|
self.logger.bind(tag=TAG).error(f"函数 '{name}' 未找到")
|
||||||
|
|||||||
Reference in New Issue
Block a user