-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBybop_Discovery.py
145 lines (113 loc) · 3.54 KB
/
Bybop_Discovery.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# This sample uses https://pypi.python.org/pypi/zeroconf
# as its MDNS implementation
from zeroconf import ServiceBrowser, Zeroconf
import socket
import threading
class DeviceID(object):
# Drones
BEBOP_DRONE = '0901'
JUMPING_SUMO = '0902'
JUMPING_NIGHT = '0905'
JUMPING_RACE = '0906'
BEBOP_2 = '090c'
MAMBO = '090b'
DISCO = '090e'
ANAFI = '0914'
# Remote controllers
SKYCONTROLLER = '0903'
SKYCONTROLLER_2 = '090f'
SKYCONTROLLER_2P = '0915'
SKYCONTROLLER_3 = '0918'
BEBOP_FAMILY = [
BEBOP_DRONE,
BEBOP_2,
DISCO,
]
ANAFI_FAMILY = [
ANAFI,
]
JUMPING_FAMILY = [
JUMPING_SUMO,
JUMPING_NIGHT,
JUMPING_RACE,
]
MAMBO_FAMILY = [
MAMBO,
]
DRONES = BEBOP_FAMILY + ANAFI_FAMILY + JUMPING_FAMILY + MAMBO_FAMILY
REMOTES = [
SKYCONTROLLER,
SKYCONTROLLER_2,
SKYCONTROLLER_2P,
SKYCONTROLLER_3
]
ALL = DRONES + REMOTES
class Discovery(object):
"""
Basic implementation of a MDNS search for ARSDK Devices.
The protocol here is not covered by the ARSDK but this implementation is
here to provide a fully working sample code.
"""
def __init__(self, deviceId):
"""
Create and start a researcher for devices on network.
Arguments:
- deviceId : List of deviceIds (strings) to search.
"""
self._zeroconf = Zeroconf()
self._browser = []
self._services = {}
self._lock = threading.RLock()
self._cond = threading.Condition(self._lock)
for did in deviceId:
self._browser.append(ServiceBrowser(self._zeroconf, '_arsdk-' +
str(did) + '._udp.local.',
self))
def stop(self):
"""
Stop searching.
When stopped, this object can not be restarted
"""
with self._lock:
self._cond.notify_all()
self._zeroconf.close()
def get_devices(self):
""" Get the current list of devices """
return dict(self._services)
def wait_for_change(self, timeout=None):
"""
Wait for a change in the device list
Keyword arguments:
- timeout : Timeout in floating point seconds for the operation
"""
with self._lock:
self._cond.wait(timeout)
def _signal_change(self):
with self._lock:
self._cond.notify_all()
def remove_service(self, zeroconf, type, name):
""" Internal function for zeroconf.ServiceBrowser. """
if name in self._services:
del self._services[name]
self._signal_change()
def add_service(self, zeroconf, type, name):
""" Internal function for zeroconf.ServiceBrowser. """
info = zeroconf.get_service_info(type, name)
if info is not None:
self._services[name] = info
self._signal_change()
else:
print('Found a service witout info : ' + name + '. Stopping !')
self.stop()
def get_name(device):
""" Get the display name of a device """
return device.name[0:-(len(device.type) + 1)]
def get_ip(device):
""" Get the IP, as string, of a device """
return socket.inet_ntoa(device.address)
def get_port(device):
""" Get the port, as string, of a device """
return str(device.port)
def get_device_id(device):
""" Get the device_id of a device """
return device.type[len('_arsdk-'):-len('._udp.local.')]