-
Notifications
You must be signed in to change notification settings - Fork 0
/
bleak_model.py
345 lines (295 loc) · 12 KB
/
bleak_model.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
This module contains the BleakModel class, which is a wrapper around the BleakScanner class.
The state machine (pytransitions) adds public methods to this class
(methods that don't start with an underscore).
Underscore-prefixed methods are meant to be called by the state machine,
and should not raise exceptions.
Instead, they should return True if the method was successful, and False if it was not.
You are responsible for dealing with transitions that weren't successful.
"""
import asyncio
import logging
from bleak import BleakClient, BleakScanner
# typing
from transitions.extensions.asyncio import AsyncMachine
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
class BleakModel:
"""
This class is a transitions.AsyncModel wrapper around the BleakScanner class.
It is used to scan and receive data for Bluetooth Low Energy devices,
while providing a simple state machine interface to the programmer.
Many of this class's methods are meant to be state machine callbacks,
so they are prefixed with an underscore to prevent accidentally calling them directly.
Methods that are called by the state machine must not raise exceptions.
Instead, return True if the method was successful, and False if it was not.
The user of the state machine is responsible for
dealing with transitions that weren't successful.
"""
# class variable to store all instances of BleakModel, for easy cleanup
instances = []
# class variable to store the discovered devices, since we can only have one BleakScanner
bt_devices = {}
_stop_scan_event = asyncio.Event() # class variable to stop the scan
async def __aenter__(self):
"""
Entering the `async with` context manager. Does nothing.
"""
async def __aexit__(self, *args):
"""
Exiting the `async with` context manager. Cleans up.
"""
await self.clean_up()
@classmethod
async def clean_up_all(cls):
"""
Go to Init state from all states for all instances of BleakModel.
Call when handling exceptions or when program is exiting.
"""
for instance in cls.instances:
await instance.clean_up()
return True
@classmethod
async def _start_scan(cls):
"""
Worker that runs the BLE scan.
"""
cls.bt_devices = {} # clear the list
cls._stop_scan_event.clear() # clear the stop_scan_event
def detection_callback(device, advertisement_data):
cls.bt_devices.update({device.address: (device, advertisement_data)})
async with BleakScanner(detection_callback) as scanner:
await cls._stop_scan_event.wait() # continues to scan until stop_scan_event is set
return True
@classmethod
async def start_scan(cls):
"""
Non-blocking start of the BLE scan.
"""
loop = asyncio.get_event_loop()
loop.set_exception_handler(
lambda loop, context: logging.error(
f"Exception: {context.get('exception')}"
)
)
asyncio.create_task(cls._start_scan())
return True
@classmethod
async def stop_scan(cls):
"""
Stop the BLE scan.
"""
cls._stop_scan_event.set()
return True
def _setup_state_machine(self):
"""
Initialize the state machine.
"""
states = ["Init", "TargetSet", "Connected", "Streaming"]
self.machine = AsyncMachine(model=self, states=states, initial="Init")
self.machine.add_transition(
trigger="set_target",
source="Init",
dest="TargetSet",
conditions="_set_target",
)
self.machine.add_transition(
trigger="unset_target",
source="TargetSet",
dest="Init",
before="_unset_target",
)
self.machine.add_transition(
trigger="connect",
source="TargetSet",
dest="Connected",
conditions="_connect_to_device_with_timeout",
)
self.machine.add_transition(
trigger="stream",
source="Connected",
dest="Streaming",
# potentially check if the measurement handler is set in a 'condition'.
conditions="_setup_stream",
after="_nonblocking_stream",
)
self.machine.add_transition(
trigger="disconnect",
source="Connected",
dest="TargetSet",
before="_disconnect_from_device",
)
# After a stream is stopped, we can't go back to Connected
# because we can't re-use the BleakClient object.
# Therefore we need to go one more back to TargetSet,
self.machine.add_transition(
trigger="disconnect",
source="Streaming",
dest="TargetSet",
before="_stop_stream_and_disconnect_from_device",
)
def __init__(self, connection_timeout=5.0, logging_level=logging.WARNING):
logging.basicConfig(level=logging_level)
self._setup_state_machine()
self.connection_timeout = connection_timeout # seconds
self.bleak_client: BleakClient = None
self.ble_device: BLEDevice = None
self.advertisement_data: AdvertisementData = None
self.target = None
self._stop_streaming_event = asyncio.Event()
self.wrapped_client = None
# --- The following must be defined by user after instantiation ---
# Callable that sets self.wrapped_client.
# If you use anything besides BleakClient, you must define this.
self.wrap = lambda client: client
# Callable that takes in the wrapped client and starts notifications.
self.enable_notifications = None
# Callable that takes in the wrapped client and stops notifications.
self.disable_notifications = None
# Callable that takes in the wrapped client and returns the measurement handler.
# It 'sets' the 'setter'.
self.set_measurement_handler = None
BleakModel.instances.append(self)
async def clean_up(self):
logging.info("Cleaning up.")
self._stop_streaming_event.set()
try:
if self.state == "Streaming":
await self.disconnect()
if self.state == "Connected":
await self.disconnect()
if self.state == "TargetSet":
await self.unset_target()
except Exception as e:
logging.error(f"An error occurred during cleanup: {str(e)}")
return False
return True
def _set_target(self, address):
try:
if address in BleakModel.bt_devices:
self.target = address
return True
else:
logging.error(f"Address {address} not found in discovered devices")
return False
except Exception as e:
logging.error(f"An error occurred while setting the target. Error: {e}")
return False
def _unset_target(self):
self.target = None
async def _connect_to_device_with_timeout(self):
"""
Connect to the device with a timeout (seconds)
"""
try:
return await asyncio.wait_for(
self._connect_to_device(), timeout=self.connection_timeout
)
except asyncio.TimeoutError:
logging.warning(f"Timed out while connecting to {self.target}")
# We must disconnect so that the device is returned to the list of discovered devices
await self._disconnect_from_device()
return False
async def _connect_to_device(self):
if len(BleakModel.bt_devices) == 0:
logging.error("No devices found")
return False
try:
self.ble_device, self.advertisement_data = BleakModel.bt_devices.pop(
self.target
) # remove the device from the list to avoid connecting to it multiple times
except Exception as e:
logging.error(
f"""
Bluetooth device {self.target} not found in scanned list.
It could be powered off, connected to another device, or to another BleakModel.
Error: {e}
"""
)
return False
try:
self.bleak_client = BleakClient(self.ble_device)
# we don't use the async context manager because
# we want to access the client object from the disconnect function
connected = await self.bleak_client.connect()
if connected:
logging.info(f"Connected to {self.target}")
self.wrapped_client = self.wrap(self.bleak_client)
return True
else:
logging.warning(f"Failed to connect to {self.target}")
except Exception as e:
logging.error(
f"An error occurred while connecting to {self.target}. Error: {e}"
)
return False
return True
async def _disconnect_from_device(self):
try:
BleakModel.bt_devices.update(
{self.target: (self.ble_device, self.advertisement_data)}
) # put it back in the list
await self.bleak_client.disconnect()
logging.info(f"Disconnected from {self.target}")
return True
except Exception as e:
logging.error(f"An error occurred while disconnecting. Error: {e}")
return False
async def _setup_stream(self):
try:
self._stop_streaming_event.clear()
if not isinstance(self.wrapped_client, BleakClient):
logging.info(
f"""Wrapped client is not a BleakClient object.
Assuming it's a Pycycling object and calling `set_measurement_handler`.
"""
)
self.set_measurement_handler(self.wrapped_client)
await self.enable_notifications(self.wrapped_client)
return True
except Exception as e:
logging.error(
f"""
An error occurred while setting up the stream.\n
Double-check that the enable_notifications and set_measurement_handler methods are set,
and that they take in a BleakClient or similar (Pycycling) object."
Error: {e}
"""
)
return False
async def _nonblocking_stream(self):
"""
Run _stream_from_device in a detached coroutine.
Allowing users to `await model.stream()` without blocking.
"""
asyncio.create_task(self._stop_streaming_event.wait())
# We checked as best we could that the stream is correctly set up in _setup_stream()
# And since we can't know at this point if the stream is actually working,
# We have to assume it is and return True.
return True
async def send_command(self, command):
raise NotImplementedError("Todo: Implement this method")
async def _stop_stream_from_device(self):
try:
self._stop_streaming_event.set()
await self.disable_notifications(self.wrapped_client)
logging.info("Stopped streaming")
return True
except Exception as e:
logging.error(f"An error occurred while stopping streaming: {e}")
return False
async def _stop_stream_and_disconnect_from_device(self):
"""
Since we can't re-use the connection after stopping notify,
we bundle the stop streaming and disconnect logic
so that the state jumps from Streaming to Init.
"""
try:
await self._stop_stream_from_device()
await self._disconnect_from_device()
return True
except Exception as e:
logging.warning(
f"An error occurred while stopping streaming. Continuing to disconnect from device. Error: {e}"
)
return False