mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
xiaozhi-server 增加redis存储设备信息
This commit is contained in:
parent
6eac25425c
commit
1831cd0927
3 changed files with 47 additions and 1 deletions
|
|
@ -22,6 +22,11 @@ server:
|
||||||
# 可选:设备白名单,如果设置了白名单,那么白名单的机器无论是什么token都可以连接。
|
# 可选:设备白名单,如果设置了白名单,那么白名单的机器无论是什么token都可以连接。
|
||||||
#allowed_devices:
|
#allowed_devices:
|
||||||
# - "24:0A:C4:1D:3B:F0" # MAC地址列表
|
# - "24:0A:C4:1D:3B:F0" # MAC地址列表
|
||||||
|
redis:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
password: ""
|
||||||
|
db: 10
|
||||||
log:
|
log:
|
||||||
# 设置控制台输出的日志格式,时间、日志级别、标签、消息
|
# 设置控制台输出的日志格式,时间、日志级别、标签、消息
|
||||||
log_format: "<green>{time:YY-MM-DD HH:mm:ss}</green>[<light-blue>{extra[tag]}</light-blue>] - <level>{level}</level> - <light-green>{message}</light-green>"
|
log_format: "<green>{time:YY-MM-DD HH:mm:ss}</green>[<light-blue>{extra[tag]}</light-blue>] - <level>{level}</level> - <light-green>{message}</light-green>"
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ from config.private_config import PrivateConfig
|
||||||
from core.auth import AuthMiddleware, AuthenticationError
|
from core.auth import AuthMiddleware, AuthenticationError
|
||||||
from core.utils.auth_code_gen import AuthCodeGenerator
|
from core.utils.auth_code_gen import AuthCodeGenerator
|
||||||
import plugins_func.loadplugins
|
import plugins_func.loadplugins
|
||||||
|
import redis
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
|
|
@ -31,10 +32,19 @@ class TTSException(RuntimeError):
|
||||||
|
|
||||||
class ConnectionHandler:
|
class ConnectionHandler:
|
||||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent):
|
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent):
|
||||||
|
|
||||||
self.config = config
|
self.config = config
|
||||||
self.logger = setup_logging()
|
self.logger = setup_logging()
|
||||||
self.auth = AuthMiddleware(config)
|
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.websocket = None
|
||||||
self.headers = None
|
self.headers = None
|
||||||
self.session_id = None
|
self.session_id = None
|
||||||
|
|
@ -98,6 +108,8 @@ class ConnectionHandler:
|
||||||
|
|
||||||
self.func_handler = FunctionHandler(self.config)
|
self.func_handler = FunctionHandler(self.config)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_connection(self, ws):
|
async def handle_connection(self, ws):
|
||||||
try:
|
try:
|
||||||
# 获取并验证headers
|
# 获取并验证headers
|
||||||
|
|
@ -207,6 +219,9 @@ class ConnectionHandler:
|
||||||
self.recode_first_last_text(text)
|
self.recode_first_last_text(text)
|
||||||
future = self.executor.submit(self.speak_and_play, text)
|
future = self.executor.submit(self.speak_and_play, text)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
|
# 封装存储逻辑
|
||||||
|
if not await self._store_device_info(auth_code):
|
||||||
|
return False
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -572,3 +587,28 @@ class ConnectionHandler:
|
||||||
self.close_after_chat = True
|
self.close_after_chat = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"Chat and close error: {str(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
|
||||||
|
|
||||||
|
|
@ -20,3 +20,4 @@ requests==2.32.3
|
||||||
cozepy==0.12.0
|
cozepy==0.12.0
|
||||||
mem0ai==0.1.62
|
mem0ai==0.1.62
|
||||||
bs4==0.0.2
|
bs4==0.0.2
|
||||||
|
redis==4.5.1
|
||||||
Loading…
Reference in a new issue