-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
156 lines (105 loc) · 4.23 KB
/
Copy pathserver.py
File metadata and controls
156 lines (105 loc) · 4.23 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
import os
import time
import redis
import logging
from flask import Flask, render_template, request, abort
import animator
from helpers import colorTools, microcontroller, animations, dimmer
from helpers.state import State, Multiplier
SECONDS_TO_IDLE = 60 * 29
# State management
cacheBust = int(time.time())
r = redis.Redis(charset="utf-8", decode_responses=True)
r.ping()
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
app = Flask(__name__)
@app.route("/")
def homepage():
return render_template('homepage.html', cacheBust=cacheBust)
@app.route("/random/<type>", methods=['POST'])
def random_color(type):
onlyOnIdle = request.args.get('automated')
now = int(time.time())
state = State.fromRedis(r)
if onlyOnIdle:
secondUntilIdle = state.lastModified + SECONDS_TO_IDLE - now
if state.lockedBy:
logging.info(f"Ignoring automated colour request, colours locked by {state.lockedBy}")
return '', 304
if secondUntilIdle > 0:
logging.info(f"Ignoring automated colour request, not idle for another {secondUntilIdle / 60} mins")
return '', 304
hue = colorTools.generateRandomDifferentHue(state.colors[0].hue) if len(state.colors) > 0 else colorTools.generateRandomHue()
color = colorTools.Color.fromHue(hue)
if type == 'single':
colors = color.multiplyBy(microcontroller.NUM_LEDS)
newState = State(now, [color], Multiplier.SINGLE_COLOR)
elif type == 'columns':
secondHue = colorTools.generateRandomDifferentHue(hue)
secondColor = colorTools.Color.fromHue(secondHue)
colors = colorTools.generateGradientColumns(color, secondColor)
newState = State(now, [color, secondColor], Multiplier.COLUMNS_GRADIENT)
elif type == 'gradient':
secondHue = colorTools.generateRandomDifferentHue(hue)
secondColor = colorTools.Color.fromHue(secondHue)
steps = microcontroller.NUM_LEDS
colors = colorTools.generateColorGradient(color, secondColor, steps)
newState = State(now, [color, secondColor], Multiplier.GRADIENT)
else:
abort(400, description=f'Unknown type of random display, given "{type}"')
animator.send(None)
response = microcontroller.sendColors(dimmer.dimColorsIfNight(colors))
newState.save(r)
return response
@app.route("/lights", methods=['POST'])
def lights():
inputData = request.get_json(force=True)
multiplier = inputData['multiplier']
rawColors = inputData['colors']
if len(rawColors) == 0:
abort(400, description='No colors given')
colors = list(map(colorTools.Color.fromDict, rawColors))
now = int(time.time())
if multiplier == 'columns':
ledColors = colorTools.generateLedColumns(colors)
newState = State(now, colors, Multiplier.COLUMNS)
elif isinstance(multiplier, int):
ledColors = colorTools.generateLedBlocks(colors, multiplier)
newState = State(now, colors, Multiplier.REPEATING)
else:
abort(400, description=f'Unknown multiplier "{multiplier}"')
animator.send(None)
response = microcontroller.sendColors(dimmer.dimColorsIfNight(ledColors))
newState.save(r)
return response
@app.route("/animations/<name>", methods=['POST'])
def animation(name):
if name not in animations.animations:
animationNames = ", ".join(animations.animations.keys())
abort(400, description=f'No animation found with name "{name}". Available: {animationNames}')
else:
animationInstance = animations.animations[name]()
animator.send(animationInstance)
lastModified = int(time.time())
newState = State(lastModified, [], Multiplier.NONE, name)
newState.save(r)
return ''
@app.route("/state")
def led_state():
# Blah blah blah API boundaries
return r.get(State.REDIS_KEY)
@app.route("/lock", methods=['POST'])
def lock():
state = State.fromRedis(r)
state.lockedBy = request.remote_addr
state.save(r)
return '', 201
@app.route("/lock", methods=['DELETE'])
def unlock():
state = State.fromRedis(r)
state.lockedBy = None
state.save(r)
return '', 204
if __name__ == "__main__":
port = int(os.environ.get('PORT', 8080))
app.run(host='0.0.0.0', port=port)