-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
499 lines (412 loc) · 14.9 KB
/
Copy pathapp.py
File metadata and controls
499 lines (412 loc) · 14.9 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from flask import Flask, render_template, jsonify, request
from flask_basicauth import BasicAuth
from tinydb import TinyDB, Query
from decouple import config
import serial, json, threading
import stock
# USB device, a Teensy 3.6 to send serial comms to.
TEENSY = '/dev/serial/by-id/usb-Teensyduino_USB_Serial_5220260-if00'
BAUD = 9600
# Instantiate our web server and database.
app = Flask(__name__)
app.config['BASIC_AUTH_USERNAME'] = config('AUTH_USER')
app.config['BASIC_AUTH_PASSWORD'] = config('AUTH_PASS')
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
db = TinyDB('db.json')
# One serial handle for the whole process, opened on first use and reused.
#
# This used to open a fresh serial.Serial() on every request and never close it,
# which leaked a file descriptor per request. That was survivable when only a
# human clicking buttons ever talked to the wall, but the stock poller sends a
# frame every minute unattended, so the leak would become hundreds of
# descriptors a day. The lock matters for the same reason: a poll and a user
# request landing together would otherwise interleave their bytes mid-frame and
# the Teensy would parse the result as garbage.
_serial = None
_serial_lock = threading.Lock()
def _port():
global _serial
if _serial is None or not _serial.is_open:
_serial = serial.Serial(TEENSY, BAUD, timeout=2)
return _serial
def send(request_string):
"""
Put one command on the wire and return the Teensy's acknowledgement.
Reconnects once if the port has gone away -- unplugging and replugging the
Teensy should not require restarting the server.
"""
global _serial
with _serial_lock:
for attempt in (1, 2):
try:
port = _port()
port.write(request_string.encode('utf-8'))
return port.readline().strip().strip(b'<>')
except (serial.SerialException, OSError):
if _serial is not None:
try:
_serial.close()
except Exception:
pass
_serial = None
if attempt == 2:
raise
# Get state of Teensy, to set initial values to web app.
def get_state():
try:
return send("<state>").split(b',')
except (serial.SerialException, OSError) as error:
print("Could not read state: %s" % error)
return [b'']
def request_and_respond(request_string):
print ("Sending: " + request_string)
try:
mode = send(request_string)
except (serial.SerialException, OSError) as error:
return jsonify({'error': str(error)}), 503
# Remember which mode the wall is on, so the stock poller knows whether it
# is allowed to draw. Without this it would happily overwrite fire or life.
_note_active_mode(request_string)
return jsonify({'response': mode})
# --- Active mode tracking -------------------------------------------------
# The command name most recently sent. The poller only pushes when this is a
# stock command, so selecting another mode in the UI silently parks it.
_active_command = None
def _note_active_mode(request_string):
global _active_command
_active_command = request_string.lstrip('<').split(',')[0].rstrip('>')
def stock_is_active():
return _active_command == 'stock'
def load_template_with_swatches(template, swatch_type, initial_state):
# Supply swatches to front end.
swatch_query = Query()
swatches = db.search(swatch_query.type == swatch_type)
return render_template(template, swatches=swatches, initialState=initial_state)
# Route for homepage.
@app.route('/')
def index():
return render_template('index.html')
# Route for Conway's Game of Life.
@app.route('/life')
def life():
initial_state = {
'type': 'life',
'h': 0,
's': 0,
'l': 0
}
state = get_state()
if (b'life' == state[0]):
initial_state = {
'type': 'life',
'h': state[1],
's': state[2],
'l': state[3]
}
return load_template_with_swatches('life.html', 'hsl', initial_state)
# Endpoint for posting life color swatches (to set color for conway's game of life).
@app.route('/_post_life_color/', methods=['POST'])
def _post_life_color():
data = request.get_json()
h = data['h']
s = data['s']
l = data['l']
return request_and_respond("<life,"+str(h)+","+str(s)+","+str(l)+">");
# Route to pause/play Conway's Game of Life.
@app.route('/_pause_life/', methods=['POST'])
def _life():
data = request.get_json()
return request_and_respond("<lifepause," + str(data['pause']) + ">")
# Route for fire.
@app.route('/fire')
def fire():
initial_state = {
'type': 'hsl',
'h':0,
's':10,
'l':10
}
state = get_state()
if (b'fire' == state[0]):
initial_state = {
'type': 'hsl',
'h': state[1],
's': 100,
'l': 50
}
return load_template_with_swatches('fire.html', 'hsl', initial_state)
# Route to pause/play (with) fire.
@app.route('/_pause_fire/', methods=['POST'])
def _fire():
data = request.get_json()
return request_and_respond("<firepause," + str(data['pause']) + ">")
# Route to send custom fire color.
@app.route('/_post_fire_color/', methods=['POST'])
def _post_fire_color():
data = request.get_json()
h = data['h']
return request_and_respond("<fire,"+str(h)+">")
# Endpoint for posting special .
@app.route('/_post_fire_special/', methods=['POST'])
def _post_fire_special():
data = request.get_json()
special = data['special']
return request_and_respond("<specialfire,"+str(special)+">")
# Route for matrix.
@app.route('/matrix')
def matrix():
return render_template('matrix.html')
# Route to play/pause matrix.
@app.route('/_pause_matrix/', methods=['POST'])
def _pause_matrix():
data = request.get_json()
return request_and_respond("<pausematrix," + str(data['pause']) + ">")
# Endpoint for posting prebuilt matrix color mode.
@app.route('/_post_matrix/', methods=['POST'])
def _post_matrix():
data = request.get_json()
request_string = ("<matrix," +
str(data[0][0])+"," +
str(data[0][1])+"," +
str(data[1][0])+"," +
str(data[1][1])+"," +
str(data[2][0])+"," +
str(data[2][1])+"," +
str(data[3][0])+"," +
str(data[3][1])+">")
print ("Sending: " + request_string)
return request_and_respond(request_string)
# Route for Gradient picker.
@app.route('/gradient-color')
def gradient_color():
initial_state = {
'type': 'rgbw',
'r': 0,
'g': 0,
'b': 0,
'w': 10
}
state = get_state()
if (b'rgbw' == state[0]):
initial_state = {
'type': 'rgbw',
'r': state[1],
'g': state[2],
'b': state[3],
'w': state[4]
}
return load_template_with_swatches('gradient-color.html', 'rgbw', initial_state)
@app.route('/_post_gradient/', methods=['POST'])
def _post_grade():
data = request.get_json()
r = data['r']
g = data['g']
b = data['b']
w = data['w']
return request_and_respond("<grade,"+str(r)+","+str(g)+","+str(b)+","+str(w)+">")
# Route for HSL color picker.
@app.route('/hsl-color')
def hsl_color():
initial_state = {
'type': 'hsl',
'h':0,
's':10,
'l':10
}
state = get_state()
if (b'hsl' == state[0]):
initial_state = {
'type': 'hsl',
'h': state[1],
's': state[2],
'l': state[3]
}
return load_template_with_swatches('hsl-color.html', 'hsl', initial_state)
# Endpoint for posting hsl color swatches (to update light wall).
@app.route('/_post_hsl_color/', methods=['POST'])
def _post_hsl_color():
data = request.get_json()
h = data['h']
s = data['s']
l = data['l']
return request_and_respond("<hsl,"+str(h)+","+str(s)+","+str(l)+">")
# Endpoint for posting hsl color swatches (to update light wall).
@app.route('/_post_hsl_special/', methods=['POST'])
def _post_hsl_special():
data = request.get_json()
special = data['special']
return request_and_respond("<specialhsl,"+str(special)+">")
# Endpoint for saving or deleting HSL color swatches to data storage.
@app.route('/_hsl_swatch_data/', methods=['PUT', 'DELETE'])
def _hsl_swatch_data():
data = request.get_json()
status = 'failed';
print (data)
if ('DELETE' == request.method):
swatch_query = Query()
record = db.remove(
(swatch_query.type == data['type']) &
(swatch_query.h == data['h']) &
(swatch_query.s == data['s']) &
(swatch_query.l == data['l'])
)
if (record):
status = 'record deleted'
if ('PUT' == request.method):
record = db.insert({'type': data['type'], 'h': data['h'], 's': data['s'], 'l': data['l']})
if (record):
status = 'record added'
return jsonify({'response': status})
# Route for RGBW color picker.
@app.route('/rgbw-color')
def rgbw_color():
initial_state = {
'type': 'rgbw',
'r': 0,
'g': 0,
'b': 0,
'w': 10
}
state = get_state()
if (b'rgbw' == state[0]):
initial_state = {
'type': 'rgbw',
'r': state[1],
'g': state[2],
'b': state[3],
'w': state[4]
}
return load_template_with_swatches('rgbw-color.html', 'rgbw', initial_state)
# Endpoint for posting rgbw color swatches (to update light wall).
@app.route('/_post_rgbw_color/', methods=['POST'])
def _post_rgbw_color():
data = request.get_json()
r = data['r']
g = data['g']
b = data['b']
w = data['w']
s = data['s']
return request_and_respond("<rgbw,"+str(r)+","+str(g)+","+str(b)+","+str(w)+","+str(s)+">")
# Endpoint for saving or deleting RGBW color swatches to data storage.
@app.route('/_rgbw_swatch_data/', methods=['PUT', 'DELETE'])
def _rgbw_swatch_data():
data = request.get_json()
status = 'failed';
print (data)
if ('DELETE' == request.method):
swatch_query = Query()
record = db.remove(
(swatch_query.type == data['type']) &
(swatch_query.r == data['r']) &
(swatch_query.g == data['g']) &
(swatch_query.b == data['b']) &
(swatch_query.w == data['w'])
)
if (record):
status = 'record deleted'
if ('PUT' == request.method):
record = db.insert({'type': data['type'], 'r': data['r'], 'g': data['g'], 'b': data['b'], 'w': data['w']})
if (record):
status = 'record added'
return jsonify({'response': status})
# --- Stock chart ----------------------------------------------------------
# The chosen symbol lives in TinyDB alongside the color swatches, so it survives
# a restart. Only ever one record of this type.
def get_stock_symbol():
record = db.get(Query().type == 'stock')
return record['symbol'] if record else ''
def set_stock_symbol(symbol):
_update_stock_record({'symbol': symbol})
def get_stock_brightness():
record = db.get(Query().type == 'stock')
if record and 'brightness' in record:
return stock.normalize_brightness(record['brightness'])
return stock.DEFAULT_BRIGHTNESS
def set_stock_brightness(value):
_update_stock_record({'brightness': stock.normalize_brightness(value)})
def _update_stock_record(fields):
query = Query()
if db.get(query.type == 'stock'):
db.update(fields, query.type == 'stock')
else:
record = {'type': 'stock', 'symbol': '', 'brightness': stock.DEFAULT_BRIGHTNESS}
record.update(fields)
db.insert(record)
# Refreshes the wall while it is on stock mode. Collaborators are passed in so
# stock.py never has to import this module back.
poller = stock.Poller(
send=send,
get_symbol=get_stock_symbol,
is_active=stock_is_active,
get_brightness=get_stock_brightness,
)
# Route for the stock chart.
@app.route('/stock')
def stock_page():
# No get_state() call here: the symbol comes from the database, and there is
# nothing else about this mode the Teensy knows better than we do. Skipping it
# keeps the page load off the serial port entirely.
initial_state = {
'type': 'stock',
'symbol': get_stock_symbol(),
# Reported rather than hardcoded in the template, so the legend and the
# slider stay correct if these ever change.
'days': stock.WINDOW,
'brightness': get_stock_brightness(),
'minBrightness': stock.MIN_BRIGHTNESS,
'maxBrightness': stock.MAX_BRIGHTNESS,
}
return render_template('stock.html', initialState=initial_state)
# Endpoint for choosing which symbol to display.
@app.route('/_post_stock/', methods=['POST'])
def _post_stock():
data = request.get_json()
symbol = stock.normalize_symbol(data.get('symbol', ''))
if not symbol:
return jsonify({'error': 'Enter a ticker symbol.'}), 400
# Fetch before persisting, so a typo is reported rather than saved. force
# sends the frame even though the wall is not on stock mode yet, which is
# what actually switches it over.
try:
result = poller.push(symbol, force=True)
except stock.StockError as error:
return jsonify({'error': str(error)}), 400
except (serial.SerialException, OSError) as error:
return jsonify({'error': str(error)}), 503
set_stock_symbol(symbol)
_note_active_mode('<stock')
return jsonify({
'response': 'ok',
'symbol': result['symbol'],
'price': result['price'],
'currency': result.get('currency', 'USD'),
'closes': result['closes'],
})
# Endpoint for the overall brightness of the stock chart.
@app.route('/_post_stock_brightness/', methods=['POST'])
def _post_stock_brightness():
data = request.get_json()
brightness = stock.normalize_brightness(data.get('brightness'))
set_stock_brightness(brightness)
# Redraw from cache rather than refetching: only a display setting changed,
# so going back to the provider would burn a request for identical prices.
try:
poller.repaint()
except stock.StockError:
# No data cached yet, so there is nothing to redraw. The value is saved
# and will apply to the first frame.
pass
except (serial.SerialException, OSError) as error:
return jsonify({'error': str(error)}), 503
return jsonify({'response': 'ok', 'brightness': brightness})
# Endpoint for the web UI's preview, which renders the same layout as the wall.
@app.route('/_stock_data/')
def _stock_data():
snapshot = poller.snapshot()
snapshot['symbol'] = get_stock_symbol()
return jsonify(snapshot)
# Run locally, accessible via any device on network.
if __name__ == '__main__':
poller.start()
app.run(debug=False, host='0.0.0.0', port=config('PORT'))