-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_discovery.py
77 lines (67 loc) · 2.03 KB
/
run_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
import click
from bluetooth.discovery.core_bluetooth import CoreBluetoothDiscovery
from bluetooth.discovery.csv import CSVDiscovery
from bluetooth.discovery.nrf import NRFBluetoothDiscovery
from listeners.display_devices import CursesDisplayDevicesListener
from listeners.link_devices import LinkDevicesListener
from listeners.log import LogListener
DISCOVERY_BACKENDS = {
"core": CoreBluetoothDiscovery,
"nrf": NRFBluetoothDiscovery,
"csv": CSVDiscovery,
}
LISTENERS = {
"list": CursesDisplayDevicesListener,
"link": LinkDevicesListener,
}
@click.command()
@click.option(
"--backend",
type=click.Choice(DISCOVERY_BACKENDS.keys()),
default="nrf",
help="Discovery backend to be used.",
prompt=True,
)
@click.option(
"--listener",
type=click.Choice(LISTENERS.keys()),
default="list",
help="Listener to be used.",
prompt=True,
)
@click.option(
"--devices_log",
type=click.File("a"),
default=None,
help="File to log devices to",
prompt=False,
)
@click.option(
"--encounters_log",
type=click.File("r+"),
default=None,
help="File to log encounters to/read from in csv discovery mode",
prompt=False,
)
def run_discovery(backend, listener, devices_log, encounters_log):
backend_class = DISCOVERY_BACKENDS[backend]
listener_class = LISTENERS[listener]
listeners = [listener_class()]
if devices_log or encounters_log and not backend_class == CSVDiscovery:
listeners.append(
LogListener(devices_log=devices_log, encounters_log=encounters_log)
)
if backend_class == CSVDiscovery:
if not encounters_log:
raise ValueError("csv discovery backend needs encounters_log to read from")
bluetooth_discovery = backend_class(
listeners=listeners, encounters_log=encounters_log
)
else:
bluetooth_discovery = backend_class(listeners=listeners)
try:
bluetooth_discovery.start()
finally:
bluetooth_discovery.cleanup()
if __name__ == "__main__":
run_discovery()