110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
from typing import Any
|
|
|
|
from psycopg.rows import dict_row
|
|
|
|
from .connection import project_connection
|
|
from .database import read_all
|
|
|
|
|
|
def get_all_sensor_placements(name: str) -> list[dict[str, Any]]:
|
|
return read_all(name, "select * from sensor_placement")
|
|
|
|
|
|
def create_sensor_placement(
|
|
name: str,
|
|
*,
|
|
scheme_name: str,
|
|
min_diameter: int,
|
|
username: str,
|
|
sensor_location: list[str],
|
|
) -> dict[str, Any]:
|
|
with project_connection(name) as conn:
|
|
with conn.cursor(row_factory=dict_row) as cur:
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO sensor_placement (
|
|
scheme_name,
|
|
sensor_number,
|
|
min_diameter,
|
|
username,
|
|
sensor_location
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
RETURNING *
|
|
""",
|
|
(
|
|
scheme_name,
|
|
len(sensor_location),
|
|
min_diameter,
|
|
username,
|
|
sensor_location,
|
|
),
|
|
)
|
|
created = cur.fetchone()
|
|
if created is None:
|
|
raise RuntimeError("监测点方案写入失败")
|
|
return dict(created)
|
|
|
|
|
|
def get_sensor_placement(name: str, scheme_id: int) -> dict[str, Any] | None:
|
|
with project_connection(name) as conn:
|
|
with conn.cursor(row_factory=dict_row) as cur:
|
|
cur.execute(
|
|
"SELECT * FROM sensor_placement WHERE id = %s",
|
|
(scheme_id,),
|
|
)
|
|
return cur.fetchone()
|
|
|
|
|
|
def get_sensor_placement_nodes(
|
|
name: str,
|
|
node_ids: list[str],
|
|
) -> list[dict[str, Any]]:
|
|
if not node_ids:
|
|
return []
|
|
with project_connection(name) as conn:
|
|
with conn.cursor(row_factory=dict_row) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT DISTINCT ON (gj.id)
|
|
gj.id AS node_id,
|
|
gj.elevation,
|
|
ST_X(c.coord) AS project_x,
|
|
ST_Y(c.coord) AS project_y,
|
|
ST_X(gj.geom) AS map_x,
|
|
ST_Y(gj.geom) AS map_y
|
|
FROM geo_junctions_mat AS gj
|
|
JOIN coordinates AS c ON c.node = gj.id
|
|
WHERE gj.id = ANY(%s)
|
|
ORDER BY gj.id
|
|
""",
|
|
(node_ids,),
|
|
)
|
|
return list(cur.fetchall())
|
|
|
|
|
|
def update_sensor_placement(
|
|
name: str,
|
|
scheme_id: int,
|
|
*,
|
|
expected_sensor_location: list[str],
|
|
sensor_location: list[str],
|
|
) -> dict[str, Any] | None:
|
|
with project_connection(name) as conn:
|
|
with conn.cursor(row_factory=dict_row) as cur:
|
|
cur.execute(
|
|
"""
|
|
UPDATE sensor_placement
|
|
SET sensor_location = %s, sensor_number = %s
|
|
WHERE id = %s AND sensor_location = %s
|
|
RETURNING *
|
|
""",
|
|
(
|
|
sensor_location,
|
|
len(sensor_location),
|
|
scheme_id,
|
|
expected_sensor_location,
|
|
),
|
|
)
|
|
return cur.fetchone()
|