新增 scheme 表下的字段 scheme_type scheme_name

This commit is contained in:
JIANG
2025-12-05 18:27:58 +08:00
parent 4231243b96
commit 4fbdea435b
5 changed files with 324 additions and 111 deletions

View File

@@ -4,6 +4,7 @@ from psycopg import AsyncConnection
from postgresql.scada_info import ScadaRepository as PostgreScadaRepository
from timescaledb.schemas.realtime import RealtimeRepository
from timescaledb.schemas.scheme import SchemeRepository
from timescaledb.schemas.scada import ScadaRepository
@@ -13,7 +14,7 @@ class CompositeQueries:
"""
@staticmethod
async def get_scada_associated_simulation_data(
async def get_scada_associated_realtime_simulation_data(
timescale_conn: AsyncConnection,
postgres_conn: AsyncConnection,
device_id: str,
@@ -71,6 +72,79 @@ class CompositeQueries:
else:
raise ValueError(f"Unknown SCADA type: {scada_type}")
@staticmethod
async def get_scada_associated_scheme_simulation_data(
timescale_conn: AsyncConnection,
postgres_conn: AsyncConnection,
device_id: str,
start_time: datetime,
end_time: datetime,
scheme_type: str,
scheme_name: str,
field: str,
) -> Optional[Any]:
"""
获取 SCADA 关联的 link/node 模拟值
根据传入的 SCADA device_id找到关联的 link/node
并根据对应的 type查询对应的模拟数据
Args:
timescale_conn: TimescaleDB 异步连接
postgres_conn: PostgreSQL 异步连接
device_id: SCADA 设备ID
start_time: 开始时间
end_time: 结束时间
field: 要查询的字段名
Returns:
模拟数据值,如果没有找到则返回 None
Raises:
ValueError: 当 SCADA 设备未找到或字段无效时
"""
# 1. 查询所有 SCADA 信息
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
# 2. 根据 device_id 找到对应的 SCADA 信息
target_scada = None
for scada in scada_infos:
if scada["id"] == device_id:
target_scada = scada
break
if not target_scada:
raise ValueError(f"SCADA device {device_id} not found")
# 3. 根据 type 和 associated_element_id 查询对应的模拟数据
element_id = target_scada["associated_element_id"]
scada_type = target_scada["type"]
if scada_type.lower() == "pipe_flow":
# 查询 link 模拟数据
return await SchemeRepository.get_link_field_by_scheme_and_time_range(
timescale_conn,
scheme_type,
scheme_name,
start_time,
end_time,
element_id,
field,
)
elif scada_type.lower() == "pressure":
# 查询 node 模拟数据
return await SchemeRepository.get_node_field_by_scheme_and_time_range(
timescale_conn,
scheme_type,
scheme_name,
start_time,
end_time,
element_id,
field,
)
else:
raise ValueError(f"Unknown SCADA type: {scada_type}")
@staticmethod
async def get_element_associated_scada_data(
timescale_conn: AsyncConnection,