Add new api getnodetype, getlinktype, getelementtype and getelementtypevalue
This commit is contained in:
@@ -31,6 +31,7 @@ from .s0_base import is_link, is_pipe, is_pump, is_valve
|
|||||||
from .s0_base import is_curve
|
from .s0_base import is_curve
|
||||||
from .s0_base import is_pattern
|
from .s0_base import is_pattern
|
||||||
from .s0_base import get_nodes, get_nodes_id_and_type, get_junctions, get_reservoirs, get_tanks, get_links, get_links_id_and_type, get_pipes, get_pumps, get_valves, get_curves, get_patterns
|
from .s0_base import get_nodes, get_nodes_id_and_type, get_junctions, get_reservoirs, get_tanks, get_links, get_links_id_and_type, get_pipes, get_pumps, get_valves, get_curves, get_patterns
|
||||||
|
from .s0_base import get_node_type, get_link_type, get_element_type, get_element_type_value
|
||||||
from .s0_base import get_node_links, get_link_nodes
|
from .s0_base import get_node_links, get_link_nodes
|
||||||
from .s0_base import get_major_nodes, get_major_pipes
|
from .s0_base import get_major_nodes, get_major_pipes
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,30 @@ CURVE = 'curve'
|
|||||||
|
|
||||||
REGION = 'region'
|
REGION = 'region'
|
||||||
|
|
||||||
|
# DingZQ, 2025-02-05
|
||||||
|
'''
|
||||||
|
C++ 代码里已经定义了这些 enum 值
|
||||||
|
{
|
||||||
|
kNothing = -1,
|
||||||
|
|
||||||
|
//Node
|
||||||
|
kReservoir = 0,
|
||||||
|
kTank,
|
||||||
|
kJunction,
|
||||||
|
|
||||||
|
//Link
|
||||||
|
kPipe,
|
||||||
|
kPump,
|
||||||
|
kValve,
|
||||||
|
'''
|
||||||
|
ELEMENT_TYPES : dict[str, int] = {
|
||||||
|
RESERVOIR : 0,
|
||||||
|
TANK : 1,
|
||||||
|
JUNCTION : 2,
|
||||||
|
PIPE : 3,
|
||||||
|
PUMP : 4,
|
||||||
|
VALVE : 5,
|
||||||
|
}
|
||||||
|
|
||||||
def _get_from(name: str, id: str, base_type: str) -> Row | None:
|
def _get_from(name: str, id: str, base_type: str) -> Row | None:
|
||||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||||
@@ -65,14 +89,36 @@ def is_valve(name: str, id: str) -> bool:
|
|||||||
row = _get_from(name, id, _LINK)
|
row = _get_from(name, id, _LINK)
|
||||||
return row != None and row['type'] == VALVE
|
return row != None and row['type'] == VALVE
|
||||||
|
|
||||||
|
# DingZQ, 2025-02-05
|
||||||
|
def get_node_type(name: str, node_id: str) -> str:
|
||||||
|
row = _get_from(name, node_id, _NODE)
|
||||||
|
return row['type']
|
||||||
|
|
||||||
|
|
||||||
|
def get_link_type(name: str, link_id: str) -> str:
|
||||||
|
row = _get_from(name, link_id, _LINK)
|
||||||
|
return row['type']
|
||||||
|
|
||||||
|
def get_element_type(name: str, element_id: str) -> str:
|
||||||
|
if is_node(name, element_id):
|
||||||
|
return get_node_type(name, element_id)
|
||||||
|
elif is_link(name, element_id):
|
||||||
|
return get_link_type(name, element_id)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_element_type_value(name: str, element_id: str) -> int:
|
||||||
|
return ELEMENT_TYPES[get_element_type(name, element_id)]
|
||||||
|
|
||||||
def is_curve(name: str, id: str) -> bool:
|
def is_curve(name: str, id: str) -> bool:
|
||||||
|
|
||||||
return _get_from(name, id, _CURVE) != None
|
return _get_from(name, id, _CURVE) != None
|
||||||
|
|
||||||
|
|
||||||
def is_pattern(name: str, id: str) -> bool:
|
def is_pattern(name: str, id: str) -> bool:
|
||||||
return _get_from(name, id, _PATTERN) != None
|
return _get_from(name, id, _PATTERN) != None
|
||||||
|
|
||||||
|
|
||||||
def is_region(name: str, id: str) -> bool:
|
def is_region(name: str, id: str) -> bool:
|
||||||
return _get_from(name, id, _REGION) != None
|
return _get_from(name, id, _REGION) != None
|
||||||
|
|
||||||
|
|||||||
17
main.py
17
main.py
@@ -388,6 +388,23 @@ async def fastapi_is_pump(network: str, link: str) -> bool:
|
|||||||
async def fastapi_is_valve(network: str, link: str) -> bool:
|
async def fastapi_is_valve(network: str, link: str) -> bool:
|
||||||
return is_valve(network, link)
|
return is_valve(network, link)
|
||||||
|
|
||||||
|
# DingZQ, 2025-02-05
|
||||||
|
@app.get('/getnodetype/')
|
||||||
|
async def fastapi_get_node_type(network: str, node: str) -> str:
|
||||||
|
return get_node_type(network, node)
|
||||||
|
|
||||||
|
@app.get('/getlinktype/')
|
||||||
|
async def fastapi_get_link_type(network: str, link: str) -> str:
|
||||||
|
return get_link_type(network, link)
|
||||||
|
|
||||||
|
@app.get('/getelementtype/')
|
||||||
|
async def fastapi_get_element_type(network: str, element: str) -> str:
|
||||||
|
return get_element_type(network, element)
|
||||||
|
|
||||||
|
@app.get('/getelementtypevalue/')
|
||||||
|
async def fastapi_get_element_type_value(network: str, element: str) -> int:
|
||||||
|
return get_element_type_value(network, element)
|
||||||
|
|
||||||
@app.get('/iscurve/')
|
@app.get('/iscurve/')
|
||||||
async def fastapi_is_curve(network: str, curve: str) -> bool:
|
async def fastapi_is_curve(network: str, curve: str) -> bool:
|
||||||
return is_curve(network, curve)
|
return is_curve(network, curve)
|
||||||
|
|||||||
13
tjnetwork.py
13
tjnetwork.py
@@ -377,6 +377,19 @@ def is_curve(name: str, curve_id: str) -> bool:
|
|||||||
def is_pattern(name: str, pattern_id: str) -> bool:
|
def is_pattern(name: str, pattern_id: str) -> bool:
|
||||||
return api.is_pattern(name, pattern_id)
|
return api.is_pattern(name, pattern_id)
|
||||||
|
|
||||||
|
# DingZQ, 2025-02-05
|
||||||
|
def get_node_type(name: str, node_id: str) -> str:
|
||||||
|
return api.get_node_type(name, node_id)
|
||||||
|
|
||||||
|
def get_link_type(name: str, link_id: str) -> str:
|
||||||
|
return api.get_link_type(name, link_id)
|
||||||
|
|
||||||
|
def get_element_type(name: str, element_id: str) -> str:
|
||||||
|
return api.get_element_type(name, element_id)
|
||||||
|
|
||||||
|
def get_element_type_value(name: str, element_id: str) -> int:
|
||||||
|
return api.get_element_type_value(name, element_id)
|
||||||
|
|
||||||
def get_nodes(name: str) -> list[str]:
|
def get_nodes(name: str) -> list[str]:
|
||||||
return api.get_nodes(name)
|
return api.get_nodes(name)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user