Initial commit
This commit is contained in:
81
main.py
Normal file
81
main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import config
|
||||
import subprocess
|
||||
import os
|
||||
import serial
|
||||
import threading
|
||||
import json
|
||||
from bottle import route, run
|
||||
|
||||
|
||||
|
||||
class AppRunner(threading.Thread):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.currentApp = -1
|
||||
self.requestedApp = 0
|
||||
self.app = None
|
||||
self.lock = threading.Lock()
|
||||
self.param = ""
|
||||
|
||||
def requestApp(self, app, param=""):
|
||||
with self.lock:
|
||||
self.requestedApp = app
|
||||
self.param = param
|
||||
|
||||
def updateApp(self):
|
||||
if self.app != None:
|
||||
self.app.terminate()
|
||||
args = [os.getcwd()+"/"+config.Apps[self.requestedApp]["cmd"], str(config.ScreenX), str(config.ScreenY), self.param]
|
||||
self.app = subprocess.Popen(args, stdout=subprocess.PIPE)
|
||||
|
||||
def run(self):
|
||||
ser = serial.Serial("/dev/ttyACM0")
|
||||
while True:
|
||||
with self.lock:
|
||||
if self.app == None or self.app.poll() != None:
|
||||
self.requestedApp = 0
|
||||
if self.currentApp != self.requestedApp:
|
||||
self.currentApp = self.requestedApp
|
||||
self.updateApp()
|
||||
d = self.app.stdout
|
||||
oshandle = d.fileno()
|
||||
data = os.read(oshandle, config.ScreenX*config.ScreenY*3)
|
||||
# data = d.raw.read(config.ScreenX*config.ScreenY*3)
|
||||
ser.write(b"\01")
|
||||
ser.write(data)
|
||||
|
||||
|
||||
runner = AppRunner()
|
||||
runner.start()
|
||||
|
||||
@route("/apps/list")
|
||||
def apps_list():
|
||||
s = []
|
||||
for app in config.Apps:
|
||||
s.append(app["name"])
|
||||
return json.dumps(s)
|
||||
|
||||
@route("/apps/start/<name>")
|
||||
def apps_start(name):
|
||||
for i in range(len(config.Apps)):
|
||||
if config.Apps[i]["name"] == name:
|
||||
runner.requestApp(i)
|
||||
return "ok"
|
||||
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:
|
||||
runner.requestApp(i, param)
|
||||
return "ok"
|
||||
return "not_found"
|
||||
|
||||
|
||||
@route("/apps/running")
|
||||
def apps_running():
|
||||
i = runner.currentApp
|
||||
return config.Apps[i]["name"]
|
||||
|
||||
run(host="localhost", port=8000)
|
Reference in New Issue
Block a user