mirror of
https://github.com/thiagoralves/OpenPLC_v3.git
synced 2025-12-16 01:26:25 +08:00
Update to python3 and deduplicate code.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user