9e38e450512448b787d2ddce4c46e448d9fbfe46
[zukebox] / control_point / zukebox_control_point.py
1 # Simple UPnP control point using BRisa framework
2
3 from brisa.core.reactors import install_default_reactor
4 reactor = install_default_reactor()
5
6 from brisa.core.network import parse_url
7 from brisa.core.threaded_call import run_async_function
8
9 from brisa.upnp.control_point.control_point import ControlPoint
10
11 service = ('u','urn:schemas-upnp-org:service:AudioLibrary:1')
12 zukebox_type = 'urn:schemas-upnp-org:device:ZukeBoxServer:1'
13
14 def on_new_device(dev):
15
16     print 'Got new device: ', dev.udn
17     if not dev:
18         return
19
20 def on_removed_device(udn):
21
22     print 'Device is gone: ', udn
23
24 def get_switch_service(device):
25     return device.services[service[1]]
26
27 def create_control_point():
28     """ Creates the control point and binds callbacks to device events.
29     """
30     c = ControlPoint()
31     c.subscribe('new_device_event', on_new_device)
32     c.subscribe('removed_device_event', on_removed_device)
33     return c
34
35
36 def main():
37     """ Main loop iteration receiving input commands.
38     """
39     c = create_control_point()
40     c.start()
41     #run_async_function(_handle_cmds, (c, ))
42     reactor.add_after_stop_func(c.stop)
43     reactor.main()
44
45 def _handle_cmds(c):
46     while True:
47         try:
48             input= raw_input('(type help)command: ')
49         except KeyboardInterrupt, EOFError:
50             c.stop()
51             break
52         # Handle command
53         if input == '':
54             continue
55         elif input == 'help':
56             print 'Available commands: '
57             for x in ['help', 'search', 'set_zukebox <dev number>', 'get_playlist', 'stop', 'list', 'exit']:
58                 print '\t%s' % x
59         elif input == 'stop':
60             # Stop searching
61             c.stop_search()
62
63         elif input == 'search':
64             # Start searching
65             c.start_search(300, 'upnp:rootdevice')
66
67         elif input == 'list':
68             # List devices found
69             k=0
70             for d in c.get_devices().values():
71                 print 'Device no.:', k
72                 print 'UDN: ', d.udn
73                 print 'Name: ', d.friendly_name
74                 print 'Device type', d.device_type
75                 print 'Services: ', d.services.keys()
76                 print 'Embedded devices:', [dev.friendly_name for dev in d.devices.values()]
77                 print
78                 k+=1
79         elif input.startswith('set_zukebox'):
80             try:
81                 c.current_server = devices[int(input.split(' ')[1])]
82             except:
83                 print 'Zukebox number not found. Please run list and check againg'
84                 c.current_server = None
85         elif input == 'get_playlist':
86             try:
87                 service = get_switch_service(c.current_server)
88                 playlist = service.GetPlaylist()
89                 for music in playlist:
90                     print '%s', music.name
91             except Exception, e:
92                 if not hasattr(c, 'current_server') or not c.current_server:
93                     print 'Zukebox device not set. Please use set zukebox <n>'
94                 else:
95                     print 'Erro in get_playlist: ',e
96         elif input == 'exit':
97             break
98
99     reactor.main_quit()
100
101 if __name__ == '__main__':
102     main()