diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 7fa8a24ae..045a664db 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -22,6 +22,11 @@ server: # 可选:设备白名单,如果设置了白名单,那么白名单的机器无论是什么token都可以连接。 #allowed_devices: # - "24:0A:C4:1D:3B:F0" # MAC地址列表 +redis: + host: 127.0.0.1 + port: 6379 + password: "" + db: 10 log: # 设置控制台输出的日志格式,时间、日志级别、标签、消息 log_format: "{time:YY-MM-DD HH:mm:ss}[{extra[tag]}] - {level} - {message}" diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index a87bccb6c..f00b2f232 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -21,6 +21,7 @@ from config.private_config import PrivateConfig from core.auth import AuthMiddleware, AuthenticationError from core.utils.auth_code_gen import AuthCodeGenerator import plugins_func.loadplugins +import redis TAG = __name__ @@ -31,10 +32,19 @@ class TTSException(RuntimeError): class ConnectionHandler: def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent): + self.config = config self.logger = setup_logging() self.auth = AuthMiddleware(config) + self.redis_client = redis.Redis( + host=config['redis']['host'], + port=config['redis']['port'], + password=config['redis']['password'], + db=config['redis']['db'], + decode_responses=True + ) + self.websocket = None self.headers = None self.session_id = None @@ -98,6 +108,8 @@ class ConnectionHandler: self.func_handler = FunctionHandler(self.config) + + async def handle_connection(self, ws): try: # 获取并验证headers @@ -207,6 +219,9 @@ class ConnectionHandler: self.recode_first_last_text(text) future = self.executor.submit(self.speak_and_play, text) self.tts_queue.put(future) + # 封装存储逻辑 + if not await self._store_device_info(auth_code): + return False return False return True @@ -572,3 +587,28 @@ class ConnectionHandler: self.close_after_chat = True except Exception as e: self.logger.bind(tag=TAG).error(f"Chat and close error: {str(e)}") + async def _store_device_info(self, auth_code: str) -> bool: + """存储设备信息到Redis(封装方法)""" + redis_key = f"device:{auth_code}" + device_info = { + **dict(self.headers), + 'timestamp': int(time.time()), + 'auth_code': auth_code, + } + + if not device_info.get('device-id'): + self.logger.bind(tag=TAG).error("设备信息缺少device-id字段") + return False + + try: + json_data = json.dumps(device_info, ensure_ascii=False) + set_result = await self.loop.run_in_executor( + None, + lambda: self.redis_client.set(redis_key, json_data, ex=3600) + ) + self.logger.bind(tag=TAG).debug(f"设备信息存储成功: {json_data}") + return True + except redis.RedisError as e: + self.logger.bind(tag=TAG).error(f"Redis操作异常: {str(e)}") + return False + \ No newline at end of file diff --git a/main/xiaozhi-server/requirements.txt b/main/xiaozhi-server/requirements.txt index bcd6b4ee4..8b5172025 100755 --- a/main/xiaozhi-server/requirements.txt +++ b/main/xiaozhi-server/requirements.txt @@ -19,4 +19,5 @@ loguru==0.7.3 requests==2.32.3 cozepy==0.12.0 mem0ai==0.1.62 -bs4==0.0.2 \ No newline at end of file +bs4==0.0.2 +redis==4.5.1 \ No newline at end of file