forked from maierp/PyRaumfeld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaumfeldControl.py
343 lines (306 loc) · 11.1 KB
/
RaumfeldControl.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
340
341
342
343
# -*- coding: utf-8 -*-
'''
Created on 25.01.2015
@author: patrick
'''
import json
import raumfeld
import threading
from bottle import route, run
from urllib import quote, unquote
updateAvailableEvent = threading.Event()
def __getSingleZone(name_udn):
"""Tries to find the first occurring Zone with the specified name or UDN"""
zone = None
if (name_udn.startswith("uuid:")):
zone = raumfeld.getZoneByUDN(name_udn)
else:
zones = raumfeld.getZonesByName(name_udn.decode('utf-8'))
if (len(zones) > 0):
zone = zones[0]
return zone
def __getSingleRoom(name_udn):
"""Tries to find the first occurring Room with the specified name or UDN"""
room = None
if (name_udn.startswith("uuid:")):
room = raumfeld.getRoomByUDN(name_udn)
else:
rooms = raumfeld.getRoomsByName(name_udn.decode('utf-8'))
if (len(rooms) > 0):
room = rooms[0]
return room
__activeTimers = dict()
@route('/')
def index():
returndata = u'<html><body>'
returndata += u'<b>Global actions:</b>'
returndata += u'<ul>'
returndata += u'<li>/ - this site</li>'
returndata += u'<li>/zones - list zones</li>'
returndata += u'<li>/unassignedRooms - list unassigned rooms</li>'
returndata += u'<li>/waitForChanges - returns the request when something changed in the zone structure</li>'
returndata += u'</ul>'
returndata += u'<b>Zone actions:</b>'
returndata += u'<ul>'
returndata += u'<li>/zone/<name_udn>/volume - get volume from the zone defined by the <name> or <udn></li>'
returndata += u'<li>/zone/<name_udn>/volume/<volume> - set the volume of the zone defined by the <name> or <udn> to <volume></li>'
returndata += u'<li>/zone/<name_udn>/volume/[+/-]<amount> - changes the volume of the zone defined by the <name> or <udn> by [+/-]<amount> percent</li>'
returndata += u'<li>/zone/<name_udn>/rooms - list the rooms in a zone defined by the <name> or <udn></li>'
returndata += u'<li>/zone/<name_udn>/play/<uri> - plays <uri> in the given zone</li>'
returndata += u'<li>/zone/<name_udn>/play - start to play in the given zone</li>'
returndata += u'<li>/zone/<name_udn>/pause - pause the given zone</li>'
returndata += u'<li>/zone/<name_udn>/stop - stop the given zone</li>'
returndata += u'</ul>'
returndata += u'<b>Room actions:</b>'
returndata += u'<ul>'
returndata += u'<li>/room/<name_udn> - Returns the Room name and UDN in JSON format</li>'
returndata += u'<li>/room/<name_udn>/volume - get the volume of the given room</li>'
returndata += u'<li>/room/<name_udn>/volume/<volume> - set the volume of the given room</li>'
returndata += u'<li>/room/<name_udn>/zone - get the zone associated to the given room</li>'
returndata += u'<li>/room/<name_udn>/separate - Separates the the Room defined by the name or UDN from its zone</li>'
returndata += u'</ul>'
returndata += u'</body></html>'
return returndata
################
# Global actions
################
@route('/zones')
def getZones():
"""Returns the Zone names and UDNs in JSON format"""
returndata = {}
returndata["data"] = []
returndata["success"] = False
for zone in raumfeld.getZones():
z = {}
z['name'] = zone.Name
z['udn'] = zone.UDN
returndata["data"].append(z)
returndata["success"] = True
return json.dumps(returndata)
@route('/unassignedRooms')
def getUnassignedRooms():
"""Returns the unassigned room names and UDNs in JSON format"""
returndata = {}
returndata["data"] = []
returndata["success"] = False
for room in raumfeld.getUnassignedRooms():
r = {}
r['name'] = room.Name
r['udn'] = room.UDN
returndata["data"].append(r)
returndata["success"] = True
return json.dumps(returndata)
################
# Zone actions
################
@route('/zone/<name_udn>')
def getZone(name_udn):
"""Returns the Zone name and UDN in JSON format"""
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
z = {}
z['name'] = zone.Name
z['udn'] = zone.UDN
returndata["data"] = z
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/volume')
def getZoneVolume(name_udn):
"""Gets the volume of the Zone defined by the name or UDN"""
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
returndata["data"] = zone.volume
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/volume/<volume:re:\d+>')
def setZoneVolume(name_udn, volume):
"""Sets the volume of the Zone defined by the name or UDN"""
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zone.volume = volume
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/volume/<amount:re:[+-]\d+>')
def changeZoneVolume(name_udn, amount):
"""Changes the volume of the Zone defined by the name or UDN"""
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zone.changeVolume(int(amount))
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/rooms')
def getZoneRooms(name_udn):
"""Gets the rooms of the Zone defined by the name or UDN"""
returndata = {}
returndata["data"] = []
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
for room in zone.getRooms():
r = {}
r['name'] = room.Name
r['udn'] = room.UDN
returndata["data"].append(r)
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/play/<uri:path>')
def zonePlayURI(name_udn, uri):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
#zone.play("dlna-playcontainer://{udn}?sid={sid}&cid={cid}&md=0".format(udn=quote(raumfeld.getMediaServerUDN()), sid=quote("urn:upnp-org:serviceId:ContentDirectory"), cid=quote("0/Playlists/MyPlaylists/Radio/" + unicode(index))))
zone.play(unquote(uri))
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/play')
def zonePlay(name_udn):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zone.play()
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/pause')
def zonePause(name_udn):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zone.pause()
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/stop')
def zoneStop(name_udn):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zone.stop()
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/setTimer/<interval>')
def zoneSetTimer(name_udn, interval):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
zoneCancelTimer(name_udn)
zone.play()
timer = threading.Timer(float(interval), zone.stop)
__activeTimers[zone.UDN] = timer
timer.start()
returndata["success"] = True
return json.dumps(returndata)
@route('/zone/<name_udn>/cancelTimer')
def zoneCancelTimer(name_udn):
returndata = {}
returndata["success"] = False
zone = __getSingleZone(name_udn)
if zone != None:
if zone.UDN in __activeTimers:
timer = __activeTimers[zone.UDN]
del __activeTimers[zone.UDN]
timer.cancel()
returndata["success"] = True
return json.dumps(returndata)
################
# Room actions
################
@route('/room/<name_udn>')
def getRoom(name_udn):
"""Returns the Room name and UDN in JSON format"""
returndata = {}
returndata["success"] = False
room = __getSingleRoom(name_udn)
if room != None:
z = {}
z['name'] = room.Name
z['udn'] = room.UDN
returndata["data"] = z
returndata["success"] = True
return json.dumps(returndata)
@route('/room/<name_udn>/volume')
def getRoomVolume(name_udn):
"""Gets the volume of the Room defined by the name or UDN"""
returndata = {}
returndata["success"] = False
room = __getSingleRoom(name_udn)
if room != None:
returndata["data"] = room.volume
returndata["success"] = True
return json.dumps(returndata)
@route('/room/<name_udn>/volume/<volume:int>')
def setRoomVolume(name_udn, volume):
"""Sets the volume of the Room defined by the name or UDN"""
returndata = {}
returndata["success"] = False
room = __getSingleRoom(name_udn)
if room != None:
room.volume = volume
returndata["success"] = True
return json.dumps(returndata)
@route('/room/<name_udn>/zone')
def getRoomZone(name_udn):
"""Gets the zone json of the Room defined by the name or UDN"""
returndata = {}
returndata["success"] = False
room = __getSingleRoom(name_udn)
if room != None:
zone = raumfeld.getZoneWithRoomUDN(room.UDN)
if zone != None:
returndata["data"] = {}
returndata["data"]["udn"] = zone.UDN
returndata["data"]["name"] = zone.Name
returndata["success"] = True
return json.dumps(returndata)
@route('/room/<name_udn>/separate')
def separateRoom(name_udn):
"""Separates the the Room defined by the name or UDN from its zone"""
global updateAvailableEvent
returndata = {}
returndata["success"] = False
room = __getSingleRoom(name_udn)
if room != None:
raumfeld.connectRoomToZone(room.UDN)
updateAvailableEvent.wait()
returndata["success"] = True
return json.dumps(returndata)
##################
# Wait for Changes
##################
@route('/waitForChanges')
def waitForChanges():
"""Returns when an update in the DataStructure happened"""
global updateAvailableEvent
updateAvailableEvent.wait()
returndata = []
r = {}
r['changes'] = True
returndata.append(r)
return json.dumps(returndata)
def __updateAvailableCallback():
global updateAvailableEvent
updateAvailableEvent.set()
def __resetUpdateAvailableEventThread():
global updateAvailableEvent
while True:
updateAvailableEvent.wait()
updateAvailableEvent.clear()
raumfeld.registerChangeCallback(__updateAvailableCallback)
raumfeld.init()
print("Host URL: " +raumfeld.hostBaseURL)
# Start observing the device list
resetUpdateAvailableEventThread = threading.Thread(target=__resetUpdateAvailableEventThread)
resetUpdateAvailableEventThread.daemon = True
resetUpdateAvailableEventThread.start()
run(host='0.0.0.0', port=8080, debug=True)