Make sure to flush stdout + some refractoring

This commit is contained in:
Odd Stråbø 2021-11-03 04:35:49 +01:00
parent 06453f90e1
commit 0ee53a73ea
1 changed files with 70 additions and 24 deletions

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import datetime import datetime
import struct import struct
import sys
import time import time
from bluepy import btle from bluepy import btle
@ -200,6 +201,7 @@ def parsePacket(data):
print(timestamp, res) print(timestamp, res)
return res return res
print(timestamp, data) print(timestamp, data)
sys.stdout.flush()
class Delegate(btle.DefaultDelegate): class Delegate(btle.DefaultDelegate):
@ -227,36 +229,80 @@ def write(fh, data):
fh.write(data + bcrc) fh.write(data + bcrc)
dlgt = Delegate() def construct_request(address, words=1):
return struct.pack("!BBHH", 0xFF, 0x03, address, words)
prev = time.time() - INTERVAL
while True: def poll(dev: btle.Peripheral, timeout: float = 1) -> bool:
try: start = time.time()
with btle.Peripheral(MAC).withDelegate(dlgt) as dev: while not dev.waitForNotifications(0.2):
if time.time() < start + timeout:
return False
wd = dev.getCharacteristics(uuid=write_device)[0] while dev.waitForNotifications(0.2):
pass
while True: return True
dev.waitForNotifications(1)
now = time.time()
diff = now - prev
if diff >= INTERVAL:
prev += INTERVAL
write(wd, CMD_GET_PANEL_STATUS) def log(string: str):
dev.waitForNotifications(1) print(datetime.datetime.utcnow().isoformat(" "), string)
sys.stdout.flush()
write(wd, CMD_GET_BATTERY_STATE)
dev.waitForNotifications(1)
# if STATUS.get('load_enabled'): if __name__ == "__main__":
# write(wd, CMD_DISABLE_LOAD) dlgt = Delegate()
# else:
# write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError: prev = time.time() - INTERVAL
print(datetime.datetime.utcnow().isoformat(" "), "ERROR: Disconnected")
time.sleep(1) while True:
try:
log("Connecting...")
with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
wd = dev.getCharacteristics(uuid=write_device)[0]
log("Connected.")
poll(dev)
write(wd, construct_request(0, 32))
poll(dev)
poll(dev)
poll(dev)
# Memory dump
# for address in range(0, 0x10000, 16):
# log(f"Reading 0x{address:04X}...")
# write(wd, construct_request(address, 16))
# poll(dev)
# poll(dev)
# poll(dev)
# poll(dev)
# poll(dev)
while True:
poll(dev)
now = time.time()
diff = now - prev
if diff >= INTERVAL:
prev += INTERVAL
write(wd, construct_request(0x0107, 4)) # CMD_GET_PANEL_STATUS
poll(dev)
write(wd, construct_request(0x0100, 7)) # CMD_GET_BATTERY_STATE
poll(dev)
# if STATUS.get('load_enabled'):
# write(wd, CMD_DISABLE_LOAD)
# else:
# write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError:
log("ERROR: Disconnected")
time.sleep(1)
try:
dev.close()
except Exception:
pass