Skip to content

Commit

Permalink
add timeout when waiting for weight to stabilize and separate callbac…
Browse files Browse the repository at this point in the history
…ks for different characteristics
  • Loading branch information
sroemer committed Sep 12, 2024
1 parent 57fae7d commit 0171a87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ There also seem to be some identical scales with a different branding on the mar

## Versions / Changelog

- v0.2.0: Wait for weight to stabilize with a timeout and add command line arguments
- v0.1.1: Use asyncio.sleep() instead of dev. info requests to keep notifications incoming
- v0.1.0: The client is able to get the weight from the scale.

Expand Down
55 changes: 37 additions & 18 deletions src/crenot-gofit-s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class CrenotGofitS2:
print_svc_info = False

is_weight_stable = False
wait_stable_timeout = 10
weight = 0 # in grams

def __init__(self, print_dev_info=False, print_svc_info=False):
def __init__(self, print_dev_info=False, print_svc_info=False, timeout=10):
self.print_dev_info = print_dev_info
self.print_svc_info = print_svc_info
self.wait_stable_timeout = timeout

async def run(self):
name = "Crenot Gofit S2"
Expand All @@ -38,16 +40,24 @@ async def run(self):
if self.print_svc_info:
await self.print_services()

await self.start_notification("FFB2")
# await self.start_notification("FFB3")
# await self.start_notification("2A05")

logging.info("Waiting for weight to stabilize")
while not self.is_weight_stable:
logging.debug(f"weight:{self.weight}")
await asyncio.sleep(1)

logging.info(f" - Weight: {self.weight/1000: .2f}kg")
await self.start_notification("FFB2", self.on_ffb2_notification)
# await self.start_notification("FFB3", self.on_ffb3_notification)
# await self.start_notification("2A05", self.on_2a05_notification)

logging.info(f"Waiting for weight to stabilize (timeout:{self.wait_stable_timeout}s)" )

try:
async with asyncio.timeout(self.wait_stable_timeout):
while not self.is_weight_stable:
logging.debug(f"weight:{self.weight}")
await asyncio.sleep(1)
logging.info(f" - Weight: {self.weight/1000: .2f}kg")
except asyncio.TimeoutError:
logging.error(f" - Timeout (Last weight: {self.weight/1000: .2f}kg)")
except asyncio.CancelledError:
logging.error(f" - Cancelled (Last weight: {self.weight/1000: .2f}kg)")
except Exception as e:
logging.error("Waiting for stable weight failed with exception", e)

###
# connect()
Expand Down Expand Up @@ -120,24 +130,30 @@ async def print_services(self):
# start_notification()
# - triggers the scale to send notification/indication messages for the given uuid
###
async def start_notification(self, uuid):
async def start_notification(self, uuid, callback):

logging.info(f"Starting notifications for uuid {uuid}")
await self.client.start_notify(uuid, self.notify_callback)
await self.client.start_notify(uuid, callback)

###
# notify_callback()
# - called when notification/indication message is received
# on_xxxx_notififaction()
# - called when notification/indication message of according uuid is received
###
async def notify_callback(self, sender: BleakGATTCharacteristic, data: bytearray):
logging.debug(f" - received data {data.hex()}")
async def on_ffb2_notification(self, sender: BleakGATTCharacteristic, data: bytearray):
logging.debug(f" - FFB2: received data {data.hex()}")
if not self.is_weight_stable:
# weight is stored in bytes 6, 7 and 8 but only 2 bits of byte 6 are used
# therefore we extract the value by binary or operation 262143
self.weight = int.from_bytes([ data[6], data[7], data[8] ]) & 262143
if data[4] == 2:
self.is_weight_stable = True

async def on_ffb3_notification(self, sender: BleakGATTCharacteristic, data: bytearray):
logging.info(f" - FFB3: received data {data.hex()}")

async def on_2a05_notification(self, sender: BleakGATTCharacteristic, data: bytearray):
logging.info(f" - 2A05: received data {data.hex()}")



# Execute when the not initialized from an import statement.
Expand All @@ -147,7 +163,10 @@ async def notify_callback(self, sender: BleakGATTCharacteristic, data: bytearray
parser = argparse.ArgumentParser(description="Client application for 'Crenot Gofit S2' scale")
parser.add_argument('--print_dev_info', action='store_true', help='enable output of device information')
parser.add_argument('--print_svc_info', action='store_true', help='enable output of service information')
parser.add_argument('--timeout', type=float, default=10, help='timeout when waiting for weight to stabilize')
args = parser.parse_args()

logging.basicConfig(level=logging.INFO, format=" - %(levelname)s \t%(message)s")
asyncio.run(CrenotGofitS2(print_dev_info=args.print_dev_info, print_svc_info=args.print_svc_info).run())
asyncio.run(CrenotGofitS2(print_dev_info=args.print_dev_info,
print_svc_info=args.print_svc_info,
timeout=args.timeout).run())

0 comments on commit 0171a87

Please sign in to comment.