Add fastapi fastapi_run_project_return_dict to return the text version of simulation results

This commit is contained in:
DingZQ
2025-02-05 15:06:00 +08:00
parent 1996ffd17d
commit 5f9b5bd310
4 changed files with 65 additions and 4 deletions

View File

@@ -1 +1 @@
from .epanet import run_project, run_inp, dump_output
from .epanet import run_project, run_project_return_dict, run_inp, dump_output

View File

@@ -238,7 +238,40 @@ def dump_output_binary(path: str) -> str:
bast64_data = base64.b64encode(data)
return str(bast64_data, 'utf-8')
#DingZQ, 2025-02-04, 返回dict[str, Any]
def run_project_return_dict(name: str, readable_output: bool = False) -> dict[str, Any]:
if not project.have_project(name):
raise Exception(f'Not found project [{name}]')
dir = os.path.abspath(os.getcwd())
db_inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.db.inp')
inp_out.dump_inp(name, db_inp, '2')
input = name + '.db'
exe = os.path.join(os.path.join(dir, 'epanet'), 'runepanet.exe')
inp = os.path.join(os.path.join(dir, 'db_inp'), input + '.inp')
rpt = os.path.join(os.path.join(dir, 'temp'), input + '.rpt')
opt = os.path.join(os.path.join(dir, 'temp'), input + '.opt')
command = f'{exe} {inp} {rpt} {opt}'
data = {}
result = os.system(command)
if result != 0:
data['simulation_result'] = 'failed'
else:
data['simulation_result'] = 'successful'
if readable_output:
data |= _dump_output(opt)
else:
data['output'] = dump_output_binary(opt)
data['report'] = dump_report(rpt)
return data
# original code
def run_project(name: str, readable_output: bool = False) -> str:
if not project.have_project(name):
raise Exception(f'Not found project [{name}]')