Updated the methods of ZukeboxControlPoint class.
authorHenry <hmmb@a01m02.(none)>
Fri, 17 Jul 2009 17:38:12 +0000 (13:38 -0400)
committerHenry <hmmb@a01m02.(none)>
Fri, 17 Jul 2009 17:38:12 +0000 (13:38 -0400)
control_point/zukebox_control_point.py

index 04b084b..d439ab9 100644 (file)
@@ -1,12 +1,13 @@
 # Simple UPnP control point using BRisa framework
 
+import thread
+
 from brisa.core.reactors import install_default_reactor
 reactor = install_default_reactor()
 
 from brisa.core.network import parse_url
 from brisa.core.threaded_call import run_async_function
 
-from brisa.upnp.control_point.control_point import ControlPoint
 from brisa.upnp.control_point.control_point_av import ControlPointAV
 
 service = ('u','urn:schemas-upnp-org:service:PlayList:1')
@@ -14,78 +15,117 @@ zukebox_type = 'urn:schemas-upnp-org:device:ZukeBoxServer:1'
 
 class ZukeboxControlPoint(ControlPointAV):
 
-    devices=[]
-
     def __init__(self):
        ControlPointAV.__init__(self)
-       devices=[]
-
-    def start(self):
+       self.devices=[]
+       self.running = False
+       self.initial_subscribes()
 
-        self.cp = self.create_control_point()
-        self.cp.start()
-       print 'Create control point'
-        #reactor.add_after_stop_func(self.cp.stop)
-        #reactor.main()
-       #print 'Reactor main() criado'
-       self.cp.start_search(300, 'upnp:rootdevice')
-       print 'Start_search'
-        while True:
-           if self.set_zukebox_server(self.cp):
-               print 'Searching Zukebox Server'
-               break
 
-       print 'After searching'
-       print self.cp.get_current_server().services()
-       return self.cp
-
-
-    def create_control_point(self):
-        """ Creates the control point and binds callbacks to device events.
-        """
-        cp = ControlPointAV()
-        cp.subscribe('new_device_event', self.on_new_device)
-        cp.subscribe('removed_device_event', self.on_removed_device)
-        return cp
+    def initial_subscribes(self):
+        self.subscribe('new_device_event', self.on_new_device)
+        self.subscribe('removed_device_event', self.on_removed_device)
 
 
     def on_new_device(self, dev):
-
-        if not dev:
-            return
-
-        devices.append(dev)
+        self.devices.append(dev)
 
 
     def on_removed_device(self, udn):
-
-        for dev in devices:
+       device = None
+        for dev in self.devices:
            if dev.udn == udn:
-               devices.remove(dev)
-
-
-    def get_switch_service(self, device):
-        return device.services[service[1]]
-
-
-    def set_zukebox_server(self, cp):
-        
-        for device in cp.get_devices():
-           print 'set_zukebox_server'
-            if device.values().friendly_name == 'Zukebox-Server':
-                cp.set_current_server(device)
-                return True
-
-        return False
-
-
-    def get_playlist(self, cp):
-
-       playlist = {}
-       try:
-            device = cp.get_current_server()
-            service = device.get_service_by_type('urn:schemas-upnp-org:service:Playlist:1')
-            playlist = service.GetPlaylist()
+               device = dev
+       self.devices.remove(device)
+
+
+    def run(self):
+       self.running = True
+       self.start()
+        reactor.add_after_stop_func(self.stop)
+       thread.start_new_thread(self.handle_cmds, ())
+        reactor.main()
+
+
+    def _list_devices(self):
+        n=0
+        for dev in self.devices:
+            print 'device %d: ' % n
+            print '\tudn:', dev.udn
+            print '\tfriendly_name:', dev.friendly_name
+            print '\tservices:', dev.services
+            print '\ttype:', dev.device_type
+            n+=1
+
+
+    def _select_zukebox_device(self):
+        zukebox_device = None
+        for dev in self.devices:
+            if dev.friendly_name == 'ZukeBox Server':
+                zukebox_device = dev
+        return zukebox_device
+
+
+    def handle_cmds(self):
+        try:
+            n=0
+            op=True
+            while self.running:
+                while op:
+                    self.start_search(600, 'upnp:rootdevice')
+                    self._list_devices();
+                    self.device = self._select_zukebox_device()
+                    if self.device:
+                        op=False
+                self.set_current_server(self.device)
+                self.append()
+                self.drop(1)
+                ret=self.getPlaylist()
+                print ret
+                list=ret['PlayList']
+                print list
+                playlist=eval(list)
+                print playlist
+                self.running = False
+        except Exception, e:
+            print 'Handle_cmds: Exception %s' %e
+                
+                
+    def append(self):
+        try:
+            device = self.get_current_server()
+            service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
+            locked = service.IsLocked()
+            if locked['Locked'] == '0':
+                d= {'CurrentURI':'teste', 'CurrentURIMetaData':'teste', 'FromName':'teste','ToName':'teste'}
+                service.Append(**d)
+            print service.GetSizeOfPlayList()
+            print service.IsLocked()
+        except Exception, e:
+            print 'Cannot insert the music in the list. Exception: \n %s' %e
+
+
+    def drop(self, id):
+        try:
+            device = self.get_current_server()
+            service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
+            print dir(service)
+            ID = {"Index":id}
+            service.Drop(**ID)
+            print service.GetSizeOfPlayList()
+            print service.IsLocked()
+        except Exception, e:
+            print 'Cannot drop the music of the list. Exception: \n %s' %e
+
+
+    def getPlaylist(self):
+        try:
+            device = self.get_current_server()
+            service = device.get_service_by_type('urn:schemas-upnp-org:service:PlayList:1')
+            playlist = service.GetPlayList()
+            #service.GetCurrentItem()
+            #service.GetNextItem()
+            #service.GetPreviousItem()
        except Exception, e:
             if not service:
                 print 'Service not discovered. Exception: %s' %e
@@ -94,15 +134,13 @@ class ZukeboxControlPoint(ControlPointAV):
            else:
                print '%s'%e
            playlist = None
-       
-       return playlist
+        return playlist
 
 
-    def searchGenreList(self, cp, genreType):
 
-       genre = {}
+    def searchGenreList(self, cp, genreType):
        try:
-           device = cp.get_current_server()
+            device = self._select_zukebox_device()
            service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
            GenreType = {'GenreType':genreType}
            genre = service.SearchGenreList(GenreType)
@@ -119,10 +157,8 @@ class ZukeboxControlPoint(ControlPointAV):
 
 
     def searchMusicList(self, cp, music_name):
-
-       music = {}
        try:
-           device = cp.get_current_server()
+            device = self._select_zukebox_device()
            service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
            MusicName = {'MusicName':music_name}
            music = service.SearchMusicList(MusicName)
@@ -139,10 +175,8 @@ class ZukeboxControlPoint(ControlPointAV):
            
 
     def searchArtistList(self, cp, artist_name):
-
-       artist = {}
        try:
-           device = cp.get_current_server()
+            device = self._select_zukebox_device()
            service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
            ArtistName = {'ArtistName':artist_name}
            artist = service.SearchMusicList(ArtistName)
@@ -159,10 +193,8 @@ class ZukeboxControlPoint(ControlPointAV):
 
 
     def searchAllAudioList(self, cp):
-
-       all_audio = {}
        try:
-           device = cp.get_current_server()
+            device = self._select_zukebox_device()
            service = device.get_service_by_type('urn:schemas-upnp-org:service:Search:1')
            all_audio = service.SearchMusicList()
        except Exception, e:
@@ -182,5 +214,4 @@ class ZukeboxControlPoint(ControlPointAV):
 
 
     def stop(self, cp):
-        cp.stop()
-        reactor.main_quit()
+        self.stop_search()