Initial commit

This commit is contained in:
DingZQ
2022-08-21 11:13:13 +08:00
commit 7ca9cb5d28
21 changed files with 279 additions and 0 deletions

82
main.py Normal file
View File

@@ -0,0 +1,82 @@
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("/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