From fa3c3cd88845365ea32aff074adf6bb485683ff3 Mon Sep 17 00:00:00 2001 From: lucasbutzke Date: Mon, 7 Jul 2025 17:43:14 -0400 Subject: [PATCH] [RTOP-23] File context in openplc.py and restapi minor fix --- webserver/active_program | 2 +- webserver/openplc.py | 33 ++++++++++++++---------------- webserver/scripts/openplc_driver | 2 +- webserver/scripts/openplc_platform | 2 +- webserver/st_files/Dockerfile | 31 ++++++++++++++++++++++++++++ webserver/webserver.py | 25 +++++++++++++--------- 6 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 webserver/st_files/Dockerfile diff --git a/webserver/active_program b/webserver/active_program index 5621f10..9414382 100755 --- a/webserver/active_program +++ b/webserver/active_program @@ -1 +1 @@ -890590.st +Dockerfile diff --git a/webserver/openplc.py b/webserver/openplc.py index 6980c97..f1a22f7 100644 --- a/webserver/openplc.py +++ b/webserver/openplc.py @@ -114,9 +114,9 @@ class runtime: compilation_status_str = "" # Extract debug information from program - f = open('./st_files/' + st_file, "r") - combined_lines = f.read() - f.close() + with open('./st_files/' + st_file, "r") as f: + combined_lines = f.read() + combined_lines = combined_lines.split('\n') program_lines = [] c_debug_lines = [] @@ -132,9 +132,9 @@ class runtime: # Could not find debug info on program uploaded if os.path.isfile('./st_files/' + st_file + '.dbg'): # Debugger info exists on file - open it - f = open('./st_files/' + st_file + '.dbg', "r") - c_debug = f.read() - f.close() + with open('./st_files/' + st_file + '.dbg', "r") as f: + c_debug = f.read() + else: # No debug info... probably a program generated from the old editor. Use the blank debug info just to compile the program f = open('./core/debug.blank', "r") @@ -142,9 +142,8 @@ class runtime: f.close() # Write c_debug file - f = open('./core/debug.cpp', "w") - f.write(c_debug) - f.close() + with open('./core/debug.cpp', "w") as f: + f.write(c_debug) # Start compilation a = subprocess.Popen(['./scripts/compile_program.sh', str(st_file)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -155,17 +154,15 @@ class runtime: c_debug = '\n'.join(c_debug_lines) # Write c_debug file - f = open('./core/debug.cpp', "w") - f.write(c_debug) - f.close() + with open('./core/debug.cpp', "w") as f: + f.write(c_debug) #Write program and debug files - f = open('./st_files/' + st_file, "w") - f.write(program) - f.close() - f = open('./st_files/' + st_file + '.dbg', "w") - f.write(c_debug) - f.close() + with open('./st_files/' + st_file, "w") as f: + f.write(program) + + with open('./st_files/' + st_file + '.dbg', "w") as f: + f.write(c_debug) # Start compilation a = subprocess.Popen(['./scripts/compile_program.sh', str(st_file)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) diff --git a/webserver/scripts/openplc_driver b/webserver/scripts/openplc_driver index 61aaae3..055bd00 100755 --- a/webserver/scripts/openplc_driver +++ b/webserver/scripts/openplc_driver @@ -1 +1 @@ -rpi +blank_linux diff --git a/webserver/scripts/openplc_platform b/webserver/scripts/openplc_platform index 61aaae3..a08e1f3 100755 --- a/webserver/scripts/openplc_platform +++ b/webserver/scripts/openplc_platform @@ -1 +1 @@ -rpi +linux diff --git a/webserver/st_files/Dockerfile b/webserver/st_files/Dockerfile new file mode 100644 index 0000000..a6d8cb4 --- /dev/null +++ b/webserver/st_files/Dockerfile @@ -0,0 +1,31 @@ +FROM debian:bullseye-20240722 + +COPY . /workdir + +WORKDIR /workdir + +RUN mkdir /docker_persistent + +VOLUME /docker_persistent + +# setup docker +RUN ./install.sh docker \ + && touch /docker_persistent/mbconfig.cfg \ + && touch /docker_persistent/persistent.file \ + && mkdir /docker_persistent/st_files \ + && cp /workdir/webserver/openplc.db /docker_persistent/openplc.db \ + && mv /workdir/webserver/openplc.db /workdir/webserver/openplc_default.db \ + && cp /workdir/webserver/dnp3.cfg /docker_persistent/dnp3.cfg \ + && mv /workdir/webserver/dnp3.cfg /workdir/webserver/dnp3_default.cfg \ + && cp -r /workdir/webserver/st_files/ /docker_persistent/st_files/ \ + && mv /workdir/webserver/st_files /workdir/webserver/st_files_default \ + && cp /workdir/webserver/active_program /docker_persistent/active_program \ + && mv /workdir/webserver/active_program /workdir/webserver/active_program_default \ + && ln -s /docker_persistent/mbconfig.cfg /workdir/webserver/mbconfig.cfg \ + && ln -s /docker_persistent/persistent.file /workdir/webserver/persistent.file \ + && ln -s /docker_persistent/openplc.db /workdir/webserver/openplc.db \ + && ln -s /docker_persistent/dnp3.cfg /workdir/webserver/dnp3.cfg \ + && ln -s /docker_persistent/st_files /workdir/webserver/st_files \ + && ln -s /docker_persistent/active_program /workdir/webserver/active_program + +ENTRYPOINT ["./start_openplc.sh"] diff --git a/webserver/webserver.py b/webserver/webserver.py index 5f2ed06..c46ddcf 100644 --- a/webserver/webserver.py +++ b/webserver/webserver.py @@ -48,7 +48,7 @@ def restapi_callback_get(argument: str, data: dict) -> dict: elif argument == "compilation-status": status = openplc_runtime.is_compiling - return {"compilation-status": status} + return {"is-compiling": status} elif argument == "compilation-logs": logs = openplc_runtime.compilation_status() @@ -67,14 +67,14 @@ def restapi_callback_post(argument: str, data: dict) -> dict: # TODO logging debug level print(f"POST | [{__name__}] Received argument: {argument}, data: {data}") - if argument == "upload_file": + if argument == "upload-file": try: + # TODO validate filename, content and size st_file = flask.request.files['file'] - # TODO save file print(st_file.filename) - st_file.save("st_files/") - + st_file.save(f"st_files/{st_file.filename}") return {"UploadFile": "Success"} + except: return {"UploadFile": "Fail"} @@ -82,14 +82,19 @@ def restapi_callback_post(argument: str, data: dict) -> dict: if (openplc_runtime.status() == "Compiling"): return {"RuntimeStatus": "Compiling"} - # st_file = flask.request.args.get('file') - st_file = flask.request.files['file'] - openplc_runtime.compile_program(st_file) + try: + # TODO return compilation result and validate filename + # st_file = flask.request.args.get('file') + st_file = flask.request.files['file'] + # print(f"st_files/{st_file.filename}") + openplc_runtime.compile_program(f"{st_file.filename}") + return {"CompilationStatus": "Program Compiled"} - return {"RuntimeStatus": "Program Compiled"} + except Exception as e: + return {"CompilationStatus": e} else: - return {"error": "Unknown argument"} + return {"PostError": "Unknown argument"} class User(flask_login.UserMixin):