Files
TJWaterServer/epanet/epanet.py
2022-11-19 11:26:04 +08:00

57 lines
1.6 KiB
Python

import ctypes
import platform
import os
import sys
sys.path.append("..")
from api import project
from api import parser
def _verify_platform():
_platform = platform.system()
if _platform != "Windows":
raise Exception(f'Platform {_platform} unsupported (not yet)')
# readable True => json, False => binary output
def run_project(name: str, readable: bool = True) -> str:
if not project.have_project(name):
raise Exception(f'Not found project {name}')
dir = os.path.dirname(os.getcwd())
db_inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.db.inp')
parser.dump_inp(name, db_inp)
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}'
result = os.system(command)
if result != 0:
raise Exception("Failed to run simulation for project {name}")
_lib_output = ctypes.CDLL(os.path.join(os.getcwd(), 'epanet-output.dll'))
handle = ctypes.c_void_p()
def _check(result):
if result != 0:
raise Exception(f'Failed to read output {name}')
_check(_lib_output.ENR_init(ctypes.byref(handle)))
_check(_lib_output.ENR_open(handle, ctypes.c_char_p(opt.encode())))
_check(_lib_output.ENR_close(ctypes.byref(handle)))
return ''
if __name__ == '__main__':
_verify_platform()
run_project('net3')