Updated the methods of ZukeboxControlPoint class.
[zukebox] / control_point / zukebox_control_point.py
1 # Simple UPnP control point using BRisa framework
2
3 import thread
4
5 from brisa.core.reactors import install_default_reactor
6 reactor = install_default_reactor()
7
8 from brisa.core.network import parse_url
9 from brisa.core.threaded_call import run_async_function
10
11 from brisa.upnp.control_point.control_point_av import ControlPointAV
12
13 service = ('u','urn:schemas-upnp-org:service:PlayList:1')
14 zukebox_type = 'urn:schemas-upnp-org:device:ZukeBoxServer:1'
15
16 class ZukeboxControlPoint(ControlPointAV):
17
18     def __init__(self):
19         ControlPointAV.__init__(self)
20         self.devices=[]
21         self.running = False
22         self.initial_subscribes()
23
24
25     def initial_subscribes(self):
26         self.subscribe('new_device_event', self.on_new_device)
27         self.subscribe('removed_device_event', self.on_removed_device)
28
29
30     def on_new_device(self, dev):
31         self.devices.append(dev)
32
33
34     def on_removed_device(self, udn):
35         device = None
36         for dev in self.devices:
37             if dev.udn == udn:
38                 device = dev
39         self.devices.remove(device)
40
41
42     def run(self):
43         self.running = True
44         self.start()
45         reactor.add_after_stop_func(self.stop)
46         thread.start_new_thread(self.handle_cmds, ())
47         reactor.main()
48
49
50     def _list_devices(self):
51         n=0
52         for dev in self.devices:
53             print 'device %d: ' % n
54             print '\tudn:', dev.udn
55             print '\tfriendly_name:', dev.friendly_name
56             print '\tservices:', dev.services
57             print '\ttype:', dev.device_type
58             n+=1
59
60
61     def _select_zukebox_device(self):
62         zukebox_device = None
63         for dev in self.devices:
64             if dev.friendly_name == 'ZukeBox Server':
65                 zukebox_device = dev
66         return zukebox_device
67
68
69     def handle_cmds(self):
70         try:
71             n=0
72             op=True
73             while self.running:
74                 while op:
75                     self.start_search(600, 'upnp:rootdevice')
76                     self._list_devices();
77                     self.device = self._select_zukebox_device()
78                     if self.device:
79                         op=False
80                 self.set_current_server(self.device)
81                 self.append()
82                 self.drop(1)
83                 ret=self.getPlaylist()
84                 print ret
85                 list=ret['PlayList']
86                 print list
87                 playlist=eval(list)
88                 print playlist
89                 self.running = False
90         except Exception, e:
91             print 'Handle_cmds: Exception %s' %e
92                 
93                 
94     def append(self):
95         try:
96             device = self.get_current_server()
97             service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
98             locked = service.IsLocked()
99             if locked['Locked'] == '0':
100                 d= {'CurrentURI':'teste', 'CurrentURIMetaData':'teste', 'FromName':'teste','ToName':'teste'}
101                 service.Append(**d)
102             print service.GetSizeOfPlayList()
103             print service.IsLocked()
104         except Exception, e:
105             print 'Cannot insert the music in the list. Exception: \n %s' %e
106
107
108     def drop(self, id):
109         try:
110             device = self.get_current_server()
111             service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
112             print dir(service)
113             ID = {"Index":id}
114             service.Drop(**ID)
115             print service.GetSizeOfPlayList()
116             print service.IsLocked()
117         except Exception, e:
118             print 'Cannot drop the music of the list. Exception: \n %s' %e
119
120
121     def getPlaylist(self):
122         try:
123             device = self.get_current_server()
124             service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
125             playlist = service.GetPlayList()
126             #service.GetCurrentItem()
127             #service.GetNextItem()
128             #service.GetPreviousItem()
129         except Exception, e:
130             if not service:
131                 print 'Service not discovered. Exception: %s' %e
132             elif not playlist:
133                 print 'Error in get current playlist. Exception: %s' %e
134             else:
135                 print '%s'%e
136             playlist = None
137         return playlist
138
139
140
141     def searchGenreList(self, cp, genreType):
142         try:
143             device = self._select_zukebox_device()
144             service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
145             GenreType = {'GenreType':genreType}
146             genre = service.SearchGenreList(GenreType)
147         except Exception, e:
148             if not service:
149                 print 'Service not discovered. Exception: %s' %e
150             elif not genre:
151                 print 'Error in get genre type list. Exception: %s' %e
152             else:
153                 print '%s' %e
154             genre = None
155
156         return genre
157
158
159     def searchMusicList(self, cp, music_name):
160         try:
161             device = self._select_zukebox_device()
162             service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
163             MusicName = {'MusicName':music_name}
164             music = service.SearchMusicList(MusicName)
165         except Exception, e:
166             if not service:
167                 print 'Service not discovered. Exception: %s' %e
168             elif not music:
169                 print 'Error in get music list by name. Exception: %s' %e
170             else:
171                 print '%s' %e
172             music = None
173
174         return music
175             
176
177     def searchArtistList(self, cp, artist_name):
178         try:
179             device = self._select_zukebox_device()
180             service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
181             ArtistName = {'ArtistName':artist_name}
182             artist = service.SearchMusicList(ArtistName)
183         except Exception, e:
184             if not service:
185                 print 'Service not discovered. Exception: %s' %e
186             elif not artist:
187                 print 'Error in get artist list by name. Exception: %s' %e
188             else:
189                 print '%s' %e
190             artist = None
191
192         return artist
193
194
195     def searchAllAudioList(self, cp):
196         try:
197             device = self._select_zukebox_device()
198             service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
199             all_audio = service.SearchMusicList()
200         except Exception, e:
201             if not service:
202                 print 'Service not discovered. Exception: %s' %e
203             elif not all_audio:
204                 print 'Error in get music list by name. Exception: %s' %e
205             else:
206                 print '%s' %e
207             all_audio = None
208
209         return all_audio
210
211
212     def content_directory(self, cp):
213         print 'Testing content_directory acess '
214
215
216     def stop(self, cp):
217         self.stop_search()