Skip to content

Commit

Permalink
Release 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-lex committed Aug 7, 2020
2 parents 1ab68bb + c94e6e0 commit 5eed4ec
Show file tree
Hide file tree
Showing 26 changed files with 1,554 additions and 794 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Changelog
=========

Release 0.3.0
-------------

* [BOF] Generic block and frame code moved from KNX and OPCUA
implementations to core
* [BOF] New "depends:" keyword in JSON specification file, to make the
value of a field depend on the value of another one
* [BOF] First version of developer documentation
* [KNX] Tunneling connection management with ``L_Data`` messages
* [KNX] KNX individual and group address parsing and conversion
4 changes: 3 additions & 1 deletion bof/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ def log(message:str, level:str="INFO") -> bool:

def to_property(value:str) -> str:
"""Replace all non alphanumeric characters in a string with ``_``"""
return sub('[^0-9a-zA-Z]+', '_', value.lower())
if isinstance(value, str):
return sub('[^0-9a-zA-Z]+', '_', value.lower().strip())
return value
48 changes: 48 additions & 0 deletions bof/byte.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
as first parameter.
"""

from re import match

from .base import BOFProgrammingError
from ipaddress import IPv4Address

Expand Down Expand Up @@ -178,6 +180,52 @@ def from_mac(mac:str):
array = bytes.fromhex(mac.replace(":", ""))
return array

def to_knx(value:bytes, group=False) -> str:
"""Converts a 2-bytes array to a KNX individual (X.Y.Z) or group
address (X/Y/Z).
:Individual address ``X.Y.Z``: X and Y are 4 bits (1st byte) and
Z is 8 bits (2nd byte).
:Group address ``X/Y/Z``: X is 6 bits, Y is 3 bits (1st byte) and
Z is 8 bits (2nd byte).
:param value: Byte array (2 bytes) to convert
:param group: Boolean stating if the KNX address is a group address
(default is False: the final string will have a the
format of an individual KNX address.
"""
if not len(value):
return None
first_chunk_size = 5 if group else 4
string_format = "{0}/{1}/{2}" if group else "{0}.{1}.{2}"
bitlist = to_bit_list(value[:1])
x = int("".join([str(x) for x in bitlist[:first_chunk_size]]), 2)
y = int("".join([str(x) for x in bitlist[first_chunk_size:]]), 2)
z = to_int(value[1:])
return string_format.format(x, y, z)

def from_knx(address:str) -> bytes:
"""Converts a KNX individual address (X.Y.Z) or group address
(X/Y/Z) to a byte array (2 bytes, X Y being one byte, and Z the
other).
:param address: KNX address as a string with format X.Y.Z or
X/Y/Z.
"""
addr = match("(\d{1,2})\.(\d{1,2})\.(\d{1,3})", address)
first_chunk_size = 4 # individual address
if not addr:
addr = match("(\d{1,2})/(\d{1,2})/(\d{1,3})", address)
first_chunk_size = 5 # group address
if not addr:
return None
x, y, z = (int(addr.group(a+1)) for a in range(3))
x = int_to_bit_list(x)[8-first_chunk_size:]
y = int_to_bit_list(y)[first_chunk_size:]
b1 = from_int(bit_list_to_int(x + y))
b2 = from_int(z)
return b''.join([b1, b2])

def int_to_bit_list(n:int, size:int=8, byteorder:str=None) -> list:
"""Representation of n as a list of bits (0 or 1).
Expand Down
Loading

0 comments on commit 5eed4ec

Please sign in to comment.