Skip to content

Commit

Permalink
fixed padding
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Wiege committed Dec 3, 2024
1 parent 4f9d668 commit 85aba87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespaces = true
# PEP 621 https://peps.python.org/pep-0621/
[project]
name = "onebase"
version = "0.1.13"
version = "0.1.14"
# for automated versioning by pypa/setuptools_scm. see https://peps.python.org/pep-0621/#dynamic
# dynamic = ["version"]
description = "A python package for local communication with Viessmann OneBase devices over CAN, Ethernet or WiFi."
Expand Down
29 changes: 16 additions & 13 deletions src/onebase/core/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,22 @@ def __init__(self, paramNumBytes: int, paramDIDName: str, paramOffset: int = 0):
def encode(self, string_ascii: Any, paramRaw:bool=False) -> bytes:
if(paramRaw):
_hexString = string_ascii
return CodecRaw.encode(self, _hexString)
_encodedBytes = CodecRaw.encode(self, _hexString)
else:
_byteArrayUTF8 = string_ascii.encode(encoding="utf-8")
return _byteArrayUTF8
_encodedBytes = _byteArrayUTF8


_paddingByte = b'\x00'
if len(_encodedBytes) < self._numBytes:
_resultPadddedCropped = _encodedBytes.ljust(self._numBytes, _paddingByte)
elif len(_encodedBytes) > self._numBytes:
_resultPadddedCropped = _encodedBytes[0:self._numBytes]
else:
_resultPadddedCropped = _encodedBytes


return _resultPadddedCropped

def decode(self, paramEncodedBytes: bytes, paramRaw:bool=False) -> Any:
if(paramRaw):
Expand Down Expand Up @@ -466,25 +478,16 @@ def __init__(self, paramNumBytes:int, paramDIDName:str, subTypes : list):

def encode(self, string_ascii:Any, paramRaw:bool=False) -> bytes:
if(paramRaw):
_result = CodecRaw.encode(self, string_ascii) #just convert hex string to bytes
_encodedBytes = CodecRaw.encode(self, string_ascii) #just convert hex string to bytes
else:
try:
_encodedBytes = bytes()
for subType in self.subTypes:
_encodedBytes+=subType.encode(string_ascii[subType._DIDName])
except KeyError as e:
raise ValueError(f"Cannot encode value due to missing key: {e}")
_result = _encodedBytes

_paddingByte = b'\x00'
if len(_result) < self._numBytes:
_resultPadddedCropped = _result.ljust(self._numBytes, _paddingByte)
elif len(_result) > self._numBytes:
_resultPadddedCropped = _result[0:self._numBytes]
else:
_resultPadddedCropped = _result

return _resultPadddedCropped
return _encodedBytes

def decode(self, paramEncodedBytes: bytes, paramRaw:bool=False) -> Any:
if(paramRaw): #just convert hex string to bytes
Expand Down

0 comments on commit 85aba87

Please sign in to comment.