Skip to content

Commit

Permalink
Improve data types.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Nov 8, 2023
1 parent a28e03f commit a563ec9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
15 changes: 7 additions & 8 deletions pyplumio/helpers/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,22 @@ def size(self) -> int | None:
return len(self.value) + 1 if self.value is not None else None


class PString(DataType):
class PascalString(DataType):
"""Represents a Pascal string."""

def unpack(self, data: bytes) -> None:
self._size = data[0]
offset = 1
self._value = data[offset : offset + self.size + 1].decode()
"""Unpack the data."""
self._size = data[0] + 1
self._value = data[1 : self.size].decode()


class UID(DataType):
"""Represents an UID string."""

def unpack(self, data: bytes) -> None:
offset = 0
self._size = data[offset]
offset += 1
self._value = decode_uid(data[offset : offset + self.size])
"""Unpack the data."""
self._size = data[0] + 1
self._value = decode_uid(data[1 : self.size])


# The regdata type map.
Expand Down
4 changes: 2 additions & 2 deletions pyplumio/structures/network_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Final

from pyplumio.const import EncryptionType
from pyplumio.helpers.data_types import PString
from pyplumio.helpers.data_types import PascalString
from pyplumio.helpers.typing import EventDataType
from pyplumio.structures import Structure
from pyplumio.utils import ensure_dict
Expand Down Expand Up @@ -98,7 +98,7 @@ def decode(
encryption=EncryptionType(int(message[offset + 26])),
signal_quality=int(message[offset + 27]),
status=bool(message[offset + 28]),
ssid=PString.from_bytes(message, offset + 33).value,
ssid=PascalString.from_bytes(message, offset + 33).value,
),
server_status=bool(message[offset + 25]),
)
Expand Down
5 changes: 2 additions & 3 deletions pyplumio/structures/product_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Final

from pyplumio.const import ProductType
from pyplumio.helpers.data_types import UID, PString, UnsignedShort
from pyplumio.helpers.data_types import UID, PascalString, UnsignedShort
from pyplumio.helpers.typing import EventDataType
from pyplumio.structures import StructureDecoder
from pyplumio.utils import ensure_dict
Expand Down Expand Up @@ -50,14 +50,13 @@ def decode(
uid = UID.from_bytes(message, offset)
offset += uid.size

offset += 1
logo = UnsignedShort.from_bytes(message, offset)
offset += logo.size

image = UnsignedShort.from_bytes(message, offset)
offset += image.size

model_name = PString.from_bytes(message, offset)
model_name = PascalString.from_bytes(message, offset)
offset += model_name.size

return (
Expand Down

0 comments on commit a563ec9

Please sign in to comment.