216 lines
5.8 KiB
Python
216 lines
5.8 KiB
Python
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 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()
|
|
|
|
# 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.get("/isprojectopen/")
|
|
async def fastapi_is_project_open(network: str):
|
|
return is_project_open(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
|
|
|
|
@app.post("/copyproject/")
|
|
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]:
|
|
return get_nodes(network)
|
|
|
|
@app.get("/isnode/")
|
|
async def fastapi_is_node(network: str, node: str) -> bool:
|
|
return is_node(network, node)
|
|
|
|
@app.get("/isjunction/")
|
|
async def fastapi_is_junction(network: str, node: str) -> bool:
|
|
return is_junction(network, node)
|
|
|
|
@app.get("/isreservoir/")
|
|
async def fastapi_is_reservoir(network: str, node: str) -> bool:
|
|
return is_reservoir(network, node)
|
|
|
|
@app.get("/istank/")
|
|
async def fastapi_is_tank(network: str, node: str) -> bool:
|
|
return is_tank(network, node)
|
|
|
|
# link
|
|
@app.get("/getlinks/")
|
|
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
|
|
@app.post("/addjunction/")
|
|
async 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/")
|
|
async def fastapi_delete_junction(network: str, junction: str) -> ChangeSet:
|
|
return delete_junction(network, junction)
|
|
|
|
@app.get("/getjunctionelevation/")
|
|
async def fastapi_get_junction_elevation(network: str, junction: str) -> float:
|
|
return get_junction_elevation(network, junction)
|
|
|
|
@app.get("/getjunctioncoord/")
|
|
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)
|
|
|
|
@app.post("/setjunctioncoord/")
|
|
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)
|
|
|
|
|
|
# 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)
|
|
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,
|
|
}
|
|
)
|
|
|
|
|