Cleaner code
This commit is contained in:
parent
bc1d3312a8
commit
f200a954b1
163
main.py
163
main.py
@ -1,4 +1,4 @@
|
||||
|
||||
#!/usr/bin/env python3
|
||||
import config
|
||||
import subprocess
|
||||
import os
|
||||
@ -14,11 +14,13 @@ import signal
|
||||
import logging
|
||||
import math
|
||||
|
||||
logging.basicConfig(filename='pixelserver.log',level=config.LogLevel)
|
||||
logging.basicConfig(filename='pixelserver.log', level=config.LogLevel)
|
||||
|
||||
running = True
|
||||
|
||||
|
||||
########################################################################
|
||||
# Utils #
|
||||
########################################################################
|
||||
class DataSource:
|
||||
def __init__(self, initial):
|
||||
self.data = initial
|
||||
@ -33,6 +35,47 @@ class DataSource:
|
||||
with listener:
|
||||
listener.notify_all()
|
||||
|
||||
class WatchDog(threading.Thread):
|
||||
def __init__(self, check, action):
|
||||
super().__init__()
|
||||
self.check = check
|
||||
self.action = action
|
||||
self.running = True
|
||||
def run(self):
|
||||
while running and self.running:
|
||||
if self.check():
|
||||
logging.error("Watchdog: Executed")
|
||||
self.action()
|
||||
time.sleep(1)
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
class LogReader(threading.Thread):
|
||||
def __init__(self, runner):
|
||||
super().__init__()
|
||||
self.runner = runner
|
||||
self.log = ""
|
||||
self.running = True
|
||||
def clear(self):
|
||||
self.log = ""
|
||||
def getLog(self):
|
||||
return self.log
|
||||
def run(self):
|
||||
logging.info("LogReader started")
|
||||
while running and self.running:
|
||||
try:
|
||||
self.log += self.runner.app.stderr.read(1).decode("utf-8")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
logging.error(str(e))
|
||||
time.sleep(1)
|
||||
logging.info("LogReader closed")
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
########################################################################
|
||||
# GUI #
|
||||
########################################################################
|
||||
if config.UseGui:
|
||||
import pygame
|
||||
|
||||
@ -42,7 +85,6 @@ if config.UseGui:
|
||||
self.datasource = datasource
|
||||
self.cv = threading.Condition()
|
||||
self.datasource.addListener(self.cv)
|
||||
|
||||
def run(self):
|
||||
global running
|
||||
logging.info("Starting GUI")
|
||||
@ -63,10 +105,8 @@ if config.UseGui:
|
||||
r = (data[i*3+0])
|
||||
g = (data[i*3+1])
|
||||
b = (data[i*3+2])
|
||||
|
||||
pygame.draw.rect(screen, (r, g, b), pygame.Rect(sf*x, sf*y, sf, sf))
|
||||
except:
|
||||
pass
|
||||
raise
|
||||
pygame.display.flip()
|
||||
logging.info("Closing GUI")
|
||||
@ -75,6 +115,9 @@ if config.UseGui:
|
||||
self.cv.notify_all()
|
||||
super().join()
|
||||
|
||||
########################################################################
|
||||
# Serial #
|
||||
########################################################################
|
||||
class SerialWriter(threading.Thread):
|
||||
def __init__(self, datasource):
|
||||
super().__init__()
|
||||
@ -82,7 +125,6 @@ class SerialWriter(threading.Thread):
|
||||
self.datasource = datasource
|
||||
self.datasource.addListener(self.cv)
|
||||
self.updateGamma = False
|
||||
|
||||
def run(self):
|
||||
should_connect = True
|
||||
ser = None
|
||||
@ -124,10 +166,9 @@ class SerialWriter(threading.Thread):
|
||||
should_connect = True
|
||||
time.sleep(5)
|
||||
logging.info("Closing SerialWriter")
|
||||
def joint(self):
|
||||
def join(self):
|
||||
self.cv.notify_all()
|
||||
super().join()
|
||||
|
||||
def setGamma(self, r, g, b):
|
||||
with self.cv:
|
||||
self.r = r
|
||||
@ -136,46 +177,9 @@ class SerialWriter(threading.Thread):
|
||||
self.updateGamma = True
|
||||
self.cv.notify_all()
|
||||
|
||||
class WatchDog(threading.Thread):
|
||||
def __init__(self, check, action):
|
||||
super().__init__()
|
||||
self.check = check
|
||||
self.action = action
|
||||
self.running = True
|
||||
|
||||
def run(self):
|
||||
while running and self.running:
|
||||
if self.check():
|
||||
logging.error("Watchdog: Executed")
|
||||
self.action()
|
||||
time.sleep(1)
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
class LogReader(threading.Thread):
|
||||
def __init__(self, runner):
|
||||
super().__init__()
|
||||
self.runner = runner
|
||||
self.log = ""
|
||||
self.running = True
|
||||
def clear(self):
|
||||
self.log = ""
|
||||
|
||||
def getLog(self):
|
||||
return self.log
|
||||
|
||||
def run(self):
|
||||
logging.info("LogReader started")
|
||||
while running and self.running:
|
||||
try:
|
||||
self.log += self.runner.app.stderr.read(1).decode("utf-8")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
time.sleep(1)
|
||||
logging.info("LogReader closed")
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
########################################################################
|
||||
# App #
|
||||
########################################################################
|
||||
class App(threading.Thread):
|
||||
def __init__(self, cmd, param, listener, is_persistent):
|
||||
super().__init__()
|
||||
@ -194,7 +198,6 @@ class App(threading.Thread):
|
||||
self.running = running
|
||||
self.listener = listener
|
||||
self.is_persistent = is_persistent
|
||||
|
||||
def run(self):
|
||||
while running and self.running and self.alive():
|
||||
oshandle = self.app.stdout.fileno()
|
||||
@ -205,18 +208,14 @@ class App(threading.Thread):
|
||||
self.datasource.pushData(data)
|
||||
except Exception as e:
|
||||
logging.debug("Exception in App.run")
|
||||
|
||||
with self.listener:
|
||||
self.listener.notify_all()
|
||||
self.watchdog.stop()
|
||||
self.logreader.stop()
|
||||
self.watchdog.join()
|
||||
self.logreader.join()
|
||||
|
||||
|
||||
def alive(self):
|
||||
return self.app.poll() == None
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
self.app.kill()
|
||||
@ -224,21 +223,19 @@ class App(threading.Thread):
|
||||
self.app.stderr.close()
|
||||
self.watchdog.stop()
|
||||
self.logreader.stop()
|
||||
|
||||
def getLog(self):
|
||||
return self.logreader.getLog()
|
||||
|
||||
def isPersistent(self):
|
||||
return self.is_persistent
|
||||
|
||||
def terminateApp(self):
|
||||
logging.error("Terminate app!")
|
||||
self.stop()
|
||||
|
||||
|
||||
def isAppTimedOut(self):
|
||||
return time.time()-self.last_update > config.NoDataTimeout
|
||||
|
||||
########################################################################
|
||||
# Main #
|
||||
########################################################################
|
||||
class AppRunner(threading.Thread):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@ -258,28 +255,23 @@ class AppRunner(threading.Thread):
|
||||
newapp.datasource.addListener(self.cv)
|
||||
newapp.start()
|
||||
self.persistent_apps[i] = newapp
|
||||
|
||||
if config.UseGui:
|
||||
self.gui = Gui(self.datasource)
|
||||
self.gui.start()
|
||||
|
||||
def requestApp(self, app, param=""):
|
||||
with self.cv:
|
||||
self.requestedApp = app
|
||||
self.param = param
|
||||
self.cv.notify_all()
|
||||
logging.info("Requesting app: "+str(app))
|
||||
|
||||
def updateApp(self):
|
||||
if self.app != None and not self.app.isPersistent():
|
||||
self.app.stop()
|
||||
|
||||
self.currentApp = self.requestedApp
|
||||
if self.currentApp in self.persistent_apps.keys():
|
||||
self.app = self.persistent_apps[self.currentApp]
|
||||
else:
|
||||
persistent = config.Apps[self.requestedApp]["persistent"]
|
||||
|
||||
logging.info("Starting app "+config.Apps[self.requestedApp]["name"])
|
||||
cmd = config.Apps[self.requestedApp]["cmd"]
|
||||
logging.debug(str(cmd))
|
||||
@ -288,7 +280,6 @@ class AppRunner(threading.Thread):
|
||||
self.app.start()
|
||||
if persistent:
|
||||
self.persistent_apps[self.currentApp] = self.app
|
||||
|
||||
def run(self):
|
||||
logging.info("Starting Apprunner")
|
||||
while running:
|
||||
@ -296,10 +287,8 @@ class AppRunner(threading.Thread):
|
||||
if self.app == None or not self.app.alive():
|
||||
self.requestedApp = 0
|
||||
if self.requestedApp != None:
|
||||
|
||||
self.updateApp()
|
||||
self.requestedApp = None
|
||||
|
||||
self.cv.wait()
|
||||
if self.app != None:
|
||||
data = self.app.datasource.getData()
|
||||
@ -308,7 +297,6 @@ class AppRunner(threading.Thread):
|
||||
if config.UseGui:
|
||||
self.gui.join()
|
||||
logging.info("Close Apprunner")
|
||||
|
||||
def getLog(self):
|
||||
if self.app == None:
|
||||
return ""
|
||||
@ -316,9 +304,9 @@ class AppRunner(threading.Thread):
|
||||
def setGamma(self, r, g, b):
|
||||
self.serial.setGamma(r, g, b)
|
||||
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
# Startup #
|
||||
########################################################################
|
||||
#normalize config
|
||||
for app in config.Apps:
|
||||
if "persistent" not in app.keys():
|
||||
@ -326,7 +314,7 @@ for app in config.Apps:
|
||||
if "guiname" not in app.keys():
|
||||
app["guiname"] = app["name"]
|
||||
|
||||
#remove unavalible apps
|
||||
#remove non existing apps
|
||||
for app in config.Apps:
|
||||
cmd = app["cmd"]
|
||||
if type(cmd) == str and not os.path.isfile(cmd):
|
||||
@ -336,6 +324,9 @@ for app in config.Apps:
|
||||
runner = AppRunner()
|
||||
runner.start()
|
||||
|
||||
########################################################################
|
||||
# Web Api #
|
||||
########################################################################
|
||||
@bottle.route('/<:re:.*>', method='OPTIONS')
|
||||
def enable_cors_generic_route():
|
||||
add_cors_headers()
|
||||
@ -351,6 +342,12 @@ def add_cors_headers():
|
||||
bottle.response.headers['Access-Control-Allow-Headers'] = \
|
||||
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
|
||||
|
||||
def getAppIdx(name):
|
||||
for i in range(len(config.Apps)):
|
||||
if config.Apps[i]["name"] == name:
|
||||
return i
|
||||
return -1
|
||||
|
||||
@route("/apps/list")
|
||||
def apps_list():
|
||||
s = []
|
||||
@ -364,29 +361,30 @@ def apps_list():
|
||||
|
||||
@route("/apps/start/<name>")
|
||||
def apps_start_param(name):
|
||||
for i in range(len(config.Apps)):
|
||||
if config.Apps[i]["name"] == name:
|
||||
i = getAppIdx(name)
|
||||
if i >= 0:
|
||||
runner.requestApp(i)
|
||||
return "ok"
|
||||
else:
|
||||
return "not_found"
|
||||
|
||||
@post("/apps/start/<name>")
|
||||
def apps_start_post(name):
|
||||
param = request.forms.get('param')
|
||||
for i in range(len(config.Apps)):
|
||||
if config.Apps[i]["name"] == name:
|
||||
i = getAppIdx(name)
|
||||
if i >= 0:
|
||||
runner.requestApp(i, param)
|
||||
return "ok"
|
||||
else:
|
||||
return "not_found"
|
||||
|
||||
|
||||
@route("/apps/start/<name>/<param>")
|
||||
def apps_start(name, param):
|
||||
for i in range(len(config.Apps)):
|
||||
|
||||
if config.Apps[i]["name"] == name:
|
||||
i = getAppIdx(name)
|
||||
if i >= 0:
|
||||
runner.requestApp(i, param)
|
||||
return "ok"
|
||||
else:
|
||||
return "not_found"
|
||||
|
||||
@route("/apps/log")
|
||||
@ -401,7 +399,7 @@ def apps_running():
|
||||
@route("/")
|
||||
def index():
|
||||
return bottle.static_file("index.html", root='html')
|
||||
#return open("html/index.html").read()
|
||||
|
||||
@route("/setgamma/<r>/<g>/<b>")
|
||||
def setGamma(r, g, b):
|
||||
r = float(r)
|
||||
@ -410,6 +408,9 @@ def setGamma(r, g, b):
|
||||
runner.setGamma(r, g, b)
|
||||
return "ok"
|
||||
|
||||
########################################################################
|
||||
# Shutdown #
|
||||
########################################################################
|
||||
run(host=config.WebHost, port=config.WebPort)
|
||||
running = False
|
||||
runner.join()
|
||||
|
Loading…
Reference in New Issue
Block a user