-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_control_point.py
82 lines (60 loc) · 1.85 KB
/
simple_control_point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php or see LICENSE file.
# Copyright 2007-2008 Brisa Team <[email protected]>
import time
from brisa.core.reactors import SelectReactor
reactor = SelectReactor()
from brisa.upnp.control_point import ControlPoint
from brisa.core.threaded_call import run_async_function
devices = []
def on_new_device(dev):
print 'Got new device:', dev
if dev.udn not in [d.udn for d in devices]:
devices.append(dev)
def on_removed_device(udn):
print 'Device is gone:', udn
for dev in devices:
if dev.udn == udn:
devices.remove(dev)
def create():
c = ControlPoint()
c.subscribe('new_device_event', on_new_device)
c.subscribe('removed_device_event', on_removed_device)
return c
def list_devices(devices):
count = 0
for d in devices:
print 'Device number: ', count
print_device(d, 1)
print
count += 1
def print_device(dev, indent=1):
print '\t'*indent, 'UDN (id): ', dev.udn
print '\t'*indent, 'Name: ', dev.friendly_name
print '\t'*indent, 'Type: ', dev.device_type
print '\t'*indent, 'Services: ', dev.services.keys()
print '\t'*indent, 'Embedded devices: '
for d in dev.devices.values():
print_device(d, indent+1)
print
def list_services(dev):
count = 0
for k, serv in dev.services.items():
print 'Service number: ', count
print 'Service id: ' + serv.id
print
count += 1
def main():
c = create()
c.start()
reactor.add_after_stop_func(c.stop)
run_async_function(run, (c, ))
reactor.main()
def run(c):
c.start_search(600, 'ssdp:all') # async / threaded
time.sleep(35) # necessary...
list_devices(devices)
c.stop_search()
reactor.main_quit()
if __name__ == '__main__':
main()