[~] Refactor

This commit is contained in:
Siarhei Siniak 2022-10-30 12:39:43 +03:00
parent eeb473b7aa
commit 599f0cc7c0
2 changed files with 79 additions and 0 deletions

@ -37,6 +37,7 @@ set noswapfile
set wrap set wrap
set textwidth=100 set textwidth=100
set colorcolumn=100 set colorcolumn=100
set backspace=indent,eol,start
colorscheme morning colorscheme morning
syntax on syntax on

78
python/tasks/ble.py Normal file

@ -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()