-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControl.py
277 lines (199 loc) · 9.39 KB
/
Control.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
# -*- encoding: utf-8 -*-
import json
from Liquidsoap import liq
from Logger import sylog
from models import PlaylistElement, Studio, SyCantMove, session, Media, Action
from Utilities import Utilities
class Control:
sender = None
def __init__(self):
pass
def send_feedback(self, request, result="success", details=None):
"""
Send feedback to Dradis.
"""
if "id" not in request:
request["id"] = None
objmsg = {"type": "feedback", "id": request['id'], "request": request, "result": result}
if details is not None:
objmsg['error_msg'] = details
sylog("%(result)s: %(details)s" % {'result': result, 'details': repr(details)})
if self.sender:
self.sender(objmsg)
def exec_global(self, request):
"""
Execute a global-scoped command.
"""
command = request["command"]
if command == "go back to perm":
liq.set_var("selected", "perm")
liq.send_command("perm-switch.skip")
self.send_feedback(request)
elif command == "rescan":
result = Utilities.rescan()
self.send_feedback(request, result)
elif command == "select":
studio = session.query(Studio).find_by(slug=request["studio"].decode('utf-8'))
if studio is not None:
studio.selected = True
session.add(studio)
session.commit()
liq.set_var("selected", str(studio.slug))
sylog("INFO: Selecting " + studio.slug)
self.send_feedback(request)
else:
self.send_feedback(request, "error", "Unknown studio %s" % request["studio"])
def exec_command(self, request):
"""
Execute a studio-related command.
"""
studio = Studio.find_by(slug=request['studio'])
try:
command = request['command']
flag = request['flag']
if studio is None:
return
if command == "live":
if flag == "on":
studio.jukebox.state = u"stopped"
liq.set_var("%s.jukebox.switch" % studio.slug, False)
liq.set_var("%s.plateau.volume" % studio.slug, 1.0)
elif flag == "off":
liq.set_var("%s.plateau.volume" % studio.slug, 0.0)
liq.set_var("%s.jukebox.volume" % studio.slug, 1.0)
else:
self.send_feedback(request, "error", 'flag should be on or off')
self.send_feedback(request)
elif command == "start jukebox":
studio.jukebox.state = u"playing"
liq.set_var("%s.jukebox.switch" % studio.slug, True)
self.send_feedback(request)
# TODO: Create here a fade-in
#if stack.lastSentStatus and stack.lastSentStatus["mode"] == "live":
# liq.set_var("%s.jukebox.volume" % studio.slug, 0.3)
if studio.jukebox.current_element is not None and \
studio.jukebox.current_element.action is not None and \
studio.jukebox.current_element.action.command == "end show":
# TODO: ORLY ?
self.exec_command({"command": "end show", "studio": studio.slug})
studio.jukebox.current_element.mark_as_done()
studio.jukebox.curpos += 1
studio.mark_as_changed()
self.exec_command({"command": "live", "flag": "off", "studio": studio.slug})
elif command == "start jukebox and live off":
self.exec_command({"command": "start jukebox", "studio": studio.slug})
self.exec_command({"command": "live", "flag": "off", "studio": studio.slug})
self.send_feedback(request)
elif command == "enable recorder":
# TODO Enable a recorder
pass
elif command == "disable recorder":
# TODO Disable a recorder
pass
elif command == "start recorder":
# TODO Start a recorder
pass
elif command == "start recorder":
# TODO Stop a recorder
pass
elif command == "start recording":
# TODO Start all enabled recoders
pass
elif command == "stop recording":
# TODO Stop all enabled recorders
pass
elif command == "start show":
studio.jukebox.state = u"playing"
self.exec_command({"command": "start recording", "studio": studio.slug})
self.exec_global({"command": "select", "studio": studio.slug})
self.exec_command({"command": "start jukebox", "studio": studio.slug})
self.send_feedback(request)
elif command == "end show":
studio.jukebox.state = u"stopped"
liq.set_var("%s.running" % studio.slug, False)
liq.set_var("%s.jukebox.switch" % studio.slug, False)
self.exec_command({"command": "stop recording", "studio": studio.slug})
if studio.selected:
studio.selected = False
self.exec_command({"command": "go back to perm"})
self.send_feedback(request)
elif command == "bed" and flag == "on":
liq.set_var("%s.bed.switch" % studio.slug, True)
liq.send_command("%s-bed-switch.skip" % studio.slug)
self.send_feedback(request)
elif command == "bed" and flag == "off":
liq.set_var("%s.bed.switch" % studio.slug, False)
self.send_feedback(request)
elif command == "skip":
liq.send_command("%s-jukebox-stereo.skip" % studio.slug)
studio.jukebox.current_element.mark_as_done()
studio.jukebox.curpos += 1
self.send_feedback(request)
elif command == "set var":
liq.set_var(request["key"], request["value"])
elif command == "push":
element = PlaylistElement.forge(request["element_type"], int(request["element_id"]))
element.status = u"ready"
studio.jukebox.add_element(element)
self.send_feedback(request)
elif command == "insert":
if "action_id" in request:
element = session.query(Action).get(int(request["action_id"]))
elif "media_id" in request:
element = session.query(Media).get(int(request["media_id"]))
else:
# ERROR
return
element.status = u"ready"
studio.jukebox.insert_element(element, int(request["position"]))
self.send_feedback(request)
elif command == "remove":
element = PlaylistElement.find_by(uid=int(request["element_id"]))
studio.jukebox.remove_element(element)
session.delete(element)
self.send_feedback(request)
elif command == "move":
element = PlaylistElement.find_by(uid=int(request["element_id"]))
studio.jukebox.move_element(element, int(request["position"]))
self.send_feedback(request)
elif command == "debug":
if "action" in request:
if request["action"] == "set position":
studio.jukebox.curpos = int(request["position"])
elif request["action"] == "reload current":
if studio.jukebox.current_element is not None and \
studio.jukebox.current_element.status == "loaded":
studio.jukebox.current_element.status = "ready"
studio.mark_as_changed()
elif request["action"] == "force unselect":
studio.selected = False
else:
self.send_feedback(request, "error", "unknown action %s for debug" % request["action"])
return
else:
self.send_feedback(request, "error", "action is required")
return
self.send_feedback(request)
else:
self.send_feedback(request, "error", "Unknown command '%s', ignored." % command)
sylog("Commande %s inconnue ; ignorée." % command)
except SyCantMove as details:
self.send_feedback(request, "error", details.message)
if studio:
studio.mark_as_changed()
session.add(studio)
session.commit()
def parse_line(self, jsoned):
try:
request = json.loads(jsoned)
except ValueError, e:
sylog(e.message)
sylog("Received a corrupted JSON, ignoring...")
return
if 'command' not in request or 'id' not in request:
self.send_feedback(request, "error", "command and id required")
sylog("INFO: Received %s from %s" % (request['command'], request['id']))
if 'studio' in request:
self.exec_command(request)
else:
self.exec_global(request)