Start to read output

This commit is contained in:
WQY\qiong
2022-11-19 11:11:56 +08:00
parent 2c43e5ad6d
commit 74509acc50

View File

@@ -6,40 +6,54 @@ sys.path.append("..")
from api import project from api import project
from api import parser from api import parser
_lib_core = None
_lib_output = None
def _load_epanet(): def _verify_platform():
_platform = platform.system() _platform = platform.system()
if _platform != "Windows": if _platform != "Windows":
raise Exception(f'Platform {_platform} unsupported (not yet)') raise Exception(f'Platform {_platform} unsupported (not yet)')
_lib_core = ctypes.CDLL("./epanet2.dll")
#version = ctypes.c_int()
#lib_core.EN_getversion(ctypes.byref(version)) def _check(result):
#print(f'Load EPANET {version}') if result != 0:
_lib_output = ctypes.CDLL("./epanet-output.dll") raise Exception("Failed to read output {name}")
# readable True => json, False => binary output # readable True => json, False => binary output
def run_project(name: str, readable: bool = True) -> str: def run_project(name: str, readable: bool = True) -> str:
if not project.have_project(name): if not project.have_project(name):
raise Exception("No such project!") raise Exception(f'Not found project {name}')
dir = os.path.dirname(os.getcwd()) dir = os.path.dirname(os.getcwd())
db_inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.db.inp') db_inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.db.inp')
parser.dump_inp(name, db_inp) parser.dump_inp(name, db_inp)
name += '.db' input = name + '.db'
exe = os.path.join(os.path.join(dir, 'epanet'), 'runepanet.exe') exe = os.path.join(os.path.join(dir, 'epanet'), 'runepanet.exe')
inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.inp') inp = os.path.join(os.path.join(dir, 'db_inp'), input + '.inp')
rpt = os.path.join(os.path.join(dir, 'temp'), name + '.rpt') rpt = os.path.join(os.path.join(dir, 'temp'), input + '.rpt')
opt = os.path.join(os.path.join(dir, 'temp'), name + '.opt') opt = os.path.join(os.path.join(dir, 'temp'), input + '.opt')
command = f'{exe} {inp} {rpt} {opt}' command = f'{exe} {inp} {rpt} {opt}'
#print(command) #print(command)
result = os.system(command) 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()
_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 '' return ''
if __name__ == '__main__': if __name__ == '__main__':
_load_epanet() _verify_platform()
run_project('net3') run_project('net3')