From 9178faef6d0948c0926ad3f0a1070ad394de534e Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 31 Mar 2025 21:24:48 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=8F=96=E6=B6=88=E4=BB=8E=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E6=96=87=E4=BB=B6=E8=BD=AC=E6=88=90=E6=96=87=E5=AD=97?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=AB=98=E5=93=8D=E5=BA=94=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/handle/helloHandle.py | 5 +++-- .../plugins_func/functions/handle_device.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index d765de56..41e2b6b0 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -38,7 +38,7 @@ async def checkWakeupWords(conn, text): opus_packets, duration = conn.tts.audio_to_opus_data(file) text_hello = WAKEUP_CONFIG["text"] if not text_hello: - text_hello, _ = await conn.asr.speech_to_text(opus_packets, conn.session_id) + text_hello = text conn.audio_play_queue.put((opus_packets, text_hello, 0)) if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]: asyncio.create_task(wakeupWordsResponse(conn)) @@ -74,6 +74,7 @@ async def wakeupWordsResponse(conn): if old_file is not None: os.remove(old_file) """将文件挪到"wakeup_words.mp3""" - shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type) + shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" + + WAKEUP_CONFIG["file_name"] + "." + file_type) WAKEUP_CONFIG["create_time"] = time.time() WAKEUP_CONFIG["text"] = result diff --git a/main/xiaozhi-server/plugins_func/functions/handle_device.py b/main/xiaozhi-server/plugins_func/functions/handle_device.py index 03fc9f5d..70902c55 100644 --- a/main/xiaozhi-server/plugins_func/functions/handle_device.py +++ b/main/xiaozhi-server/plugins_func/functions/handle_device.py @@ -6,6 +6,7 @@ import asyncio TAG = __name__ logger = setup_logging() + async def _get_device_status(conn, device_name, device_type, property_name): """获取设备状态""" status = await get_iot_status(conn, device_type, property_name) @@ -13,6 +14,7 @@ async def _get_device_status(conn, device_name, device_type, property_name): raise Exception(f"你的设备不支持{device_name}控制") return status + async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10): """设置设备属性""" current_value = await _get_device_status(conn, device_name, device_type, property_name) @@ -32,9 +34,11 @@ async def _set_device_property(conn, device_name, device_type, method_name, prop await send_iot_conn(conn, device_type, method_name, {property_name: current_value}) return current_value + def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs): """处理设备操作的通用函数""" - future = asyncio.run_coroutine_threadsafe(func(conn, *args, **kwargs), conn.loop) + future = asyncio.run_coroutine_threadsafe( + func(conn, *args, **kwargs), conn.loop) try: result = future.result() logger.bind(tag=TAG).info(f"{success_message}: {result}") @@ -45,9 +49,8 @@ def _handle_device_action(conn, func, success_message, error_message, *args, **k response = f"{error_message}: {e}" return ActionResponse(action=Action.RESPONSE, result=None, response=response) + # 设备控制 -# TODO: 和自动注册的iot方法重复了,也许可以删掉这里手动定义的实现? -# 另外观察到function_call准确率不够高,可能是llm的问题 handle_device_function_desc = { "type": "function", "function": { @@ -80,6 +83,7 @@ handle_device_function_desc = { } } + @register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL) def handle_device(conn, device_type: str, action: str, value: int = None): if device_type == "Speaker": @@ -88,10 +92,10 @@ def handle_device(conn, device_type: str, action: str, value: int = None): method_name, property_name, device_name = "SetBrightness", "brightness", "亮度" else: raise Exception(f"未识别的设备类型: {device_type}") - + if action not in ["get", "set", "raise", "lower"]: raise Exception(f"未识别的动作名称: {action}") - + if action == "get": # get return _handle_device_action(