重构SCADA信息获取,移除旧的数据库接口
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from psycopg import AsyncConnection
|
||||
|
||||
from app.infra.db.postgresql.scada_info import ScadaRepository
|
||||
import app.native.wndb as wndb
|
||||
from app.infra.db.postgresql.scheme import SchemeRepository
|
||||
from app.auth.project_dependencies import get_project_pg_connection
|
||||
from app.services import project_info
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -22,7 +23,9 @@ async def get_scada_info_with_connection(
|
||||
使用连接池查询所有SCADA信息
|
||||
"""
|
||||
try:
|
||||
scada_data = await ScadaRepository.get_scadas(conn)
|
||||
_ = conn
|
||||
network_name = project_info.name
|
||||
scada_data = wndb.get_all_scada_info(network_name) if network_name else []
|
||||
return {"success": True, "data": scada_data, "count": len(scada_data)}
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
from typing import List, Optional, Any
|
||||
from psycopg import AsyncConnection
|
||||
|
||||
|
||||
class ScadaRepository:
|
||||
|
||||
@staticmethod
|
||||
async def get_scadas(conn: AsyncConnection) -> List[dict]:
|
||||
"""
|
||||
查询pg数据库中,scada_info 的所有记录
|
||||
:param conn: 异步数据库连接
|
||||
:return: 包含所有记录的列表,每条记录为一个字典
|
||||
"""
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(
|
||||
"""
|
||||
SELECT id, type, associated_element_id, transmission_mode, transmission_frequency, reliability
|
||||
FROM public.scada_info
|
||||
"""
|
||||
)
|
||||
records = await cur.fetchall()
|
||||
# 将查询结果转换为字典列表(假设 record 是字典)
|
||||
scada_infos = []
|
||||
for record in records:
|
||||
scada_infos.append(
|
||||
{
|
||||
"id": record["id"], # 使用字典键
|
||||
"type": record["type"],
|
||||
"associated_element_id": record["associated_element_id"],
|
||||
"transmission_mode": record["transmission_mode"],
|
||||
"transmission_frequency": record["transmission_frequency"],
|
||||
"reliability": record["reliability"],
|
||||
}
|
||||
)
|
||||
|
||||
return scada_infos
|
||||
@@ -7,12 +7,13 @@ import numpy as np
|
||||
from app.algorithms.cleaning.flow import clean_flow_data_df_kf
|
||||
from app.algorithms.cleaning.pressure import clean_pressure_data_df_km
|
||||
from app.algorithms.health.analyzer import PipelineHealthAnalyzer
|
||||
import app.native.wndb as wndb
|
||||
|
||||
from app.infra.db.postgresql.internal_queries import InternalQueries
|
||||
from app.infra.db.postgresql.scada_info import ScadaRepository as PostgreScadaRepository
|
||||
from app.infra.db.timescaledb.repositories.realtime import RealtimeRepository
|
||||
from app.infra.db.timescaledb.repositories.scheme import SchemeRepository
|
||||
from app.infra.db.timescaledb.repositories.scada import ScadaRepository
|
||||
from app.services import project_info
|
||||
|
||||
|
||||
class CompositeQueries:
|
||||
@@ -49,7 +50,8 @@ class CompositeQueries:
|
||||
"""
|
||||
result = {}
|
||||
# 1. 查询所有 SCADA 信息
|
||||
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
|
||||
network_name = project_info.name
|
||||
scada_infos = wndb.get_all_scada_info(network_name) if network_name else []
|
||||
|
||||
for device_id in device_ids:
|
||||
# 2. 根据 device_id 找到对应的 SCADA 信息
|
||||
@@ -115,7 +117,8 @@ class CompositeQueries:
|
||||
"""
|
||||
result = {}
|
||||
# 1. 查询所有 SCADA 信息
|
||||
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
|
||||
network_name = project_info.name
|
||||
scada_infos = wndb.get_all_scada_info(network_name) if network_name else []
|
||||
|
||||
for device_id in device_ids:
|
||||
# 2. 根据 device_id 找到对应的 SCADA 信息
|
||||
@@ -300,7 +303,8 @@ class CompositeQueries:
|
||||
"""
|
||||
|
||||
# 1. 查询所有 SCADA 信息
|
||||
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
|
||||
network_name = project_info.name
|
||||
scada_infos = wndb.get_all_scada_info(network_name) if network_name else []
|
||||
|
||||
# 2. 根据 element_type 和 element_id 找到关联的 SCADA 设备
|
||||
associated_scada = None
|
||||
@@ -352,7 +356,8 @@ class CompositeQueries:
|
||||
"""
|
||||
try:
|
||||
# 获取所有 SCADA 信息
|
||||
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
|
||||
network_name = project_info.name
|
||||
scada_infos = wndb.get_all_scada_info(network_name) if network_name else []
|
||||
# 将列表转换为字典,以 device_id 为键
|
||||
scada_device_info_dict = {info["id"]: info for info in scada_infos}
|
||||
|
||||
|
||||
@@ -2,12 +2,17 @@ from .database import *
|
||||
|
||||
|
||||
def get_scada_info_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
'type' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||
'query_api_id' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'associated_element_id' : {'type': 'str' , 'optional': False , 'readonly': True } }
|
||||
return {
|
||||
"id": {"type": "str", "optional": False, "readonly": True},
|
||||
"type": {"type": "str", "optional": False, "readonly": True},
|
||||
"x": {"type": "float", "optional": False, "readonly": False},
|
||||
"y": {"type": "float", "optional": False, "readonly": False},
|
||||
"query_api_id": {"type": "str", "optional": False, "readonly": False},
|
||||
"transmission_mode": {"type": "str", "optional": True, "readonly": True},
|
||||
"transmission_frequency": {"type": "float", "optional": True, "readonly": True},
|
||||
"reliability": {"type": "float", "optional": True, "readonly": True},
|
||||
"associated_element_id": {"type": "str", "optional": False, "readonly": True},
|
||||
}
|
||||
|
||||
|
||||
def get_scada_info(name: str, id: str) -> dict[str, Any]:
|
||||
@@ -16,15 +21,19 @@ def get_scada_info(name: str, id: str) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
d = {}
|
||||
d['id'] = si['id']
|
||||
d['type'] = si['type']
|
||||
d['x'] = float(si['x_coor'])
|
||||
d['y'] = float(si['y_coor'])
|
||||
d['api_query_id'] = si['api_query_id']
|
||||
d['associated_element_id'] = si['associated_element_id']
|
||||
d["id"] = si["id"]
|
||||
d["type"] = si["type"]
|
||||
d["x"] = float(si["x_coor"])
|
||||
d["y"] = float(si["y_coor"])
|
||||
d["api_query_id"] = si["api_query_id"]
|
||||
d["transmission_mode"] = si.get("transmission_mode")
|
||||
d["transmission_frequency"] = si.get("transmission_frequency")
|
||||
d["reliability"] = si.get("reliability")
|
||||
d["associated_element_id"] = si["associated_element_id"]
|
||||
|
||||
return d
|
||||
|
||||
|
||||
def get_all_scada_info(name: str) -> list[dict[str, Any]]:
|
||||
sis = read_all(name, f"select * from scada_info")
|
||||
if sis is None:
|
||||
@@ -32,11 +41,18 @@ def get_all_scada_info(name: str) -> list[dict[str, Any]]:
|
||||
|
||||
d = []
|
||||
for si in sis:
|
||||
d.append({ 'id': si['id'],
|
||||
'type': si['type'],
|
||||
'x': float(si['x_coor']),
|
||||
'y': float(si['y_coor']),
|
||||
'api_query_id': si['api_query_id'],
|
||||
'associated_element_id': si['associated_element_id'] })
|
||||
|
||||
return d
|
||||
d.append(
|
||||
{
|
||||
"id": si["id"],
|
||||
"type": si["type"],
|
||||
"x": float(si["x_coor"]),
|
||||
"y": float(si["y_coor"]),
|
||||
"api_query_id": si["api_query_id"],
|
||||
"transmission_mode": si.get("transmission_mode"),
|
||||
"transmission_frequency": si.get("transmission_frequency"),
|
||||
"reliability": si.get("reliability"),
|
||||
"associated_element_id": si["associated_element_id"],
|
||||
}
|
||||
)
|
||||
|
||||
return d
|
||||
|
||||
Reference in New Issue
Block a user