| @@ 23-87 (lines=65) @@ | ||
| 20 | import bleak |
|
| 21 | #from bleak import BleakClient |
|
| 22 | ||
| 23 | class Bleak: |
|
| 24 | """Interface class between curio loop and asyncio loop running bleak |
|
| 25 | ||
| 26 | This class is basically just a queue interface. It has two queue, |
|
| 27 | one for incoming messages `in_queue` and one for outgoing messages `out_queue`. |
|
| 28 | ||
| 29 | A loop running in asyncio's event_loop waits for messages on the `in_queue`. |
|
| 30 | ||
| 31 | The `out_queue` is used to respond to "discover" and "connect" messages with the |
|
| 32 | list of discovered devices and a connected device respectively. All messages |
|
| 33 | incoming from a device are relayed directly to a call back function, and does |
|
| 34 | not go through either of these queues. |
|
| 35 | ||
| 36 | ||
| 37 | """ |
|
| 38 | ||
| 39 | def __init__(self): |
|
| 40 | # Need to start an event loop |
|
| 41 | self.in_queue = curio.UniversalQueue() # Incoming message queue |
|
| 42 | self.out_queue = curio.UniversalQueue() # Outgoing message queue |
|
| 43 | ||
| 44 | self.devices = [] |
|
| 45 | #self.loop = threading.Thread(target=self.run, daemon=True) |
|
| 46 | #self.loop.start() |
|
| 47 | ||
| 48 | def run(self): |
|
| 49 | #self.loop = asyncio.new_event_loop() |
|
| 50 | #asyncio.set_event_loop(self.loop) |
|
| 51 | self.loop = asyncio.get_event_loop() |
|
| 52 | self.loop.run_until_complete(self.asyncio_loop()) |
|
| 53 | ||
| 54 | async def asyncio_loop(self): |
|
| 55 | ||
| 56 | # Wait for messages on in_queue |
|
| 57 | done = False |
|
| 58 | while not done: |
|
| 59 | msg = await self.in_queue.get() |
|
| 60 | if isinstance(msg, tuple): |
|
| 61 | msg, val = msg |
|
| 62 | await self.in_queue.task_done() |
|
| 63 | if msg == 'discover': |
|
| 64 | print('Awaiting on bleak discover') |
|
| 65 | devices = await bleak.discover(timeout=1, loop=self.loop) |
|
| 66 | print('Done Awaiting on bleak discover') |
|
| 67 | await self.out_queue.put(devices) |
|
| 68 | elif msg == 'connect': |
|
| 69 | device = bleak.BleakClient(address=val, loop=self.loop) |
|
| 70 | self.devices.append(device) |
|
| 71 | await device.connect() |
|
| 72 | await self.out_queue.put(device) |
|
| 73 | elif msg == 'tx': |
|
| 74 | device, char_uuid, msg_bytes = val |
|
| 75 | await device.write_gatt_char(char_uuid, msg_bytes) |
|
| 76 | elif msg == 'notify': |
|
| 77 | device, char_uuid, msg_handler = val |
|
| 78 | await device.start_notify(char_uuid, msg_handler) |
|
| 79 | elif msg =='quit': |
|
| 80 | print("quitting") |
|
| 81 | logging.info('quitting') |
|
| 82 | for device in self.devices: |
|
| 83 | await device.disconnect() |
|
| 84 | done = True |
|
| 85 | print("quit") |
|
| 86 | else: |
|
| 87 | print(f'Unknown message to Bleak: {msg}') |
|
| 88 | ||
| 89 | ||
| 90 | ||
| @@ 23-85 (lines=63) @@ | ||
| 20 | import bleak |
|
| 21 | from bleak import BleakClient |
|
| 22 | ||
| 23 | class Bleak: |
|
| 24 | """Interface class between curio loop and asyncio loop running bleak |
|
| 25 | ||
| 26 | This class is basically just a queue interface. It has two queue, |
|
| 27 | one for incoming messages `in_queue` and one for outgoing messages `out_queue`. |
|
| 28 | ||
| 29 | A loop running in asyncio's event_loop waits for messages on the `in_queue`. |
|
| 30 | ||
| 31 | The `out_queue` is used to respond to "discover" and "connect" messages with the |
|
| 32 | list of discovered devices and a connected device respectively. All messages |
|
| 33 | incoming from a device are relayed directly to a call back function, and does |
|
| 34 | not go through either of these queues. |
|
| 35 | ||
| 36 | ||
| 37 | """ |
|
| 38 | ||
| 39 | def __init__(self): |
|
| 40 | # Need to start an event loop |
|
| 41 | self.in_queue = curio.UniversalQueue() # Incoming message queue |
|
| 42 | self.out_queue = curio.UniversalQueue() # Outgoing message queue |
|
| 43 | ||
| 44 | self.devices = [] |
|
| 45 | #self.loop = threading.Thread(target=self.run, daemon=True) |
|
| 46 | #self.loop.start() |
|
| 47 | ||
| 48 | def run(self): |
|
| 49 | #self.loop = asyncio.new_event_loop() |
|
| 50 | #asyncio.set_event_loop(self.loop) |
|
| 51 | self.loop = asyncio.get_event_loop() |
|
| 52 | self.loop.run_until_complete(self.asyncio_loop()) |
|
| 53 | ||
| 54 | async def asyncio_loop(self): |
|
| 55 | ||
| 56 | # Wait for messages on in_queue |
|
| 57 | done = False |
|
| 58 | while not done: |
|
| 59 | msg = await self.in_queue.get() |
|
| 60 | if isinstance(msg, tuple): |
|
| 61 | msg, val = msg |
|
| 62 | await self.in_queue.task_done() |
|
| 63 | if msg == 'discover': |
|
| 64 | print('Awaiting on bleak discover') |
|
| 65 | devices = await bleak.discover(timeout=1, loop=self.loop) |
|
| 66 | print('Done Awaiting on bleak discover') |
|
| 67 | await self.out_queue.put(devices) |
|
| 68 | elif msg == 'connect': |
|
| 69 | device = BleakClient(address=val, loop=self.loop) |
|
| 70 | self.devices.append(device) |
|
| 71 | await device.connect() |
|
| 72 | await self.out_queue.put(device) |
|
| 73 | elif msg == 'tx': |
|
| 74 | device, char_uuid, msg_bytes = val |
|
| 75 | await device.write_gatt_char(char_uuid, msg_bytes) |
|
| 76 | elif msg == 'notify': |
|
| 77 | device, char_uuid, msg_handler = val |
|
| 78 | await device.start_notify(char_uuid, msg_handler) |
|
| 79 | elif msg =='quit': |
|
| 80 | logging.info('quitting') |
|
| 81 | for device in self.devices: |
|
| 82 | await device.disconnect() |
|
| 83 | done = True |
|
| 84 | else: |
|
| 85 | print(f'Unknown message to Bleak: {msg}') |
|
| 86 | ||
| 87 | ||
| 88 | ||