From d1687b846448f4975cfe310971caf892e37890ea Mon Sep 17 00:00:00 2001 From: DingZQ Date: Sat, 29 Mar 2025 17:02:30 +0800 Subject: [PATCH] Add API to get all valve and pumps --- api/__init__.py | 4 ++-- api/s6_pumps.py | 21 +++++++++++++++++++++ api/s7_valves.py | 21 +++++++++++++++++++++ main.py | 10 ++++++++++ tjnetwork.py | 8 ++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 8cb10eb..23dc5ae 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -51,11 +51,11 @@ from .s5_pipes import PIPE_STATUS_OPEN, PIPE_STATUS_CLOSED, PIPE_STATUS_CV from .s5_pipes import get_pipe_schema, add_pipe, get_pipe, set_pipe, get_all_pipes from .batch_api import delete_pipe_cascade -from .s6_pumps import get_pump_schema, add_pump, get_pump, set_pump +from .s6_pumps import get_pump_schema, add_pump, get_pump, set_pump, get_all_pumps from .batch_api import delete_pump_cascade from .s7_valves import VALVES_TYPE_PRV, VALVES_TYPE_PSV, VALVES_TYPE_PBV, VALVES_TYPE_FCV, VALVES_TYPE_TCV, VALVES_TYPE_GPV -from .s7_valves import get_valve_schema, add_valve, get_valve, set_valve +from .s7_valves import get_valve_schema, add_valve, get_valve, set_valve, get_all_valves from .batch_api import delete_valve_cascade from .s8_tags import TAG_TYPE_NODE, TAG_TYPE_LINK diff --git a/api/s6_pumps.py b/api/s6_pumps.py index 6bced9c..3f10bd9 100644 --- a/api/s6_pumps.py +++ b/api/s6_pumps.py @@ -26,6 +26,27 @@ def get_pump(name: str, id: str) -> dict[str, Any]: d['pattern'] = str(p['pattern']) if p['pattern'] != None else None return d +# DingZQ, 2025-03-29 +def get_all_pumps(name: str) -> list[dict[str, Any]]: + rows = read_all(name, f"select * from pumps") + if rows == None: + return [] + + result = [] + for row in rows: + d = {} + d['id'] = str(row['id']) + d['node1'] = str(row['node1']) + d['node2'] = str(row['node2']) + d['power'] = float(row['power']) if row['power'] != None else None + d['head'] = str(row['head']) if row['head'] != None else None + d['speed'] = float(p['speed']) if p['speed'] != None else None + d['pattern'] = str(p['pattern']) if p['pattern'] != None else None + result.append(d) + + return result + + class Pump(object): def __init__(self, input: dict[str, Any]) -> None: diff --git a/api/s7_valves.py b/api/s7_valves.py index e257740..3192088 100644 --- a/api/s7_valves.py +++ b/api/s7_valves.py @@ -34,6 +34,27 @@ def get_valve(name: str, id: str) -> dict[str, Any]: d['minor_loss'] = float(p['minor_loss']) return d +def get_all_valves(name: str) -> list[dict[str, Any]]: + rows = read_all(name, f"select * from valves") + if rows == None: + return [] + + result = [] + for row in rows: + d = {} + d['id'] = str(row['id']) + d['node1'] = str(row['node1']) + d['node2'] = str(row['node2']) + d['diameter'] = float(row['diameter']) + d['v_type'] = str(row['v_type']) + d['setting'] = str(row['setting']) + d['minor_loss'] = float(row['minor_loss']) + result.append(d) + + return result + + + class Valve(object): def __init__(self, input: dict[str, Any]) -> None: diff --git a/main.py b/main.py index 9b185b2..c83fbd5 100644 --- a/main.py +++ b/main.py @@ -1077,6 +1077,11 @@ async def fastapi_set_pump_node2(network: str, pump: str, node2: str) -> ChangeS async def fastapi_get_pump_properties(network: str, pump: str) -> dict[str, Any]: return get_pump(network, pump) +# DingZQ, 2025-03-29 +@app.get('/getallpumpproperties/') +async def fastapi_get_all_pump_properties(network: str) -> list[dict[str, Any]]: + return get_all_pumps(network) + @app.post("/setpumpproperties/",response_model=None) async def fastapi_set_pump_properties(network: str, pump: str, req: Request) -> ChangeSet: props = await req.json() @@ -1172,6 +1177,11 @@ async def fastapi_set_valve_setting(network: str, valve: str, setting: float) -> async def fastapi_get_valve_properties(network: str, valve: str) -> dict[str, Any]: return get_valve(network, valve) +# DingZQ, 2025-03-29 +@app.get('/getallvalveproperties/') +async def fastapi_get_all_valve_properties(network: str) -> list[dict[str, Any]]: + return get_all_valves(network) + @app.post("/setvalveproperties/",response_model=None) async def fastapi_set_valve_properties(network: str, valve: str, req: Request) -> ChangeSet: props = await req.json() diff --git a/tjnetwork.py b/tjnetwork.py index 370e0cd..e26ac3c 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -556,6 +556,10 @@ def get_pump_schema(name: str) -> dict[str, dict[str, Any]]: def get_pump(name: str, id: str) -> dict[str, Any]: return api.get_pump(name, id) +# DingZQ, 2025-03-29 +def get_all_pumps(name: str) -> list[dict[str, Any]]: + return api.get_all_pumps(name) + def set_pump(name: str, cs: ChangeSet) -> ChangeSet: return api.set_pump(name, cs) @@ -577,6 +581,10 @@ def get_valve_schema(name: str) -> dict[str, dict[str, Any]]: def get_valve(name: str, id: str) -> dict[str, Any]: return api.get_valve(name, id) +# DingZQ, 2025-03-29 +def get_all_valves(name: str) -> list[dict[str, Any]]: + return api.get_all_valves(name) + def set_valve(name: str, cs: ChangeSet) -> ChangeSet: return api.set_valve(name, cs)