|
|
|
@ -1,27 +1,20 @@
|
|
|
|
|
import logging
|
|
|
|
|
import asyncio
|
|
|
|
|
import uuid
|
|
|
|
|
from aiomysql import create_pool
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
# MYSQL配置信息
|
|
|
|
|
MYSQL_HOST = "10.10.14.203"
|
|
|
|
|
MYSQL_PORT = 3306
|
|
|
|
|
MYSQL_USER = "root"
|
|
|
|
|
MYSQL_PASSWORD = "Password123@mysql"
|
|
|
|
|
MYSQL_DB_NAME = "xiaozhi_esp32_server"
|
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# 全局 MySQL 连接池
|
|
|
|
|
mysql_pool = None
|
|
|
|
|
|
|
|
|
|
# MySQL 配置
|
|
|
|
|
MYSQL_CONFIG = {
|
|
|
|
|
"host": MYSQL_HOST,
|
|
|
|
|
"port": MYSQL_PORT,
|
|
|
|
|
"user": MYSQL_USER,
|
|
|
|
|
"password": MYSQL_PASSWORD,
|
|
|
|
|
"db": MYSQL_DB_NAME,
|
|
|
|
|
"host": "10.10.14.203",
|
|
|
|
|
"port": 3306,
|
|
|
|
|
"user": "root",
|
|
|
|
|
"password": "Password123@mysql",
|
|
|
|
|
"db": "xiaozhi_esp32_server",
|
|
|
|
|
"minsize": 1,
|
|
|
|
|
"maxsize": 20,
|
|
|
|
|
"autocommit": True,
|
|
|
|
@ -31,32 +24,28 @@ MYSQL_CONFIG = {
|
|
|
|
|
|
|
|
|
|
async def init_mysql_pool():
|
|
|
|
|
"""初始化 MySQL 连接池"""
|
|
|
|
|
global mysql_pool
|
|
|
|
|
try:
|
|
|
|
|
pool = await create_pool(**MYSQL_CONFIG)
|
|
|
|
|
mysql_pool = await create_pool(**MYSQL_CONFIG)
|
|
|
|
|
logger.info("MySQL连接池创建成功")
|
|
|
|
|
return pool
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"创建MySQL连接池失败: {str(e)}")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def truncate_chat_log(pool):
|
|
|
|
|
"""清空聊天历史表"""
|
|
|
|
|
try:
|
|
|
|
|
async with pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute("TRUNCATE TABLE t_chat_history")
|
|
|
|
|
logger.info("表 t_chat_history 已清空")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"清空表失败: {str(e)}")
|
|
|
|
|
raise
|
|
|
|
|
async def close_mysql_pool():
|
|
|
|
|
"""关闭 MySQL 连接池"""
|
|
|
|
|
global mysql_pool
|
|
|
|
|
if mysql_pool:
|
|
|
|
|
mysql_pool.close()
|
|
|
|
|
await mysql_pool.wait_closed()
|
|
|
|
|
logger.info("MySQL连接池已关闭")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def insert_chat_log(pool, chat_text, chat_wav, ai_text, person_id, person_name):
|
|
|
|
|
"""插入聊天记录"""
|
|
|
|
|
async def save_chat_history(chat_text, chat_wav, ai_text, person_id, person_name):
|
|
|
|
|
"""保存聊天记录"""
|
|
|
|
|
try:
|
|
|
|
|
async with pool.acquire() as conn:
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
sql = """
|
|
|
|
@ -65,20 +54,19 @@ async def insert_chat_log(pool, chat_text, chat_wav, ai_text, person_id, person_
|
|
|
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
|
|
|
"""
|
|
|
|
|
await cur.execute(sql, (chat_text, chat_wav, ai_text, person_id, person_name))
|
|
|
|
|
logger.info(f"成功插入聊天记录: {person_name}")
|
|
|
|
|
logger.info(f"成功保存聊天记录: {person_name}")
|
|
|
|
|
return cur.lastrowid
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"插入聊天记录失败: {str(e)}")
|
|
|
|
|
logger.error(f"保存聊天记录失败: {str(e)}")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def update_chat_log(pool, chat_id, chat_text=None, chat_wav=None, ai_text=None, person_name=None):
|
|
|
|
|
async def update_chat_history(chat_id, chat_text=None, chat_wav=None, ai_text=None, person_name=None):
|
|
|
|
|
"""更新聊天记录"""
|
|
|
|
|
try:
|
|
|
|
|
async with pool.acquire() as conn:
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 构建更新语句
|
|
|
|
|
updates = []
|
|
|
|
|
params = []
|
|
|
|
|
if chat_text is not None:
|
|
|
|
@ -107,10 +95,10 @@ async def update_chat_log(pool, chat_id, chat_text=None, chat_wav=None, ai_text=
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_chat_log(pool, chat_id):
|
|
|
|
|
async def delete_chat_history(chat_id):
|
|
|
|
|
"""删除聊天记录"""
|
|
|
|
|
try:
|
|
|
|
|
async with pool.acquire() as conn:
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
sql = "DELETE FROM t_chat_history WHERE id = %s"
|
|
|
|
@ -121,47 +109,21 @@ async def delete_chat_log(pool, chat_id):
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
"""主函数"""
|
|
|
|
|
pool = None
|
|
|
|
|
async def get_chat_history(person_id=None, limit=10):
|
|
|
|
|
"""获取聊天记录"""
|
|
|
|
|
try:
|
|
|
|
|
# 初始化连接池
|
|
|
|
|
pool = await init_mysql_pool()
|
|
|
|
|
|
|
|
|
|
# 清空表
|
|
|
|
|
await truncate_chat_log(pool)
|
|
|
|
|
|
|
|
|
|
# 测试插入
|
|
|
|
|
person_id = str(uuid.uuid4())
|
|
|
|
|
chat_id = await insert_chat_log(
|
|
|
|
|
pool=pool,
|
|
|
|
|
chat_text="你好,小智",
|
|
|
|
|
chat_wav="test.wav",
|
|
|
|
|
ai_text="你好,有什么我可以帮你的吗?",
|
|
|
|
|
person_id=person_id,
|
|
|
|
|
person_name="测试用户"
|
|
|
|
|
)
|
|
|
|
|
logger.info(f"插入的聊天记录ID: {chat_id}")
|
|
|
|
|
|
|
|
|
|
# 测试更新
|
|
|
|
|
await update_chat_log(
|
|
|
|
|
pool=pool,
|
|
|
|
|
chat_id=chat_id,
|
|
|
|
|
ai_text="你好,很高兴为你服务!",
|
|
|
|
|
person_name="更新后的用户"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 测试删除
|
|
|
|
|
await delete_chat_log(pool, chat_id)
|
|
|
|
|
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
if person_id:
|
|
|
|
|
sql = "SELECT * FROM t_chat_history WHERE person_id = %s ORDER BY id DESC LIMIT %s"
|
|
|
|
|
await cur.execute(sql, (person_id, limit))
|
|
|
|
|
else:
|
|
|
|
|
sql = "SELECT * FROM t_chat_history ORDER BY id DESC LIMIT %s"
|
|
|
|
|
await cur.execute(sql, (limit,))
|
|
|
|
|
|
|
|
|
|
result = await cur.fetchall()
|
|
|
|
|
return result
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"程序执行出错: {str(e)}")
|
|
|
|
|
finally:
|
|
|
|
|
if pool:
|
|
|
|
|
pool.close()
|
|
|
|
|
await pool.wait_closed()
|
|
|
|
|
logger.info("MySQL连接池已关闭")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
asyncio.run(main())
|
|
|
|
|
logger.error(f"获取聊天记录失败: {str(e)}")
|
|
|
|
|
raise
|