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