pixelserver2/main.py

377 lines
13 KiB
Python
Raw Normal View History

2018-12-30 19:45:11 +00:00
#!/usr/bin/env python3
2018-08-19 18:47:08 +00:00
import config
import subprocess
import os
import os.path
2018-08-19 18:47:08 +00:00
import serial
import threading
import json
2018-08-26 20:07:06 +00:00
import bottle
from bottle import route, run, request, post
2018-08-22 16:04:04 +00:00
import time
2018-08-22 17:43:16 +00:00
import sys
2018-08-22 18:48:26 +00:00
import logging
2018-10-21 13:03:56 +00:00
import math
2018-08-22 18:48:26 +00:00
2018-12-30 19:45:11 +00:00
logging.basicConfig(filename='pixelserver.log', level=config.LogLevel)
2018-08-22 17:43:16 +00:00
running = True
2018-12-30 19:45:11 +00:00
########################################################################
# Utils #
########################################################################
2018-08-25 17:01:09 +00:00
class DataSource:
def __init__(self, initial):
self.data = initial
self.listeners = []
def getData(self):
return self.data
def addListener(self, listener):
self.listeners.append(listener)
2018-12-30 20:23:05 +00:00
return self
2018-08-25 17:01:09 +00:00
def pushData(self, data):
self.data = data
for listener in self.listeners:
with listener:
listener.notify_all()
2018-12-30 19:45:11 +00:00
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 #
########################################################################
2018-08-22 17:23:36 +00:00
if config.UseGui:
import pygame
class Gui(threading.Thread):
2018-08-25 17:01:09 +00:00
def __init__(self, datasource):
2018-08-22 17:23:36 +00:00
super().__init__()
2018-08-25 17:01:09 +00:00
self.cv = threading.Condition()
2018-12-30 20:23:05 +00:00
self.datasource = datasource.addListener(self.cv)
2018-08-22 17:23:36 +00:00
def run(self):
2018-08-22 18:48:26 +00:00
logging.info("Starting GUI")
2018-08-22 17:23:36 +00:00
sf = config.GuiScaleFactor
screen = pygame.display.set_mode((sf*config.ScreenX, sf*config.ScreenY))
pygame.display.set_caption("Pixelserver - GUI Vis")
2018-08-22 17:43:16 +00:00
while running:
2018-08-22 17:23:36 +00:00
for event in pygame.event.get():
2018-08-25 17:01:09 +00:00
pass
with self.cv:
self.cv.wait()
data = self.datasource.getData()
2018-08-22 17:23:36 +00:00
screen.fill((0, 255, 0))
for x in range(config.ScreenX):
for y in range(config.ScreenY):
i = x+y*config.ScreenX
color = (data[i*3+0], data[i*3+1], data[i*3+2])
pygame.draw.rect(screen, color, pygame.Rect(sf*x, sf*y, sf, sf))
2018-08-22 17:23:36 +00:00
pygame.display.flip()
2018-08-22 18:48:26 +00:00
logging.info("Closing GUI")
2018-08-25 17:01:09 +00:00
def join(self):
with self.cv:
self.cv.notify_all()
super().join()
2018-08-22 16:04:04 +00:00
2018-12-30 19:45:11 +00:00
########################################################################
# Serial #
########################################################################
2018-08-22 16:04:04 +00:00
class SerialWriter(threading.Thread):
2018-08-25 17:01:09 +00:00
def __init__(self, datasource):
2018-08-22 16:04:04 +00:00
super().__init__()
2018-08-25 17:01:09 +00:00
self.cv = threading.Condition()
2018-12-30 20:23:05 +00:00
self.datasource = datasource.addListener(self.cv)
2018-10-21 13:03:56 +00:00
self.updateGamma = False
2018-08-22 16:04:04 +00:00
def run(self):
should_connect = True
ser = None
2018-08-22 18:48:26 +00:00
logging.info("Starting SerialWriter")
2018-08-22 17:43:16 +00:00
while running:
try:
if should_connect:
ser = serial.Serial(config.Serial)
should_connect = False
2018-08-22 18:48:26 +00:00
logging.info("Serial Opened")
2018-08-25 17:01:09 +00:00
with self.cv:
self.cv.wait(timeout = 1/30)
data = self.datasource.getData()
2018-10-21 13:03:56 +00:00
if self.updateGamma:
2018-10-21 13:55:12 +00:00
buf = bytearray(b"\x00")*3*256
2018-10-21 13:03:56 +00:00
for i in range(256):
2018-12-30 20:23:05 +00:00
apply = lambda x, g: max(0, min(255, int(math.pow(x/255, g)*255)))
buf[i] = apply(i, self.r)
buf[i+256] = apply(i, self.g)
buf[i+512] = apply(i, self.b)
2018-10-21 13:03:56 +00:00
ser.write(b"\x02")
ser.write(buf)
self.updateGamma = False
2018-08-25 17:01:09 +00:00
ser.write(b"\01")
ser.write(data)
2018-10-21 13:19:51 +00:00
ser.flush()
2018-08-22 18:48:26 +00:00
except Exception as e:
if ser != None:
ser.close()
ser = None
2018-08-22 18:48:26 +00:00
logging.warning("Serial was close because: "+str(e))
should_connect = True
2018-08-25 17:01:09 +00:00
time.sleep(5)
2018-08-22 18:48:26 +00:00
logging.info("Closing SerialWriter")
2018-12-30 19:45:11 +00:00
def join(self):
2018-12-30 19:48:46 +00:00
with self.cv:
self.cv.notify_all()
2018-08-25 17:01:09 +00:00
super().join()
2018-10-21 13:03:56 +00:00
def setGamma(self, r, g, b):
with self.cv:
2018-12-30 20:23:05 +00:00
self.r, self.g, self.b = r, g, b
2018-10-21 13:03:56 +00:00
self.updateGamma = True
self.cv.notify_all()
2018-12-30 19:45:11 +00:00
########################################################################
# App #
########################################################################
2018-08-25 17:01:09 +00:00
class App(threading.Thread):
2018-08-26 18:31:48 +00:00
def __init__(self, cmd, param, listener, is_persistent):
2018-08-25 17:01:09 +00:00
super().__init__()
#start app
if type(cmd) != list:
cmd = [cmd,]
args = cmd+[str(config.ScreenX), str(config.ScreenY), param]
self.app = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
self.last_update = time.time()
self.cv = threading.Condition()
self.watchdog = WatchDog(lambda: self.isAppTimedOut(), lambda: self.terminateApp())
self.watchdog.start()
self.logreader = LogReader(self)
self.logreader.start()
self.datasource = DataSource(b"\x00"*config.ScreenX*config.ScreenY*3)
2018-12-30 20:23:05 +00:00
self.running = True
2018-08-25 17:01:09 +00:00
self.listener = listener
2018-08-26 18:31:48 +00:00
self.is_persistent = is_persistent
2018-08-25 17:01:09 +00:00
def run(self):
while running and self.running and self.alive():
oshandle = self.app.stdout.fileno()
try:
data = os.read(oshandle, config.ScreenX*config.ScreenY*3)
assert len(data) == config.ScreenX*config.ScreenY*3
self.last_update = time.time()
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()
self.app.stdout.close()
self.app.stderr.close()
self.watchdog.stop()
self.logreader.stop()
def getLog(self):
return self.logreader.getLog()
def terminateApp(self):
logging.error("Terminate app!")
self.stop()
def isAppTimedOut(self):
return time.time()-self.last_update > config.NoDataTimeout
2018-12-30 19:45:11 +00:00
########################################################################
# Main #
########################################################################
2018-08-19 18:47:08 +00:00
class AppRunner(threading.Thread):
def __init__(self):
super().__init__()
self.currentApp = -1
self.requestedApp = 0
self.app = None
2018-08-25 17:01:09 +00:00
self.cv = threading.Condition()
2018-08-19 18:47:08 +00:00
self.param = ""
2018-08-25 17:01:09 +00:00
self.datasource = DataSource(b"\x00"*config.ScreenX*config.ScreenY*3)
self.serial = SerialWriter(self.datasource)
2018-08-22 16:04:04 +00:00
self.serial.start()
2018-08-26 18:31:48 +00:00
self.persistent_apps = {}
2018-12-30 14:55:59 +00:00
#start persistent apps
for app, i in zip(config.Apps, range(len(config.Apps))):
if app["persistent"]:
2018-12-30 20:23:05 +00:00
self.startApp(i)
2018-08-22 17:23:36 +00:00
if config.UseGui:
2018-08-25 17:01:09 +00:00
self.gui = Gui(self.datasource)
2018-08-22 17:23:36 +00:00
self.gui.start()
2018-08-19 18:47:08 +00:00
def requestApp(self, app, param=""):
2018-08-25 17:01:09 +00:00
with self.cv:
2018-08-19 18:47:08 +00:00
self.requestedApp = app
self.param = param
2018-08-25 17:01:09 +00:00
self.cv.notify_all()
logging.info("Requesting app: "+str(app))
2018-12-30 20:23:05 +00:00
def startApp(self, i, param=""):
app = config.Apps[i]
newapp = App(app["cmd"], param, self.cv, is_persistent=app["persistent"])
newapp.datasource.addListener(self.cv)
newapp.start()
if app["persistent"]:
self.persistent_apps[self.currentApp] = newapp
return newapp
2018-08-19 18:47:08 +00:00
def updateApp(self):
2018-12-30 20:23:05 +00:00
if self.app != None and not self.app.is_persistent:
2018-08-25 17:01:09 +00:00
self.app.stop()
2018-08-26 18:31:48 +00:00
self.currentApp = self.requestedApp
if (self.currentApp in self.persistent_apps.keys()
and self.persistent_apps[self.currentApp].alive()):
2018-08-26 18:31:48 +00:00
self.app = self.persistent_apps[self.currentApp]
else:
2018-12-30 20:23:05 +00:00
self.app = self.startApp(self.requestedApp, self.param)
2018-08-19 18:47:08 +00:00
def run(self):
2018-08-22 18:48:26 +00:00
logging.info("Starting Apprunner")
2018-08-22 17:43:16 +00:00
while running:
2018-08-25 17:01:09 +00:00
with self.cv:
if self.app == None or not self.app.alive():
2018-08-19 18:47:08 +00:00
self.requestedApp = 0
2018-08-22 16:21:59 +00:00
if self.requestedApp != None:
2018-08-19 18:47:08 +00:00
self.updateApp()
2018-08-22 16:21:59 +00:00
self.requestedApp = None
2018-08-25 17:01:09 +00:00
data = self.app.datasource.getData()
self.datasource.pushData(data)
2018-12-30 20:23:05 +00:00
self.cv.wait()
2018-08-22 17:43:16 +00:00
self.serial.join()
if config.UseGui:
self.gui.join()
2018-08-22 18:48:26 +00:00
logging.info("Close Apprunner")
def getLog(self):
2018-08-25 17:01:09 +00:00
if self.app == None:
return ""
return self.app.getLog()
2018-10-21 13:03:56 +00:00
def setGamma(self, r, g, b):
self.serial.setGamma(r, g, b)
2018-08-25 17:01:09 +00:00
2018-12-30 19:45:11 +00:00
########################################################################
# Web Api #
########################################################################
2018-10-21 14:09:48 +00:00
@bottle.route('/<:re:.*>', method='OPTIONS')
def enable_cors_generic_route():
add_cors_headers()
@bottle.hook('after_request')
def enable_cors_after_request_hook():
add_cors_headers()
def add_cors_headers():
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
bottle.response.headers['Access-Control-Allow-Methods'] = \
'GET, POST, PUT, OPTIONS'
bottle.response.headers['Access-Control-Allow-Headers'] = \
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
2018-12-30 20:23:05 +00:00
def startApp(name, param=""):
2018-12-30 19:45:11 +00:00
for i in range(len(config.Apps)):
if config.Apps[i]["name"] == name:
2018-12-30 20:23:05 +00:00
runner.requestApp(i, param)
return "ok"
return "not_found"
2018-12-30 19:45:11 +00:00
2018-08-19 18:47:08 +00:00
@route("/apps/list")
def apps_list():
s = []
for app in config.Apps:
2018-12-30 20:23:05 +00:00
s.append({"name": app["name"],
"guiname": app["guiname"],
"persistent": app["persistent"]})
2018-08-19 18:47:08 +00:00
return json.dumps(s)
@route("/apps/start/<name>")
2018-08-26 20:07:06 +00:00
def apps_start_param(name):
2018-12-30 20:23:05 +00:00
return startApp(name)
2018-08-19 18:47:08 +00:00
2018-08-26 20:07:06 +00:00
@post("/apps/start/<name>")
def apps_start_post(name):
param = request.forms.get('param')
2018-12-30 20:23:05 +00:00
return startApp(name, param)
2018-08-26 20:07:06 +00:00
2018-08-19 18:47:08 +00:00
@route("/apps/start/<name>/<param>")
def apps_start(name, param):
2018-12-30 20:23:05 +00:00
return startApp(name, param)
2018-08-19 18:47:08 +00:00
@route("/apps/log")
def apps_log():
return runner.getLog()
2018-08-19 18:47:08 +00:00
@route("/apps/running")
def apps_running():
2018-12-30 20:23:05 +00:00
return config.Apps[runner.currentApp]["name"]
2018-08-19 18:47:08 +00:00
2018-08-22 19:15:32 +00:00
@route("/")
def index():
2018-08-26 20:07:06 +00:00
return bottle.static_file("index.html", root='html')
2018-12-30 19:45:11 +00:00
2018-10-21 13:03:56 +00:00
@route("/setgamma/<r>/<g>/<b>")
def setGamma(r, g, b):
r = float(r)
g = float(g)
b = float(b)
runner.setGamma(r, g, b)
2018-10-21 14:09:48 +00:00
return "ok"
2018-08-22 17:43:16 +00:00
2018-12-30 19:45:11 +00:00
########################################################################
2018-12-30 19:48:46 +00:00
# Startup #
2018-12-30 19:45:11 +00:00
########################################################################
2018-12-30 19:48:46 +00:00
#normalize config
for app in config.Apps:
if "persistent" not in app.keys():
app["persistent"] = False
if "guiname" not in app.keys():
app["guiname"] = app["name"]
cmd = app["cmd"]
2018-12-30 20:23:05 +00:00
#remove non existing apps
2018-12-30 19:48:46 +00:00
if type(cmd) == str and not os.path.isfile(cmd):
config.Apps.remove(app)
logging.warning("Removed app "+app["name"])
runner = AppRunner()
runner.start()
2018-08-26 20:07:06 +00:00
run(host=config.WebHost, port=config.WebPort)
2018-12-30 19:48:46 +00:00
########################################################################
# Shutdown #
########################################################################
2018-08-22 18:48:26 +00:00
running = False
2018-08-22 19:15:32 +00:00
runner.join()