Merged playlist
[zukebox] / control_point / zukebox_control_point.py
index 408be8d..c45a6a9 100644 (file)
@@ -7,3 +7,97 @@ 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
+
+service = ('u','urn:schemas-upnp-org:service:AudioLibrary:1')
+zukebox_type = 'urn:schemas-upnp-org:device:ZukeBoxServer:1'
+
+def on_new_device(dev):
+
+    print 'Got new device: ', dev.udn
+    print "Type 'list' to see the whole list"
+    if not dev:
+        return
+
+def on_removed_device(udn):
+
+    print 'Device is gone: ', udn
+
+def get_switch_service(device):
+    return device.services[service[1]]
+
+def create_control_point():
+    """ Creates the control point and binds callbacks to device events.
+    """
+    c = ControlPoint()
+    c.subscribe('new_device_event', on_new_device)
+    c.subscribe('removed_device_event', on_removed_device)
+    return c
+
+
+def main():
+    """ Main loop iteration receiving input commands.
+    """
+    c = create_control_point()
+    c.start()
+    run_async_function(_handle_cmds, (c, ))
+    reactor.add_after_stop_func(c.stop)
+    reactor.main()
+
+def _handle_cmds(c):
+    while True:
+       try:
+           input= raw_input('(in doubt use help)command: ')
+       except KeyboardInterrupt, EOFError:
+           c.stop()
+           break
+       # Handle command
+       if input == '':
+           continue
+       elif input == 'help':
+           print 'Available commands: '
+           for x in ['help', 'search', 'set_zukebox <dev number>', 'get_playlist', 'stop', 'list', 'exit']:
+               print '\t%s' % x
+       elif input == 'stop':
+           # Stop searching
+           c.stop_search()
+
+       elif input == 'search':
+           # Start searching
+           c.start_search(300, 'upnp:rootdevice')
+
+       elif input == 'list':
+           # List devices found
+           k=0
+           for d in c.get_devices().values():
+               print 'Device no.:', k
+               print 'UDN: ', d.udn
+               print 'Name: ', d.friendly_name
+               print 'Device type', d.device_type
+               print 'Services: ', d.services.keys()
+               print 'Embedded devices:', [dev.friendly_name for dev in d.devices.values()]
+               print
+               k+=1
+       elif input.startswith('set_zukebox'):
+           try:
+               c.current_server = devices[int(input.split(' ')[1])]
+           except:
+               print 'Zukebox number not found. Please run list and check againg'
+               c.current_server = None
+       elif input == 'get_playlist':
+           try:
+               service = get_switch_service(c.current_server)
+               playlist = service.GetPlaylist()
+               for music in playlist:
+                   print '%s', music.name
+           except Exception, e:
+               if not hasattr(c, 'current_server') or not c.current_server:
+                   print 'Zukebox device not set. Please use set zukebox <n>'
+               else:
+                   print 'Erro in get_playlist: ',e
+       elif input == 'exit':
+           break
+
+    reactor.main_quit()
+
+if __name__ == '__main__':
+    main()