From 6f1d836df1af6e90176644248012989245e8cde7 Mon Sep 17 00:00:00 2001 From: DingZQ Date: Sat, 17 Sep 2022 14:01:44 +0800 Subject: [PATCH] Add more fastapi API --- main.py | 171 ++++++++++++++++++++++---------------------------------- 1 file changed, 66 insertions(+), 105 deletions(-) diff --git a/main.py b/main.py index d347e4e..0c4b5ed 100644 --- a/main.py +++ b/main.py @@ -28,19 +28,6 @@ if not os.path.exists(tmpDir): app = FastAPI() -# undo/redo -@app.post("/undo/") -async def fastapi_undo(network: str): - undo(network) - - return True - -@app.post("/redo/") -async def fastapi_redo(network: str): - redo(network) - - return True - # project operations @app.get("/haveproject/") async def fastapi_have_project(network: str): @@ -79,6 +66,19 @@ async def fastapi_copy_project(source: str, target: str): copy_project(source, target) return True +# undo/redo +@app.post("/undo/") +async def fastapi_undo(network: str): + undo(network) + + return True + +@app.post("/redo/") +async def fastapi_redo(network: str): + redo(network) + + return True + # node @app.get("/getnodes/") async def fastapi_get_nodes(network: str) -> list[str]: @@ -100,7 +100,44 @@ async def fastapi_is_reservoir(network: str, node: str) -> bool: async def fastapi_is_tank(network: str, node: str) -> bool: return is_tank(network, node) +# link +@app.get("/getnodes/") +async def fastapi_get_links(network: str) -> list[str]: + return get_links(network) +@app.get("/islink/") +def fastapi_is_link(network: str, link: str) -> bool: + return is_link(network, link) + +@app.get("/ispipe/") +def fastapi_is_pipe(network: str, link: str) -> bool: + return is_pipe(network, link) + +@app.get("/ispump/") +def fastapi_is_pump(network: str, link: str) -> bool: + return is_pump(network, link) + +@app.get("/isvalve/") +def is_valve(network: str, link: str) -> bool: + return is_valve(network, link) + +# curve +@app.get("/getcurves/") +def fastapi_get_curves(network: str) -> list[str]: + return get_curves(network) + +@app.get("/iscurve/") +def fastapi_is_curve(network: str, curve: str) -> bool: + return is_curve(network, curve) + +# parttern +@app.get("/getpatterns/") +def fastapi_get_patterns(network: str) -> list[str]: + return get_patterns(network) + +@app.get("/ispattern/") +def fastapi_is_curve(network: str, pattern: str) -> bool: + return is_pattern(network, pattern) # junction @@ -120,6 +157,14 @@ async def fastapi_get_junction_elevation(network: str, junction: str) -> float: async def fastapi_get_junction_coord(network: str, junction: str) -> dict[str, float]: return get_junction_coord(network, junction) +@app.get("/getjunctiondemand/") +async def fastapi_get_junction_demand(network: str, junction: str) -> float: + return get_junction_demand(network, junction) + +@app.get("/getjunctionpattern/") +async def fastapi_get_junction_pattern(network: str, junction: str) -> str: + return get_junction_pattern(network, junction) + @app.post("/setjunctionelevation/") async def fastapi_set_junction_elevation(network: str, junction: str, elevation: float) -> ChangeSet: return set_junction_elevation(network, junction, elevation) @@ -128,100 +173,16 @@ async def fastapi_set_junction_elevation(network: str, junction: str, elevation: async def fastapi_set_junction_coord(network: str, junction: str, x: float, y: float) -> ChangeSet: return set_junction_coord(network, junction, x, y) +@app.post("/setjunctiondemand/") +async def fastapi_set_junction_demand(network: str, junction: str, demand: float) -> ChangeSet: + return set_junction_demand(network, junction, demand) + +@app.post("/setjunctionpattern/") +async def fastapi_set_junction_pattern(network: str, junction: str, pattern: str) -> ChangeSet: + return set_junction_pattern(network, junction, pattern) -@app.post("/addnode/") -async def fastapi_add_node(network: str, node: str): - idx = add_node(network, node, JUNCTION) - print(idx) - print("add node") - count = get_count(network, NODE_COUNT) - print(count.value) - - return idx - -@app.post("/addnodewithcoord/") -async def fastapi_add_node_with_coord(network: str, node: str, x: float, y: float): - idx = add_node(network, node, JUNCTION) - print(idx) - count = get_count(network, NODE_COUNT) - print(count.value) - set_node_coord(network, node, x, y) - - return idx - -@app.post("/deletenode/") -async def fastapi_delete_node(network: str, node: str): - delete_node(network, node) - return True - -@app.get("/countnode/") -async def fastapi_count_node(network: str): - count = get_count(network, NODE_COUNT) - print(count) - - return count.value - -@app.get("/getnodecoord/") -async def fastapi_get_node_coord(network: str, node: str): - value = get_node_coord(network, node).value - - return JSONResponse( - status_code = status.HTTP_200_OK, - content = - { - 'x': value.x, - 'y': value.y - } - ) - -async def fastapi_set_node_coord(network: str, node: str, x: float, y: float): - set_node_coord(network, node, x, y) - - return True - -@app.get("/getlinks/") -async def fastapi_get_links(network: str): - linkCount = get_count(network, LINK_COUNT) - links = [] - for i in range(linkCount.value): - link_id = get_link_id(network, i + 1) - if link_id.error_code == 0 and len(link_id.value) > 0: - links.append(link_id.value) - - return links - -@app.get("/getlinknodes/") -async def fastapi_get_link_nodes(network: str, link: str): - result = get_link_nodes(network, link) - - return JSONResponse( - status_code = status.HTTP_200_OK, - content={ - 'start': str(result.value.from_node, 'utf-8'), - 'end': str(result.value.to_node, 'utf-8') - }) - -@app.post("/addlink/") -async def fastapi_add_link(network: str, link: str, fromNode: str, toNode: str): - idx = add_link(network, link, PIPE, fromNode, toNode) - print(idx) - - return idx - -@app.post("/deletelink/") -async def fastapi_delete_link(network: str, link: str): - delete_link(network, link) - - return True - -@app.get("/countlink/") -async def fastapi_count_link(network: str): - count = get_count(network, LINK_COUNT) - print(count) - - return count.value - +# inp file @app.post("/uploadinp/", status_code=status.HTTP_200_OK) async def upload_inp(file: bytes = File(), name: str = None): filePath = inpDir + str(name)