From 74509acc50fb9639de4d287755a5a60f0b6e553e Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sat, 19 Nov 2022 11:11:56 +0800 Subject: [PATCH] Start to read output --- epanet/epanet.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/epanet/epanet.py b/epanet/epanet.py index 56bb8ed..ce661be 100644 --- a/epanet/epanet.py +++ b/epanet/epanet.py @@ -6,40 +6,54 @@ sys.path.append("..") from api import project from api import parser -_lib_core = None -_lib_output = None -def _load_epanet(): +def _verify_platform(): _platform = platform.system() if _platform != "Windows": 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)) - #print(f'Load EPANET {version}') - _lib_output = ctypes.CDLL("./epanet-output.dll") + + +def _check(result): + if result != 0: + raise Exception("Failed to read output {name}") + # readable True => json, False => binary output def run_project(name: str, readable: bool = True) -> str: if not project.have_project(name): - raise Exception("No such project!") + 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) - name += '.db' - + input = name + '.db' exe = os.path.join(os.path.join(dir, 'epanet'), 'runepanet.exe') - inp = os.path.join(os.path.join(dir, 'db_inp'), name + '.inp') - rpt = os.path.join(os.path.join(dir, 'temp'), name + '.rpt') - opt = os.path.join(os.path.join(dir, 'temp'), name + '.opt') + 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}' #print(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 '' + if __name__ == '__main__': - _load_epanet() + _verify_platform() run_project('net3')