mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 09:03:54 +08:00
update:统一工具注册及调用
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
"""设备端IoT工具模块"""
|
||||
|
||||
from .iot_descriptor import IotDescriptor
|
||||
from .iot_handler import handleIotDescriptors, handleIotStatus
|
||||
from .iot_executor import DeviceIoTExecutor
|
||||
|
||||
__all__ = [
|
||||
"IotDescriptor",
|
||||
"handleIotDescriptors",
|
||||
"handleIotStatus",
|
||||
"DeviceIoTExecutor",
|
||||
]
|
||||
@@ -0,0 +1,46 @@
|
||||
"""IoT设备描述符定义"""
|
||||
|
||||
from config.logger import setup_logging
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
class IotDescriptor:
|
||||
"""IoT设备描述符"""
|
||||
|
||||
def __init__(self, name, description, properties, methods):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.properties = []
|
||||
self.methods = []
|
||||
|
||||
# 根据描述创建属性
|
||||
if properties is not None:
|
||||
for key, value in properties.items():
|
||||
property_item = {}
|
||||
property_item["name"] = key
|
||||
property_item["description"] = value["description"]
|
||||
if value["type"] == "number":
|
||||
property_item["value"] = 0
|
||||
elif value["type"] == "boolean":
|
||||
property_item["value"] = False
|
||||
else:
|
||||
property_item["value"] = ""
|
||||
self.properties.append(property_item)
|
||||
|
||||
# 根据描述创建方法
|
||||
if methods is not None:
|
||||
for key, value in methods.items():
|
||||
method = {}
|
||||
method["description"] = value["description"]
|
||||
method["name"] = key
|
||||
# 检查方法是否有参数
|
||||
if "parameters" in value:
|
||||
method["parameters"] = {}
|
||||
for k, v in value["parameters"].items():
|
||||
method["parameters"][k] = {
|
||||
"description": v["description"],
|
||||
"type": v["type"],
|
||||
}
|
||||
self.methods.append(method)
|
||||
@@ -0,0 +1,243 @@
|
||||
"""设备端IoT工具执行器"""
|
||||
|
||||
import json
|
||||
import asyncio
|
||||
from typing import Dict, Any
|
||||
from ..base import ToolType, ToolDefinition, ToolResult, ToolExecutor, ToolAction
|
||||
|
||||
|
||||
class DeviceIoTExecutor(ToolExecutor):
|
||||
"""设备端IoT工具执行器"""
|
||||
|
||||
def __init__(self, conn):
|
||||
self.conn = conn
|
||||
self.iot_tools: Dict[str, ToolDefinition] = {}
|
||||
|
||||
async def execute(
|
||||
self, conn, tool_name: str, arguments: Dict[str, Any]
|
||||
) -> ToolResult:
|
||||
"""执行设备端IoT工具"""
|
||||
if not self.has_tool(tool_name):
|
||||
return ToolResult(
|
||||
action=ToolAction.NOT_FOUND, content=f"IoT工具 {tool_name} 不存在"
|
||||
)
|
||||
|
||||
try:
|
||||
# 解析工具名称,获取设备名和操作类型
|
||||
if tool_name.startswith("get_"):
|
||||
# 查询操作:get_devicename_property
|
||||
parts = tool_name.split("_", 2)
|
||||
if len(parts) >= 3:
|
||||
device_name = parts[1]
|
||||
property_name = parts[2]
|
||||
|
||||
value = await self._get_iot_status(device_name, property_name)
|
||||
if value is not None:
|
||||
# 处理响应模板
|
||||
response_success = arguments.get(
|
||||
"response_success", "查询成功:{value}"
|
||||
)
|
||||
response = response_success.replace("{value}", str(value))
|
||||
|
||||
return ToolResult(
|
||||
action=ToolAction.RESPONSE,
|
||||
content=str(value),
|
||||
response=response,
|
||||
)
|
||||
else:
|
||||
response_failure = arguments.get(
|
||||
"response_failure", f"无法获取{device_name}的状态"
|
||||
)
|
||||
return ToolResult(
|
||||
action=ToolAction.ERROR,
|
||||
content=f"属性{property_name}不存在",
|
||||
error=response_failure,
|
||||
)
|
||||
else:
|
||||
# 控制操作:devicename_method
|
||||
parts = tool_name.split("_", 1)
|
||||
if len(parts) >= 2:
|
||||
device_name = parts[0]
|
||||
method_name = parts[1]
|
||||
|
||||
# 提取控制参数(排除响应参数)
|
||||
control_params = {
|
||||
k: v
|
||||
for k, v in arguments.items()
|
||||
if k not in ["response_success", "response_failure"]
|
||||
}
|
||||
|
||||
# 发送IoT控制命令
|
||||
await self._send_iot_command(
|
||||
device_name, method_name, control_params
|
||||
)
|
||||
|
||||
# 等待状态更新
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
response_success = arguments.get("response_success", "操作成功")
|
||||
|
||||
# 处理响应中的占位符
|
||||
for param_name, param_value in control_params.items():
|
||||
placeholder = "{" + param_name + "}"
|
||||
if placeholder in response_success:
|
||||
response_success = response_success.replace(
|
||||
placeholder, str(param_value)
|
||||
)
|
||||
if "{value}" in response_success:
|
||||
response_success = response_success.replace(
|
||||
"{value}", str(param_value)
|
||||
)
|
||||
break
|
||||
|
||||
return ToolResult(
|
||||
action=ToolAction.REQUEST_LLM,
|
||||
content=f"{device_name}操作执行成功,请继续处理剩余指令",
|
||||
response=response_success,
|
||||
)
|
||||
|
||||
return ToolResult(action=ToolAction.ERROR, content="无法解析IoT工具名称")
|
||||
|
||||
except Exception as e:
|
||||
response_failure = arguments.get("response_failure", "操作失败")
|
||||
return ToolResult(
|
||||
action=ToolAction.ERROR, content=str(e), error=response_failure
|
||||
)
|
||||
|
||||
async def _get_iot_status(self, device_name: str, property_name: str):
|
||||
"""获取IoT设备状态"""
|
||||
for key, value in self.conn.iot_descriptors.items():
|
||||
if key == device_name:
|
||||
for property_item in value.properties:
|
||||
if property_item["name"] == property_name:
|
||||
return property_item["value"]
|
||||
return None
|
||||
|
||||
async def _send_iot_command(
|
||||
self, device_name: str, method_name: str, parameters: Dict[str, Any]
|
||||
):
|
||||
"""发送IoT控制命令"""
|
||||
for key, value in self.conn.iot_descriptors.items():
|
||||
if key == device_name:
|
||||
for method in value.methods:
|
||||
if method["name"] == method_name:
|
||||
command = {
|
||||
"name": device_name,
|
||||
"method": method_name,
|
||||
}
|
||||
|
||||
if parameters:
|
||||
command["parameters"] = parameters
|
||||
|
||||
send_message = json.dumps(
|
||||
{"type": "iot", "commands": [command]}
|
||||
)
|
||||
await self.conn.websocket.send(send_message)
|
||||
return
|
||||
|
||||
raise Exception(f"未找到设备{device_name}的方法{method_name}")
|
||||
|
||||
def register_iot_tools(self, descriptors: list):
|
||||
"""注册IoT工具"""
|
||||
for descriptor in descriptors:
|
||||
device_name = descriptor["name"]
|
||||
device_desc = descriptor["description"]
|
||||
|
||||
# 注册查询工具
|
||||
if "properties" in descriptor:
|
||||
for prop_name, prop_info in descriptor["properties"].items():
|
||||
tool_name = f"get_{device_name.lower()}_{prop_name.lower()}"
|
||||
|
||||
tool_desc = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool_name,
|
||||
"description": f"查询{device_desc}的{prop_info['description']}",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"response_success": {
|
||||
"type": "string",
|
||||
"description": f"查询成功时的友好回复,必须使用{{value}}作为占位符表示查询到的值",
|
||||
},
|
||||
"response_failure": {
|
||||
"type": "string",
|
||||
"description": f"查询失败时的友好回复",
|
||||
},
|
||||
},
|
||||
"required": ["response_success", "response_failure"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
self.iot_tools[tool_name] = ToolDefinition(
|
||||
name=tool_name,
|
||||
description=tool_desc,
|
||||
tool_type=ToolType.DEVICE_IOT,
|
||||
)
|
||||
|
||||
# 注册控制工具
|
||||
if "methods" in descriptor:
|
||||
for method_name, method_info in descriptor["methods"].items():
|
||||
tool_name = f"{device_name.lower()}_{method_name.lower()}"
|
||||
|
||||
# 构建参数
|
||||
parameters = {}
|
||||
required_params = []
|
||||
|
||||
# 添加方法的原始参数
|
||||
if "parameters" in method_info:
|
||||
parameters.update(
|
||||
{
|
||||
param_name: {
|
||||
"type": param_info["type"],
|
||||
"description": param_info["description"],
|
||||
}
|
||||
for param_name, param_info in method_info[
|
||||
"parameters"
|
||||
].items()
|
||||
}
|
||||
)
|
||||
required_params.extend(method_info["parameters"].keys())
|
||||
|
||||
# 添加响应参数
|
||||
parameters.update(
|
||||
{
|
||||
"response_success": {
|
||||
"type": "string",
|
||||
"description": "操作成功时的友好回复",
|
||||
},
|
||||
"response_failure": {
|
||||
"type": "string",
|
||||
"description": "操作失败时的友好回复",
|
||||
},
|
||||
}
|
||||
)
|
||||
required_params.extend(["response_success", "response_failure"])
|
||||
|
||||
tool_desc = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool_name,
|
||||
"description": f"{device_desc} - {method_info['description']}",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": parameters,
|
||||
"required": required_params,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
self.iot_tools[tool_name] = ToolDefinition(
|
||||
name=tool_name,
|
||||
description=tool_desc,
|
||||
tool_type=ToolType.DEVICE_IOT,
|
||||
)
|
||||
|
||||
def get_tools(self) -> Dict[str, ToolDefinition]:
|
||||
"""获取所有设备端IoT工具"""
|
||||
return self.iot_tools.copy()
|
||||
|
||||
def has_tool(self, tool_name: str) -> bool:
|
||||
"""检查是否有指定的设备端IoT工具"""
|
||||
return tool_name in self.iot_tools
|
||||
@@ -0,0 +1,86 @@
|
||||
"""IoT设备支持模块,提供IoT设备描述符和状态处理"""
|
||||
|
||||
import asyncio
|
||||
from config.logger import setup_logging
|
||||
from .iot_descriptor import IotDescriptor
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
async def handleIotDescriptors(conn, descriptors):
|
||||
"""处理物联网描述"""
|
||||
wait_max_time = 5
|
||||
while (
|
||||
not hasattr(conn, "func_handler")
|
||||
or conn.func_handler is None
|
||||
or not conn.func_handler.finish_init
|
||||
):
|
||||
await asyncio.sleep(1)
|
||||
wait_max_time -= 1
|
||||
if wait_max_time <= 0:
|
||||
logger.bind(tag=TAG).debug("连接对象没有func_handler")
|
||||
return
|
||||
|
||||
functions_changed = False
|
||||
|
||||
for descriptor in descriptors:
|
||||
# 如果descriptor没有properties和methods,则直接跳过
|
||||
if "properties" not in descriptor and "methods" not in descriptor:
|
||||
continue
|
||||
|
||||
# 处理缺失properties的情况
|
||||
if "properties" not in descriptor:
|
||||
descriptor["properties"] = {}
|
||||
# 从methods中提取所有参数作为properties
|
||||
if "methods" in descriptor:
|
||||
for method_name, method_info in descriptor["methods"].items():
|
||||
if "parameters" in method_info:
|
||||
for param_name, param_info in method_info["parameters"].items():
|
||||
# 将参数信息转换为属性信息
|
||||
descriptor["properties"][param_name] = {
|
||||
"description": param_info["description"],
|
||||
"type": param_info["type"],
|
||||
}
|
||||
|
||||
# 创建IOT设备描述符
|
||||
iot_descriptor = IotDescriptor(
|
||||
descriptor["name"],
|
||||
descriptor["description"],
|
||||
descriptor["properties"],
|
||||
descriptor["methods"],
|
||||
)
|
||||
conn.iot_descriptors[descriptor["name"]] = iot_descriptor
|
||||
functions_changed = True
|
||||
|
||||
# 如果注册了新函数,更新function描述列表
|
||||
if functions_changed and hasattr(conn, "func_handler"):
|
||||
# 注册IoT工具到统一工具处理器
|
||||
await conn.func_handler.register_iot_tools(descriptors)
|
||||
|
||||
func_names = conn.func_handler.current_support_functions()
|
||||
logger.bind(tag=TAG).info(
|
||||
f"更新function描述列表完成,当前支持的函数: {func_names}"
|
||||
)
|
||||
|
||||
|
||||
async def handleIotStatus(conn, states):
|
||||
"""处理物联网状态"""
|
||||
for state in states:
|
||||
for key, value in conn.iot_descriptors.items():
|
||||
if key == state["name"]:
|
||||
for property_item in value.properties:
|
||||
for k, v in state["state"].items():
|
||||
if property_item["name"] == k:
|
||||
if type(v) != type(property_item["value"]):
|
||||
logger.bind(tag=TAG).error(
|
||||
f"属性{property_item['name']}的值类型不匹配"
|
||||
)
|
||||
break
|
||||
else:
|
||||
property_item["value"] = v
|
||||
logger.bind(tag=TAG).info(
|
||||
f"物联网状态更新: {key} , {property_item['name']} = {v}"
|
||||
)
|
||||
break
|
||||
break
|
||||
Reference in New Issue
Block a user