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