-
Notifications
You must be signed in to change notification settings - Fork 48
/
screenbloom.py
executable file
·203 lines (155 loc) · 5.32 KB
/
screenbloom.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
import jinja2.ext
import threading
import socket
import os
import sys
from flask import Flask, render_template, jsonify, request
from modules import screenbloom_functions as sb
app_path = ''
if getattr(sys, 'frozen', False):
app_path = os.path.dirname(sys.executable)
elif __file__:
app_path = os.path.dirname(__file__)
# app = Flask(__name__) # Development
app = Flask(__name__, static_url_path='', static_folder=app_path + '/static', template_folder=app_path + '/templates') # Production
app.secret_key = os.urandom(24)
@app.route('/')
def index():
global startup_thread
if startup_thread.is_alive():
startup_thread.join()
data = sb.get_index_data()
return render_template('/home.html',
update=data['update'],
max_bri=data['max_bri'],
min_bri=data['min_bri'],
default=data['default'],
default_color=data['default_color'],
lights=data['lights'],
lights_number=data['lights_number'],
icon_size=data['icon_size'],
party_mode=data['party_mode'],
state=int(data['state']),
title='Home')
@app.route('/start')
def start():
data = sb.start_screenbloom()
return jsonify(data)
@app.route('/stop')
def stop():
data = sb.stop_screenbloom()
return jsonify(data)
@app.route('/new-user')
def new_user():
return render_template('/new_user.html',
title='New User')
@app.route('/manual')
def manual():
return render_template('/new_user_manual.html',
title='Manual IP')
@app.route('/register', methods=['GET', 'POST'])
def register():
hue_ip = request.args.get('hue_ip', 0, type=str)
data = sb.register_logic(hue_ip, local_host)
return jsonify(data)
@app.route('/update-bri', methods=['POST'])
def update_bri():
if request.method == 'POST':
bri_values = request.json
max_bri = bri_values[0]
min_bri = bri_values[1]
sb.write_config('Light Settings', 'min_bri', min_bri)
sb.write_config('Light Settings', 'max_bri', max_bri)
sb.restart_check()
data = {
'message': 'Brightness Updated!',
'max_bri': max_bri,
'min_bri': min_bri
}
return jsonify(data)
@app.route('/update-update-speed', methods=['POST'])
def update_update_speed():
if request.method == 'POST':
update_speed = float(request.json)
sb.write_config('Light Settings', 'update', update_speed)
sb.restart_check()
data = {
'message': 'Update Speed Updated!',
'value': update_speed
}
return jsonify(data)
@app.route('/update-default-color', methods=['POST'])
def update_default_color():
if request.method == 'POST':
color = request.json
helper = sb.rgb_cie.ColorHelper()
default = helper.hexToRGB(color)
default = '%d,%d,%d' % (default[0], default[1], default[2])
sb.write_config('Light Settings', 'default', default)
sb.restart_check()
data = {
'message': 'Default Color Updated!',
'value': default
}
return jsonify(data)
@app.route('/update-party-mode', methods=['POST'])
def update_party_mode():
if request.method == 'POST':
print 'Update Party Mode Route Hit'
party_mode_state = request.json
wording = 'enabled' if int(party_mode_state) else 'disabled'
sb.write_config('Party Mode', 'running', party_mode_state)
sb.restart_check()
data = {
'message': 'Party mode %s!' % wording
}
return jsonify(data)
@app.route('/update-bulbs', methods=['POST'])
def update_bulbs():
if request.method == 'POST':
bulbs = request.json
sb.write_config('Light Settings', 'active', bulbs)
sb.restart_check()
data = {
'message': 'Bulbs updated!',
}
return jsonify(data)
@app.route('/on-off')
def on_off():
state = request.args.get('state', 0, type=str)
sb.lights_on_off(state)
data = {
'message': 'Turned lights %s' % state
}
return jsonify(data)
# Error Pages
@app.errorhandler(404)
def page_not_found(e):
code = e.code
name = e.name
return render_template('/error.html',
code=code,
name=name)
@app.errorhandler(500)
def page_not_found(e):
error = str(e)
return render_template('/error.html',
code=500,
name='Internal Server Error',
error=error)
if __name__ == '__main__':
local_host = socket.gethostbyname(socket.gethostname())
startup_thread = sb.StartupThread(local_host)
startup_thread.start()
# Flask default server
# app.run(debug=False, host=local_host, use_reloader=False)
# Gevent
# http_server = WSGIServer((local_host, 5000), app)
# http_server.serve_forever()
# Tornado
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()