[RTOP-23] File context in openplc.py and restapi minor fix

This commit is contained in:
lucasbutzke
2025-07-07 17:43:14 -04:00
parent 940a0b15dd
commit fa3c3cd888
6 changed files with 64 additions and 31 deletions

View File

@@ -1 +1 @@
890590.st
Dockerfile

View File

@@ -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)

View File

@@ -1 +1 @@
rpi
blank_linux

View File

@@ -1 +1 @@
rpi
linux

View File

@@ -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"]

View File

@@ -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):