-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.py
More file actions
88 lines (70 loc) · 2.09 KB
/
server.py
File metadata and controls
88 lines (70 loc) · 2.09 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import multiprocessing
import os
from flask import Flask, request, redirect, render_template
import run_pattern
import tile
import pkgutil
import signal
app = Flask("LEDServer")
rows = 5
cols = 6
width = 10
height = 10
shuffle = False
pattern = "patrickstar"
extra_args = ""
process = None
board = None
leds = None
def run():
global board, leds
signal.signal(signal.SIGTERM, exit_gracefully)
board = tile.TileArray(rows=rows, cols=cols, height=height, width=width)
leds = tile.LEDStripTeensyUART(board)
if shuffle:
run_pattern.shuffle(board, leds)
else:
run_pattern.run_pattern(board, leds, pattern, extra_args.split())
def exit_gracefully():
raise KeyboardInterrupt()
def start_proc():
global process
if process is not None:
process.terminate()
process.join()
process = multiprocessing.Process(target=run)
process.start()
@app.route('/')
def hello_world():
patterns = [name for _, name, _ in pkgutil.iter_modules(['patterns'])]
return render_template("index.html", rows=rows, cols=cols, width=width, height=height, shuffle=shuffle,
pattern=pattern, patterns=patterns, extra=extra_args)
@app.route('/save-settings', methods=['POST'])
def handle_data():
global rows, cols, width, height
rows = int(request.form['rows'])
cols = int(request.form['cols'])
width = int(request.form['width'])
height = int(request.form['height'])
return redirect("/")
@app.route('/save-pattern', methods=['POST'])
def save_pattern():
global shuffle, pattern, extra_args
shuffle = False
pattern = request.form['pattern']
extra_args = request.form['extra']
start_proc()
return redirect("/")
@app.route('/save-shuffle', methods=['POST'])
def save_shuffle():
global shuffle, pattern, extra_args
shuffle = True
pattern = ""
extra_args = ""
start_proc()
return redirect("/")
@app.route('/update')
def update():
return os.system("git stash && git checkout master && git pull && systemctl restart leds.service")
if __name__ == "__main__":
app.run(port=80, host="0")