Files
xiaozhi-esp32-server/main/xiaozhi-server/core/handle/textHandle.py
T
878809ecb4 update:修复iot的bug (#407)
* fix: 修复manual模式无法识别 (#404)

* feat(docs): 新增Issues模板

* fix: 补全core依赖

* fix: 修复manual模式无法识别

* 去除重复依赖.txt

已经有torch和torchaudio

---------

Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>

* 修复iot功能中表达式问题 (#400)

* Custom paths asr tts (#388)

* #164 自定义asr、tts缓存目录,项目启动自动创建目录

* #164 自定义asr、tts缓存目录,项目启动自动创建目录

* fix:修复语音无法找到新配置项output_file的bug

* fix:电脑不支持iot音量控制bug

---------

Co-authored-by: Junsen <66542771+Huang-junsen@users.noreply.github.com>
Co-authored-by: tang <tangyiyong@gmail.com>
Co-authored-by: shudongW <178200623@qq.com>
Co-authored-by: hrz <1710360675@qq.com>
2025-03-18 13:25:34 +08:00

49 lines
2.0 KiB
Python

from config.logger import setup_logging
import json
from core.handle.abortHandle import handleAbortMessage
from core.handle.helloHandle import handleHelloMessage
from core.handle.receiveAudioHandle import startToChat, handleAudioMessage
from core.handle.iotHandle import handleIotDescriptors, handleIotStatus
TAG = __name__
logger = setup_logging()
async def handleTextMessage(conn, message):
"""处理文本消息"""
logger.bind(tag=TAG).info(f"收到文本消息:{message}")
try:
msg_json = json.loads(message)
if isinstance(msg_json, int):
await conn.websocket.send(message)
return
if msg_json["type"] == "hello":
await handleHelloMessage(conn)
elif msg_json["type"] == "abort":
await handleAbortMessage(conn)
elif msg_json["type"] == "listen":
if "mode" in msg_json:
conn.client_listen_mode = msg_json["mode"]
logger.bind(tag=TAG).debug(f"客户端拾音模式:{conn. client_listen_mode}")
if msg_json["state"] == "start":
conn.client_have_voice = True
conn.client_voice_stop = False
elif msg_json["state"] == "stop":
conn.client_have_voice = True
conn.client_voice_stop = True
if len(conn.asr_audio) > 0:
await handleAudioMessage(conn, b'')
elif msg_json["state"] == "detect":
conn.asr_server_receive = False
conn.client_have_voice = False
conn.asr_audio.clear()
if "text" in msg_json:
await startToChat(conn, msg_json["text"])
elif msg_json["type"] == "iot":
if "descriptors" in msg_json:
await handleIotDescriptors(conn, msg_json["descriptors"])
if "states" in msg_json:
await handleIotStatus(conn, msg_json["states"])
except json.JSONDecodeError:
await conn.websocket.send(message)