mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 09:33:55 +08:00
同步工具异步化处理
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
"""呼叫设备工具"""
|
||||
import requests
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
from config.logger import setup_logging
|
||||
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.connection import ConnectionHandler
|
||||
@@ -35,8 +34,9 @@ call_device_function_desc = {
|
||||
}
|
||||
|
||||
|
||||
def _request_api(url: str, params: dict, headers: dict) -> requests.Response:
|
||||
return requests.get(url, params=params, headers=headers, timeout=10)
|
||||
async def _request_api(url: str, params: dict, headers: dict):
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0, connect=3.0)) as client:
|
||||
return await client.get(url, params=params, headers=headers)
|
||||
|
||||
|
||||
def _failed_reply(msg: str) -> ActionResponse:
|
||||
@@ -49,7 +49,7 @@ def _is_answering(conn: "ConnectionHandler") -> bool:
|
||||
|
||||
|
||||
@register_function("call_device", call_device_function_desc, ToolType.SYSTEM_CTL)
|
||||
def call_device(conn: "ConnectionHandler", nickname: str):
|
||||
async def call_device(conn: "ConnectionHandler", nickname: str):
|
||||
caller_mac = conn.headers.get("device-id")
|
||||
if not caller_mac:
|
||||
return _failed_reply("无法获取本机MAC地址")
|
||||
@@ -71,13 +71,13 @@ def call_device(conn: "ConnectionHandler", nickname: str):
|
||||
|
||||
# 查询通讯录并发起呼叫
|
||||
try:
|
||||
resp = _request_api(
|
||||
resp = await _request_api(
|
||||
f"{api_url}/device/address-book/call",
|
||||
params=params,
|
||||
headers=headers,
|
||||
)
|
||||
result = resp.json()
|
||||
except requests.RequestException as e:
|
||||
except httpx.HTTPError as e:
|
||||
logger.bind(tag=TAG).error(f"呼叫请求失败: {e}")
|
||||
return _failed_reply("呼叫失败,请稍后再试")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user