-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·137 lines (100 loc) · 3.2 KB
/
server.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
#!/usr/bin/env python
import argparse
import logging
import sys
import unicornhat as unicorn
from flask import Flask
from flask import make_response
from flask import request
LOG_LEVEL = "loglevel"
logger = logging.getLogger(__name__)
x_cache = None
y_cache = None
flask = Flask(__name__)
@flask.route("/clear")
def clear():
unicorn.clear()
return response()
@flask.route("/show")
def show():
unicorn.show()
return response()
@flask.route("/x")
def x():
global x_cache
if not x_cache:
x_cache = unicorn.get_shape()[0]
return response(x_cache)
@flask.route("/y")
def y():
global y_cache
if not y_cache:
y_cache = unicorn.get_shape()[1]
return response(y_cache)
@flask.route("/set_all")
def set_all():
r = request.args.get("r")
g = request.args.get("g")
b = request.args.get("b")
unicorn.set_all(r=int(r), g=int(g), b=int(b))
return response()
@flask.route("/set_all_rgb/<string:rgb>")
def set_all_rgb(rgb):
s = rgb.split(",")
unicorn.set_all(r=int(s[0]), g=int(s[1]), b=int(s[2]))
return response()
@flask.route("/set_pixel")
def set_pixel():
x = request.args.get("x")
y = request.args.get("y")
r = request.args.get("r")
g = request.args.get("g")
b = request.args.get("b")
unicorn.set_pixel(x=int(x), y=int(y), r=int(r), g=int(g), b=int(b))
return response()
@flask.route("/set_pixel_rgb/<string:rgb>")
def set_pixel_rgb(rgb):
x = request.args.get("x")
y = request.args.get("y")
s = rgb.split(",")
unicorn.set_pixel(x=int(x), y=int(y), r=int(s[0]), g=int(s[1]), b=int(s[2]))
return response()
@flask.route("/get_pixel")
def get_pixel():
x = request.args.get("x")
y = request.args.get("y")
val = str(unicorn.get_pixel(x=int(x), y=int(y)))[1:-1]
return response(val)
@flask.route("/rotation/<int:rotation>")
def rotation(rotation):
unicorn.rotation(rotation)
return response()
@flask.route("/brightness/<float:brightness>")
def brigtness(brightness):
unicorn.brightness(brightness)
return response("")
@flask.route("/get_brightness")
def get_brightness():
return response(unicorn.get_brightness())
def response(val=""):
resp = make_response(str(val), 200)
resp.headers["Content-Type"] = "text/plain"
resp.headers["Access-Control-Allow-Origin"] = "http://snap.berkeley.edu"
resp.headers["Access-Control-Allow-Methods"] = "GET, POST"
return resp
def setup_logging(filename=None,
filemode="a",
stream=sys.stderr,
level=logging.INFO,
format="%(asctime)s %(name)-10s %(funcName)-10s():%(lineno)i: %(levelname)-6s %(message)s"):
if filename:
logging.basicConfig(filename=filename, filemode=filemode, level=level, format=format)
else:
logging.basicConfig(stream=stream, level=level, format=format)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", dest=LOG_LEVEL, default=logging.INFO, action="store_const",
const=logging.DEBUG, help="Enable debugging info")
args = vars(parser.parse_args())
setup_logging(level=args[LOG_LEVEL])
flask.run(host="0.0.0.0", port=9000)