Reconnect if the BLE connection dies

This commit is contained in:
Odd Stråbø 2021-11-02 23:48:06 +01:00
parent 2578fdca75
commit 06453f90e1

View file

@ -230,40 +230,33 @@ def write(fh, data):
dlgt = Delegate() dlgt = Delegate()
prev = time.time() - INTERVAL prev = time.time() - INTERVAL
with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# for svc in dev.services:
# print(svc, svc.uuid)
# print(dir(dev)) while True:
try:
with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# wd = dev.getServiceByUUID(write_service) wd = dev.getCharacteristics(uuid=write_device)[0]
# print(wd) while True:
dev.waitForNotifications(1)
wd = dev.getCharacteristics(uuid=write_device)[0] now = time.time()
# print(cs) diff = now - prev
# print(dir(cs)) if diff >= INTERVAL:
# print(wd.write(b'\xf0\x03\x00\x0c\x00\x08\x91\xd1')) prev += INTERVAL
# rd = dev.getCharacteristics(uuid=read_device)[0] write(wd, CMD_GET_PANEL_STATUS)
# print(rd.read()) dev.waitForNotifications(1)
while True: write(wd, CMD_GET_BATTERY_STATE)
dev.waitForNotifications(1) dev.waitForNotifications(1)
now = time.time()
diff = now - prev
if diff >= INTERVAL:
prev += INTERVAL
write(wd, CMD_GET_PANEL_STATUS)
dev.waitForNotifications(1)
write(wd, CMD_GET_BATTERY_STATE)
dev.waitForNotifications(1)
# if STATUS.get('load_enabled'): # if STATUS.get('load_enabled'):
# write(wd, CMD_DISABLE_LOAD) # write(wd, CMD_DISABLE_LOAD)
# else: # else:
# write(wd, CMD_ENABLE_LOAD) # write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError:
print(datetime.datetime.utcnow().isoformat(" "), "ERROR: Disconnected")
time.sleep(1)