-
Notifications
You must be signed in to change notification settings - Fork 39
/
bleak.py
89 lines (71 loc) · 3.21 KB
/
bleak.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
# Copyright 2019 Virantha N. Ekanayake
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Interface to the BLEAK library in Linux for BLE calls
"""
import curio, asyncio, threading, logging
import bleak
from bleak import BleakClient
class Bleak:
"""Interface class between curio loop and asyncio loop running bleak
This class is basically just a queue interface. It has two queue,
one for incoming messages `in_queue` and one for outgoing messages `out_queue`.
A loop running in asyncio's event_loop waits for messages on the `in_queue`.
The `out_queue` is used to respond to "discover" and "connect" messages with the
list of discovered devices and a connected device respectively. All messages
incoming from a device are relayed directly to a call back function, and does
not go through either of these queues.
"""
def __init__(self):
# Need to start an event loop
self.in_queue = curio.UniversalQueue() # Incoming message queue
self.out_queue = curio.UniversalQueue() # Outgoing message queue
self.devices = []
#self.loop = threading.Thread(target=self.run, daemon=True)
#self.loop.start()
def run(self):
#self.loop = asyncio.new_event_loop()
#asyncio.set_event_loop(self.loop)
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(self.asyncio_loop())
async def asyncio_loop(self):
# Wait for messages on in_queue
done = False
while not done:
msg = await self.in_queue.get()
if isinstance(msg, tuple):
msg, val = msg
await self.in_queue.task_done()
if msg == 'discover':
print('Awaiting on bleak discover')
devices = await bleak.discover(timeout=1, loop=self.loop)
print('Done Awaiting on bleak discover')
await self.out_queue.put(devices)
elif msg == 'connect':
device = BleakClient(address=val, loop=self.loop)
self.devices.append(device)
await device.connect()
await self.out_queue.put(device)
elif msg == 'tx':
device, char_uuid, msg_bytes = val
await device.write_gatt_char(char_uuid, msg_bytes)
elif msg == 'notify':
device, char_uuid, msg_handler = val
await device.start_notify(char_uuid, msg_handler)
elif msg =='quit':
logging.info('quitting')
for device in self.devices:
await device.disconnect()
done = True
else:
print(f'Unknown message to Bleak: {msg}')