Can get log of current app under /apps/log

This commit is contained in:
deckensteuerung 2018-08-22 20:05:57 +02:00
parent 3f34d88bbc
commit 01ffe5b124

33
main.py
View File

@ -84,6 +84,23 @@ class WatchDog(threading.Thread):
time.sleep(1)
class LogReader(threading.Thread):
def __init__(self, runner):
super().__init__()
self.runner = runner
self.log = ""
def clear(self):
self.log = ""
def run(self):
while running:
try:
self.log += runner.app.stderr.read(1).decode("utf-8")
except Exception as e:
time.sleep(0.1)
pass
class AppRunner(threading.Thread):
def __init__(self):
super().__init__()
@ -97,6 +114,8 @@ class AppRunner(threading.Thread):
self.last_update = time.time()
self.watchdog = WatchDog(lambda: self.appTimedOut(), lambda: self.terminateApp())
self.watchdog.start()
self.logreader = LogReader(self)
self.logreader.start()
if config.UseGui:
self.gui = Gui()
self.gui.start()
@ -110,8 +129,10 @@ class AppRunner(threading.Thread):
if self.app != None:
# don't ask
self.app.terminate()
if self.app != None:
self.app.kill()
self.app.stdout.close()
self.app.stderr.close()
self.last_update = time.time()
self.requestApp = 0
@ -121,8 +142,10 @@ class AppRunner(threading.Thread):
def updateApp(self):
if self.app != None:
self.app.terminate()
self.app.stderr.close()
args = [os.getcwd()+"/"+config.Apps[self.requestedApp]["cmd"], str(config.ScreenX), str(config.ScreenY), self.param]
self.app = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
self.app = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
self.logreader.clear()
def run(self):
while running:
@ -145,8 +168,11 @@ class AppRunner(threading.Thread):
pass
self.watchdog.join()
self.serial.join()
logreader.join()
if config.UseGui:
self.gui.join()
def getLog(self):
return self.logreader.log
runner = AppRunner()
runner.start()
@ -175,6 +201,9 @@ def apps_start(name, param):
return "ok"
return "not_found"
@route("/apps/log")
def apps_log():
return runner.getLog()
@route("/apps/running")
def apps_running():
@ -183,5 +212,5 @@ def apps_running():
run(host="localhost", port=8000)
running = False
running = Falselog
runner.join()