from itertools import count from netrc import netrc import os import io from typing import * from fastapi import FastAPI, File, UploadFile from pydantic import BaseModel from starlette.responses import FileResponse, JSONResponse from fastapi import FastAPI, Response, status from tjnetwork_new import * JUNCTION = 0 RESERVOIR = 1 TANK = 2 PIPE = 1 NODE_COUNT = 0 LINK_COUNT = 2 prjs = [] inpDir = "C:/inpfiles/" tmpDir = "C:/tmpfiles/" if not os.path.exists(inpDir): os.mkdir(inpDir) if not os.path.exists(tmpDir): os.mkdir(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): return have_project(network) @app.post("/createproject/") async def fastapi_create_project(network: str): create_project(network) print(network) return network @app.post("/openproject/") async def fastapi_open_project(network: str): open_project(network) print(network) return network @app.post("/closeproject/") async def fastapi_close_project(network: str): close_project(network) return True @app.post("/deleteproject/") async def fastapi_delete_project(network: str): delete_project(network) return True # element operations @app.get("/getnodes/") async def fastapi_get_nodes(network: str) -> list[str]: return get_nodes(network) # junction @app.post("/addjunction/") def fastapi_add_junction(network: str, junction: str, x: float, y: float, z: float) -> ChangeSet: return add_junction(network, junction, x, y, z) @app.post("/deletejunction/") def fastapi_delete_junction(network: str, junction: str) -> ChangeSet: return delete_junction(network, junction) @app.get("/getjunctionelevation/") def fastapi_get_junction_elevation(network: str, junction: str) -> float: return get_junction_elevation(network, junction) @app.get("/getjunctioncoord/") def fastapi_get_junction_coord(network: str, junction: str) -> dict[str, float]: return get_junction_coord(network, junction) @app.post("/sje/") def fastapi_set_junction_elevation(network: str, junction: str, elevation: float) -> ChangeSet: return set_junction_elevation(network, junction, elevation) @app.post("/setjunctioncoord/") def fastapi_set_junction_coord(network: str, junction: str, x: float, y: float) -> ChangeSet: return set_junction_coord(network, junction, x, y) @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 @app.post("/uploadinp/", status_code=status.HTTP_200_OK) async def upload_inp(file: bytes = File(), name: str = None): filePath = inpDir + str(name) f = open(filePath, 'wb') f.write(file) f.close() return True @app.get("/downloadinp/", status_code=status.HTTP_200_OK) async def download_inp(name: str, response: Response): filePath = inpDir + name if os.path.exists(filePath): return FileResponse(filePath, media_type='application/octet-stream', filename="inp.inp") else: response.status_code = status.HTTP_400_BAD_REQUEST return True @app.get("/getjson/") async def get_json(): return JSONResponse( status_code = status.HTTP_400_BAD_REQUEST, content={ 'code': 400, 'message': "this is message", 'data': 123, } )