Added some logging

This commit is contained in:
deckensteuerung 2018-08-22 20:48:26 +02:00
parent 01ffe5b124
commit f15a9c4ebd

25
main.py
View File

@ -8,6 +8,9 @@ from bottle import route, run
import time
import sys
import signal
import logging
logging.basicConfig(filename='pixelserver.log',level=config.LogLevel)
running = True
@ -24,12 +27,14 @@ if config.UseGui:
def run(self):
global running
logging.info("Starting GUI")
sf = config.GuiScaleFactor
screen = pygame.display.set_mode((sf*config.ScreenX, sf*config.ScreenY))
pygame.display.set_caption("Pixelserver - GUI Vis")
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
logging.debug("GUI quits app")
running = False
screen.fill((0, 255, 0))
for x in range(config.ScreenX):
@ -41,6 +46,7 @@ if config.UseGui:
pygame.draw.rect(screen, (r, g, b), pygame.Rect(sf*x, sf*y, sf, sf))
pygame.display.flip()
logging.info("Closing GUI")
class SerialWriter(threading.Thread):
def __init__(self):
@ -55,22 +61,25 @@ class SerialWriter(threading.Thread):
def run(self):
should_connect = True
ser = None
logging.info("Starting SerialWriter")
while running:
try:
if should_connect:
ser = serial.Serial(config.Serial)
should_connect = False
logging.info("Serial Opened")
with self.lock:
ser.write(b"\01")
ser.write(self.data)
time.sleep(1/120)
except:
except Exception as e:
if ser != None:
ser.close()
ser = None
logging.warning("Serial was close because: "+str(e))
should_connect = True
time.sleep(0.1)
logging.info("Closing SerialWriter")
class WatchDog(threading.Thread):
def __init__(self, check, action):
@ -80,6 +89,7 @@ class WatchDog(threading.Thread):
def run(self):
while running:
if self.check():
logging.error("Watchdog: Executed")
self.action()
time.sleep(1)
@ -93,12 +103,13 @@ class LogReader(threading.Thread):
self.log = ""
def run(self):
logging.info("LogReader started")
while running:
try:
self.log += runner.app.stderr.read(1).decode("utf-8")
except Exception as e:
time.sleep(0.1)
pass
logging.info("LogReader closed")
class AppRunner(threading.Thread):
@ -127,6 +138,7 @@ class AppRunner(threading.Thread):
def terminateApp(self):
if self.app != None:
logging.error("Terminate app "+config.Apps[self.currentApp]["name"])
# don't ask
self.app.terminate()
if self.app != None:
@ -148,6 +160,7 @@ class AppRunner(threading.Thread):
self.logreader.clear()
def run(self):
logging.info("Starting Apprunner")
while running:
with self.lock:
if self.app == None or self.app.poll() != None:
@ -165,12 +178,14 @@ class AppRunner(threading.Thread):
if config.UseGui:
self.gui.setData(data)
except:
pass
logging.debug("Exception in Apprunner.run")
self.watchdog.join()
self.serial.join()
logreader.join()
if config.UseGui:
self.gui.join()
logging.info("Close Apprunner")
def getLog(self):
return self.logreader.log
@ -212,5 +227,5 @@ def apps_running():
run(host="localhost", port=8000)
running = Falselog
running = False
runner.join()