-
Notifications
You must be signed in to change notification settings - Fork 6
/
web.py
339 lines (265 loc) · 9.17 KB
/
web.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
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
import cherrypy
import time
import eyes
import eye_effect
import config
class SheepyWeb(object):
def __init__(self, queue, runner, cm, watchdog):
self.queue = queue
self.runner = runner
self.cm = cm
self.watchdog = watchdog
self.show_library = show_library = {}
for name in runner.master_shows:
show_library[name] = s = {
'type': "master"
}
if name in runner.random_eligible_shows:
s['random'] = True
for name in runner.eo_shows:
existing = show_library.get(name)
if existing is None:
show_library[name] = {
'type': "eyes_only",
'controls_eyes': True
}
else:
existing['controls_eyes'] = True
for name in runner.overlay_shows:
show_library[name] = {
'type': 'overlay'
}
@cherrypy.expose
def clear_show(self):
self.queue.put("clear")
return "<a href='.'/>Back</a>"
@cherrypy.expose
def change_run_time(self, run_time=None):
try:
print("RUNTIME XXXXX:::: %s" % run_time)
run_time = int(run_time)
self.queue.put("inc runtime:%s"%run_time)
except Exception as e:
print("\n\nCRASH\n\n", e)
#probably a string... do nothing!
pass
return "<a href='.'/>Back</a>"
@cherrypy.expose
def index_old(self):
# set a no-cache header so the show status is up to date
cherrypy.response.headers['Cache-Control'] = "no-cache, no-store, must-revalidate, max-age=0"
cherrypy.response.headers['Expires'] = 0
ret_html = "<h1></h1>"
ret_html += "<p>" + self.runner.status() + "</p>"
ret_html += "<b>Choose a show</b><ul>"
for s in sorted(self.shows):
ret_html += "<li><a href='run_show?show_name=%s' > %s</a>" % (s, s)
ret_html += "<br><br><a href='clear_show' > CLEAR SHOW // STOP</a>"
ret_html += """<br><br><h3>Set Show Cycle Time(seconds):<form name=change_run_time action='change_run_time'>
Seconds:<input type=text name=run_time value=60><input type=submit></form>
"""
return(ret_html)
@cherrypy.expose
def run_show(self, show_name=None):
if show_name:
self.queue.put("run_show:"+show_name)
print("setting show to:", show_name)
else:
print("didn't get a show name")
# XXX otherwise the runner.status() method
# hasn't had time to update
time.sleep(0.2)
raise cherrypy.HTTPRedirect("/")
@cherrypy.expose
def admin(self):
raise cherrypy.HTTPRedirect("/static/admin.html")
@cherrypy.expose
@cherrypy.tools.json_out()
def shows(self):
return self.show_library
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def start_show(self):
data = cherrypy.request.json
name = data.get("name")
print("Start show name='%s'" % name)
self.runner.next_show(name=name)
return {'ok': True}
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def start_eo_show(self):
data = cherrypy.request.json
name = data.get("name")
print("Start EO show name='%s'" % name)
self.runner.next_eo_show(name=name)
return {'ok': True}
@cherrypy.expose
@cherrypy.tools.json_out()
def stop_eo_show(self):
self.runner.next_eo_show(None)
return {'ok': True}
@cherrypy.expose
@cherrypy.tools.json_out()
def status(self):
now = time.time()
out = {
'show': {
'name': self.runner.show.name,
'run_time': int(self.runner.show_runtime * 1000)
},
'message': self.cm.message,
'max_time': int(self.runner.max_show_time * 1000),
'reset_state': {
'party': {
'mode': self.runner.model.party_eye.reset_mode,
'duration': int((now - self.runner.model.party_eye.reset_changed_at)* 1000)
},
'business': {
'mode': self.runner.model.business_eye.reset_mode,
'duration': int((now - self.runner.model.business_eye.reset_changed_at)* 1000)
}
}
}
if self.runner.eo_show is not None:
out['eo_show'] = {
'name': self.runner.eo_show.name
}
return out
@cherrypy.expose
@cherrypy.tools.json_out()
def effect_presets(self):
out = []
for e in self.cm.effects:
out.append(e.as_json())
return out
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def save_effect(self):
data = cherrypy.request.json
effect = eye_effect.EyeEffect(json=data)
if self.cm.set_effect_preset(data.get("effect_index"), effect):
out = {"ok": True}
else:
out = {"ok": False}
return out
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def manual_eyes(self):
data = cherrypy.request.json
if not data.get("enabled"):
# Done with manual mode
self.runner.set_force_mute(False)
return {"ok": True}
# Make sure force mute is enabled
self.runner.set_force_mute(True)
print("Got data %s" % str(data))
effect = None
if data.get("effect"):
effect = eye_effect.EyeEffect(json=data.get("effect"))
x = data.get("x_pos")
y = data.get("y_pos")
z = data.get("z_pos")
is_xyz = data.get("pos_is_xyz") or False
dimmer = data.get("dimmer")
color_pos = data.get("color_pos")
if data.get("set_party"):
eye = self.runner.model.party_eye
if effect is not None:
eye.effect = effect
if is_xyz:
p = [x, y, z]
print("Setting pos of %s" % str(p))
eye.set_xyz_pos(p, False)
else:
print("Setting party pan=%f, tilt=%f" % (x, y))
eye.pan = x
eye.tilt = y
if isinstance(color_pos, int):
eye.color_pos = color_pos
else:
print("NOT setting color_pos")
if isinstance(dimmer, float):
eye.dimmer = dimmer
else:
print("NOT setting dimmer")
if data.get("set_business"):
eye = self.runner.model.business_eye
if effect is not None:
eye.effect = effect
if is_xyz:
p = [x, y, z]
print("Setting pos of %s" % str(p))
eye.set_xyz_pos(p, False)
else:
print("Setting party pan=%f, tilt=%f" % (x, y))
eye.pan = x
eye.tilt = y
if isinstance(color_pos, int):
eye.color_pos = color_pos
else:
print("NOT setting color_pos")
if isinstance(dimmer, float):
eye.dimmer = dimmer
else:
print("NOT setting dimmer")
eye.go();
return {"ok": True}
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def server_reset(self):
data = cherrypy.request.json
if not data.get("please"):
return {"ok": False, "msg": "You didn't say please"}
self.watchdog.manual_reset()
return {"ok": True}
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def store_position(self):
data = cherrypy.request.json
x = data.get("x_pos")
y = data.get("y_pos")
z = data.get("z_pos")
is_xyz = data.get("pos_is_xyz") or False
which = data.get("position")
eye_positions = config.get("eye_positions")
if is_xyz:
xyz_pos = [x,y,z]
pnt_p = eyes.xyz_to_pnt(xyz_pos, True)
pnt_b = eyes.xyz_to_pnt(xyz_pos, False)
else:
pnt_p = [x,y]
pnt_b = [x,y]
if which == "disco":
# Don't need to look at left / right. Disco is only left
p = eye_positions["disco"]
p[0] =pnt_p
elif which == "headlights":
p = eye_positions["headlights"]
p[0] = pnt_p
p[1] = pnt_b
try:
config.save()
except Exception:
return {"ok": False, "msg": "Couldn't save the positon"}
return {"ok": True}
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def set_reset_state(self):
data = cherrypy.request.json
is_party = data.get("is_party")
mode = data.get("mode")
if is_party:
eye = self.runner.model.party_eye
else:
eye = self.runner.model.business_eye
eye.reset_mode = mode
if eye.reset_mode == mode:
return {"ok": True}
return {"ok": False}