小程序后端接入 talkingq 共享库并新增消息与定位能力

This commit is contained in:
stu2not
2026-04-20 20:07:35 +08:00
parent e7fe92b863
commit 24b1d1025b
22 changed files with 3469 additions and 762 deletions

View File

@@ -2,7 +2,7 @@ from collections.abc import Generator
import logging
import time
from sqlalchemy import create_engine, event, text
from sqlalchemy import bindparam, create_engine, event, text
from sqlalchemy.orm import Session, sessionmaker
try:
@@ -43,6 +43,29 @@ def set_sqlite_pragma(dbapi_conn, connection_record):
logger = logging.getLogger("app.db")
SHARED_TALKINGQ_REQUIRED_TABLES = (
"device_auth",
"device_configs",
"conversation_histories",
"conversation_messages",
"roles",
"role_languages",
"device_firmware_update",
"system_config",
"parents",
"children",
"parent_child_relations",
"device_bindings",
"device_bind_sessions",
"device_bind_history",
"cards",
"device_settings",
"im_conversations",
"im_messages",
"child_location_current",
"child_location_history",
)
@event.listens_for(engine, "before_cursor_execute")
def before_cursor_execute(
@@ -112,6 +135,46 @@ def check_db_connection() -> None:
logger.info("database connection check succeeded", extra={"event": "db_check"})
def check_shared_schema_tables() -> None:
if not settings.uses_shared_talkingq_db:
return
query = text(
"""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = :table_schema
AND table_name IN :table_names
"""
).bindparams(bindparam("table_names", expanding=True))
with engine.connect() as conn:
rows = conn.execute(
query,
{
"table_schema": settings.db_name,
"table_names": list(SHARED_TALKINGQ_REQUIRED_TABLES),
},
)
existing_tables = {row[0] for row in rows}
missing_tables = sorted(set(SHARED_TALKINGQ_REQUIRED_TABLES) - existing_tables)
if missing_tables:
raise RuntimeError(
"shared talkingq schema is incomplete; missing tables: "
+ ", ".join(missing_tables)
)
logger.info(
"shared talkingq schema check succeeded",
extra={
"event": "db_shared_schema_check",
"db_name": settings.db_name,
"table_count": len(SHARED_TALKINGQ_REQUIRED_TABLES),
},
)
def close_db_engine() -> None:
engine.dispose()
logger.info("database engine disposed", extra={"event": "db_close"})