Skip to content

Commit

Permalink
add two part message parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Nov 13, 2024
1 parent 32941f9 commit 5fbefe5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
24 changes: 21 additions & 3 deletions aioacaia/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

from .const import (
DEFAULT_CHAR_ID,
HEADER1,
HEADER2,
HEARTBEAT_INTERVAL,
NOTIFY_CHAR_ID,
OLD_STYLE_CHAR_ID,
)
from .exceptions import AcaiaDeviceNotFound, AcaiaError
from .exceptions import AcaiaDeviceNotFound, AcaiaError, AcaiaMessageTooShort
from .const import UnitMass
from .decode import Message, Settings, decode
from .helpers import encode, encode_id, encode_notification_request, derive_model_name
Expand Down Expand Up @@ -88,6 +90,8 @@ def __init__(
self._queue: asyncio.Queue = asyncio.Queue()
self._add_to_queue_lock = asyncio.Lock()

self._last_short_msg: bytearray | None = None

self._msg_types["auth"] = encode_id(is_pyxis_style=is_new_style_scale)

if not is_new_style_scale:
Expand Down Expand Up @@ -222,7 +226,7 @@ async def connect(
address_or_ble_device=self.address_or_ble_device,
disconnected_callback=self.device_disconnected_handler,
)
self._client.get_services()

try:
await self._client.connect()
except BleakError as ex:
Expand Down Expand Up @@ -389,7 +393,21 @@ async def on_bluetooth_data_received(
data: bytearray,
) -> None:
"""Receive data from scale."""
msg = decode(data)[0]

# For some scales the header is sent and then in next message the content
if (
self._last_short_msg is not None
and self._last_short_msg[0] == HEADER1
and self._last_short_msg[1] == HEADER2
):
data = self._last_short_msg + data
self._last_short_msg = None

try:
msg = decode(data)[0]
except AcaiaMessageTooShort as ex:
self._last_short_msg = ex.bytes_recvd
return

if isinstance(msg, Settings):
self._device_state = AcaiaDeviceState(
Expand Down
3 changes: 2 additions & 1 deletion aioacaia/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bleak import BleakGATTCharacteristic

from .const import HEADER1, HEADER2
from .exceptions import AcaiaMessageTooShort

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -161,7 +162,7 @@ def decode(byte_msg: bytearray):

if msg_start < 0 or len(byte_msg) - msg_start < 6:
_LOGGER.debug("Message too short %s", byte_msg)
return (None, byte_msg)
raise AcaiaMessageTooShort(byte_msg)

msg_end = msg_start + byte_msg[msg_start + 3] + 5

Expand Down
10 changes: 10 additions & 0 deletions aioacaia/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ class AcaiaError(BleakError):

class AcaiaUnknownDevice(Exception):
"""Exception for unknown devices."""


class AcaiaMessageTooShort(Exception):
"""Exception for messages that are too short."""

def __init__(
self, bytes_recvd: bytearray, message: str = "Message too short"
) -> None:
super().__init__(message)
self.bytes_recvd = bytes_recvd
18 changes: 18 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@

from aioacaia import AcaiaScale
from aioacaia.decode import notification_handler
from aioacaia.decode import decode, Message


async def main():
scale = AcaiaScale("aa:bb:cc:dd:ee:ff")
await scale.on_bluetooth_data_received(None, bytearray(b"\xef\xdd\x0c"))
res = await scale.on_bluetooth_data_received(
None, bytearray(b"\x0c\x05\xdf\x06\x00\x00\x01\x00\x07\x00\x00\x02\xf3\r")
)
decode(
bytearray(b"\xef\xdd\x0c\x0c\x05\xdf\x06\x00\x00\x01\x00\x07\x00\x00\x02\xf3\r")
)
# Message(5, bytearray(b"\xdf\x06\x00\x00\x01\x00\x07\x00\x00\x02\xf3\r"))
# Message(5, b"\xa1\x10\x00\x00\x01\x01\x07\x00\x00\x02\xb5\x18")
# Message(5, bytearray(b"]\x07\x00\x00\x01\x00\x07\x00\x00\x02q\x0e"))
# 0 = 12
# 1 = 5
Message(5, b"\x00\x00\x00\x00\x01\x00\x07\x00\x00\x02\x14\x07")
decode(bytearray(b"\x0c\x05\x00\x00\x00\x00\x01\x00\x07\x00\x00\x02\x14\x07"))
exit(0)

with open("mac.txt", "r") as f:
mac = f.read().strip()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="aioacaia",
version="0.1.4",
version="0.1.5b1",
description="An async implementation of PyAcaia",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 5fbefe5

Please sign in to comment.