-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
180 lines (137 loc) · 3.61 KB
/
app.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
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
import sys
import time
from http.client import HTTPException
from flask import Flask, jsonify, request
from blinkt import set_brightness, set_pixel, show, clear, get_pixel, NUM_PIXELS
import json
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.errorhandler(Exception)
def handle_error(e):
code = 500
if isinstance(e, HTTPException):
code = e.code
set_brightness(0.1)
set_pixel(0, 255, 0, 0)
set_pixel(1, 255, 165, 0)
set_pixel(2, 255, 0, 0)
set_pixel(3, 255, 165, 0)
set_pixel(4, 255, 0, 0)
set_pixel(5, 255, 165, 0)
set_pixel(6, 255, 0, 0)
set_pixel(7, 255, 165, 0)
show()
time.sleep(0.25)
set_pixel(0, 255, 165, 0)
set_pixel(1, 255, 0, 0)
set_pixel(2, 255, 165, 0)
set_pixel(3, 255, 0, 0)
set_pixel(4, 255, 165, 0)
set_pixel(5, 255, 0, 0)
set_pixel(6, 255, 165, 0)
set_pixel(7, 255, 0, 0)
show()
time.sleep(0.25)
set_pixel(0, 255, 0, 0)
set_pixel(1, 255, 165, 0)
set_pixel(2, 255, 0, 0)
set_pixel(3, 255, 165, 0)
set_pixel(4, 255, 0, 0)
set_pixel(5, 255, 165, 0)
set_pixel(6, 255, 0, 0)
set_pixel(7, 255, 165, 0)
show()
time.sleep(0.25)
set_pixel(0, 255, 165, 0)
set_pixel(1, 255, 0, 0)
set_pixel(2, 255, 165, 0)
set_pixel(3, 255, 0, 0)
set_pixel(4, 255, 165, 0)
set_pixel(5, 255, 0, 0)
set_pixel(6, 255, 165, 0)
set_pixel(7, 255, 0, 0)
show()
time.sleep(0.25)
return jsonify(error=str(e)), code
@app.route('/red')
def set_red():
set_brightness(0.5)
for x in range(NUM_PIXELS):
set_pixel(x, 255, 0, 0)
show()
data = {
"status": 200,
"message": "Lights are now red"
}
return jsonify(data)
@app.route('/green')
def set_green():
set_brightness(0.5)
for x in range(NUM_PIXELS):
set_pixel(x, 0, 255, 0)
show()
data = {
"status": 200,
"message": "Lights are now green"
}
return jsonify(data)
@app.route('/blue')
def set_blue():
set_brightness(0.5)
for x in range(NUM_PIXELS):
set_pixel(x, 0, 0, 255)
show()
data = {
"status": 200,
"message": "Lights are now blue"
}
return jsonify(data)
@app.route('/off')
def turn_off():
set_brightness(0)
for x in range(NUM_PIXELS):
set_pixel(x, 0, 0, 0)
show()
data = {
"status": 200,
"message": "Lights are now off"
}
return jsonify(data)
@app.route('/on')
def turn_on():
set_brightness(1)
for x in range(8):
set_pixel(x, 255, 255, 255)
show()
data = {
"status": 200,
"message": "Lights are now on"
}
return jsonify(data)
@app.route('/custom', methods=['POST'])
def turn_custom():
data = request.get_json()
# return jsonify(data)
for x in data:
print(jsonify(x), file=sys.stderr)
set_pixel(x['led'], x['red'], x['green'], x['blue'])
show()
data = {
"status": 200,
"message": "Custom pattern applied"
}
return jsonify(data)
@app.route('/status', methods=['GET'])
def report_status():
pixel_status = []
for i in range(NUM_PIXELS):
pixel_status.append(dict(Pixel=i, Status=dict(zip(["Red","Green","Blue","Brightness"],get_pixel(i)))))
data = {
"status": 200,
"message": pixel_status
}
return jsonify(data) # This ruins the order of keys for humans. JSON doesn't care, and neither do we.
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)