-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.py
73 lines (51 loc) · 1.24 KB
/
framework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import time
from flask import Flask, render_template, redirect, url_for
from threading import Thread
import signal
from matrix.Matrix import Matrix
from apps import apps
PIX_SIZE = 20
WIDTH = 28
HEIGHT = 22
matrix = Matrix(WIDTH, HEIGHT, PIX_SIZE)
current_app = None
def switch_app(app):
global current_app
if app and isinstance(current_app, app):
return
if current_app:
current_app.exit()
del current_app
if app:
current_app = app(matrix)
else:
current_app = None
web = Flask(__name__)
@web.route('/')
def index():
return render_template('index.html', apps=apps)
@web.route('/off')
def off():
switch_app(None)
return redirect(url_for('index'))
@web.route('/switch/<int:app_id>')
def switch_route(app_id):
switch_app(apps[app_id])
return redirect(url_for('index'))
web_thread = Thread(target=lambda: web.run(host='0.0.0.0', port=80))
web_thread.daemon = True
web_thread.start()
def terminate():
if current_app:
current_app.exit()
global matrix
del matrix
sys.exit(0)
signal.signal(signal.SIGTERM, terminate)
while True:
if current_app:
current_app.update(0)
else:
matrix.fill((1, 1, 1))
matrix.run()