From 599f0cc7c0db26c0f4bd829e5806e071617742d2 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Sun, 30 Oct 2022 12:39:43 +0300 Subject: [PATCH] [~] Refactor --- dotfiles/.vimrc | 1 + python/tasks/ble.py | 78 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 python/tasks/ble.py diff --git a/dotfiles/.vimrc b/dotfiles/.vimrc index 4967447..0e5c21a 100644 --- a/dotfiles/.vimrc +++ b/dotfiles/.vimrc @@ -37,6 +37,7 @@ set noswapfile set wrap set textwidth=100 set colorcolumn=100 +set backspace=indent,eol,start colorscheme morning syntax on diff --git a/python/tasks/ble.py b/python/tasks/ble.py new file mode 100644 index 0000000..708673f --- /dev/null +++ b/python/tasks/ble.py @@ -0,0 +1,78 @@ +import bleak +import pprint + +async def f1(): + devices = await bleak.BleakScanner.discover() + return devices + +async def f2(device, timeout=None): + if timeout is None: + timeout = 1.0 + + assert isinstance(timeout, float) and timeout >= 1e-8 + + p = await bleak.BleakClient( + device, + timeout=timeout, + ).__aenter__() + return p + +async def f3(client): + t1 = [ + dict( + service=o.__dict__, + characteristics=[ + o2.__dict__ + for o2 in o.characteristics + ] + ) + for o in client.services + ] + return t1 + +async def f5(name=None): + t2 = [] + + attempt = 0 + while True: + t1 = await f1() + pprint.pprint([o.__dict__ for o in t1]) + + if not name is None: + assert isinstance(name, str) + t5 = { + i : o.details[0].name() + for i, o in enumerate(t1) + } + + t2.extend( + [ + t1[k] + for k, v in t5.items() + if isinstance(v, str) and v.lower() == name + ] + ) + + if len(t2) > 0: + break + + attempt += 1 + print('\rattempt #%d' % attempt, end='') + + return t2 + +async def f4(timeout=None): + t2 = await f5(name='watch fit') + + if len(t2) == 0: + print('not found') + return + + t3 = None + try: + t3 = await f2(t2[0], timeout=timeout) + t4 = await f3(t3) + pprint.pprint(t4) + finally: + if not t3 is None: + await t3.disconnect()