191 lines
4.4 KiB
Python
191 lines
4.4 KiB
Python
from itertools import count
|
|
from netrc import netrc
|
|
import os
|
|
import io
|
|
from typing import Union, Optional
|
|
from fastapi import FastAPI, File, UploadFile
|
|
from pydantic import BaseModel
|
|
from starlette.responses import FileResponse
|
|
from fastapi import FastAPI, Response, status
|
|
import pytjnetwork as tj
|
|
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()
|
|
|
|
# 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):
|
|
nodeCount = get_count(network, NODE_COUNT)
|
|
nodes = []
|
|
for i in range(nodeCount.value):
|
|
node_id = get_node_id(network, i + 1)
|
|
if node_id.error_code == 0:
|
|
nodes.append(node_id.value)
|
|
|
|
return nodes
|
|
|
|
@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: int, y: int):
|
|
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):
|
|
pt = []
|
|
value = get_node_coord(network, node).value
|
|
pt.append(value.x)
|
|
pt.append(value.y)
|
|
|
|
return pt
|
|
|
|
@app.post("/setnodecoord/")
|
|
async def fastapi_set_node_coord(network: str, node: str, x: int, y: int):
|
|
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:
|
|
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 [result.value.from_node, result.value.to_node]
|
|
|
|
@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
|
|
|
|
|
|
|
|
|