Files
TJWaterServer/main.py
2022-08-21 21:39:53 +08:00

105 lines
2.5 KiB
Python

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()
# project operations
@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
@app.post("/createproject/")
async def fastapi_create_project(network: str):
create_project(network)
open_project(network)
print(network)
return network
@app.post("/addnode/")
async def fastapi_add_node(network: str, node: str):
idx = add_node(network, node, JUNCTION)
print(idx)
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.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.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()
@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