From a3aff63eab64495906561f33bbb03577b755c06d Mon Sep 17 00:00:00 2001 From: Lucas Vinicius Hartmann Date: Wed, 6 Sep 2023 23:23:52 -0300 Subject: [PATCH] Update to python3 and deduplicate code. --- webserver/openplc.py | 176 ++++++++++------------------------------- webserver/webserver.py | 3 - 2 files changed, 42 insertions(+), 137 deletions(-) diff --git a/webserver/openplc.py b/webserver/openplc.py index cb7a025..df3a4a5 100644 --- a/webserver/openplc.py +++ b/webserver/openplc.py @@ -4,7 +4,8 @@ import socket import errno import time from threading import Thread -from Queue import Queue, Empty +from queue import Queue, Empty +import io intervals = ( ('weeks', 604800), # 60 * 60 * 24 * 7 @@ -36,7 +37,7 @@ class NonBlockingStreamReader: Usually a process' stdout or stderr. ''' - self._s = stream + self._s = io.TextIOWrapper(stream, encoding='utf-8') self._q = Queue() def _populateQueue(stream, queue): @@ -49,7 +50,7 @@ class NonBlockingStreamReader: line = stream.readline() if line: queue.put(line) - if (line.find("Compilation finished with errors!") >= 0 or line.find("Compilation finished successfully!") >= 0): + if "Compilation finished with errors!" in line or "Compilation finished successfully!" in line: self.end_of_stream = True else: self.end_of_stream = True @@ -78,22 +79,29 @@ class runtime: if (self.status() == "Stopped"): self.theprocess = subprocess.Popen(['./core/openplc']) # XXX: iPAS self.runtime_status = "Running" - + + def _rpc(self, msg, timeout=1000): + if not self.runtime_status == "Running": + return "" + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('localhost', 43628)) + s.send(f'{msg}\n'.encode('utf-8')) + data = s.recv(timeout).decode('utf-8') + s.close() + self.runtime_status = "Running" + except socket.error as serr: + print(f'Socket error during {msg}, is the runtime active?') + self.runtime_status = "Stopped" + return data + def stop_runtime(self): if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('quit()\n') - data = s.recv(1000) - s.close() - self.runtime_status = "Stopped" + self._rpc(f'quit()') + self.runtime_status = "Stopped" - while self.theprocess.poll() is None: # XXX: iPAS, to prevent the defunct killed process. - time.sleep(1) # https://www.reddit.com/r/learnpython/comments/776r96/defunct_python_process_when_using_subprocesspopen/ - - except socket.error as serr: - print("Failed to stop the runtime. Error: " + str(serr)) + while self.theprocess.poll() is None: # XXX: iPAS, to prevent the defunct killed process. + time.sleep(1) # https://www.reddit.com/r/learnpython/comments/776r96/defunct_python_process_when_using_subprocesspopen/ def compile_program(self, st_file): if (self.status() == "Running"): @@ -114,143 +122,43 @@ class runtime: if not line: break compilation_status_str += line return compilation_status_str - + def status(self): if ('compilation_object' in globals()): if (compilation_object.end_of_stream == False): return "Compiling" - - #If it is running, make sure that it really is running - if (self.runtime_status == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('exec_time()\n') - data = s.recv(10000) - s.close() - self.runtime_status = "Running" - except socket.error as serr: - print("OpenPLC Runtime is not running. Error: " + str(serr)) - self.runtime_status = "Stopped" - + + if not self._rpc('exec_time()', 10000): + self.runtime_status = "Stopped" + return self.runtime_status - + def start_modbus(self, port_num): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('start_modbus(' + str(port_num) + ')\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") - + return self._rpc(f'start_modbus({port_num})') + def stop_modbus(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('stop_modbus()\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'stop_modbus()') def start_dnp3(self, port_num): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('start_dnp3(' + str(port_num) + ')\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'start_dnp3({port_num})') def stop_dnp3(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('stop_dnp3()\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'stop_dnp3()') def start_enip(self, port_num): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('start_enip(' + str(port_num) + ')\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") - + return self._rpc(f'start_enip({port_num})') + def stop_enip(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('stop_enip()\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'stop_enip()') def start_pstorage(self, poll_rate): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('start_pstorage(' + str(poll_rate) + ')\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'start_pstorage({poll_rate})') def stop_pstorage(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('stop_pstorage()\n') - data = s.recv(1000) - s.close() - except: - print("Error connecting to OpenPLC runtime") + return self._rpc(f'stop_pstorage()') def logs(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('runtime_logs()\n') - data = s.recv(1000000) - s.close() - return data - except: - print("Error connecting to OpenPLC runtime") - - return "Error connecting to OpenPLC runtime" - else: - return "OpenPLC Runtime is not running" + return self._rpc(f'runtime_logs()',1000000) def exec_time(self): - if (self.status() == "Running"): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(('localhost', 43628)) - s.send('exec_time()\n') - data = s.recv(10000) - s.close() - return display_time(int(data), 4) - except: - print("Error connecting to OpenPLC runtime") - - return "Error connecting to OpenPLC runtime" - else: - return "N/A" + return self._rpc(f'exec_time()',10000) or "N/A" diff --git a/webserver/webserver.py b/webserver/webserver.py index d00ef2c..6b50c0f 100644 --- a/webserver/webserver.py +++ b/webserver/webserver.py @@ -2374,9 +2374,6 @@ if __name__ == '__main__': st_file = file.read() st_file = st_file.replace('\r','').replace('\n','') - reload(sys) - sys.setdefaultencoding('UTF8') - database = "openplc.db" conn = create_connection(database) if (conn != None):