Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 89 additions & 7 deletions luxtronik/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from luxtronik.datatypes import Base
from luxtronik.definitions import LuxtronikDefinition, LuxtronikDefinitionsDictionary
from luxtronik.version import VER_IGNORE


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -138,6 +139,93 @@ def integrate_data(definition, field, raw_data, num_bits, data_offset=-1):
# Definition / field pair
###############################################################################

def get_and_check_def(def_name_or_idx, definitions, version=VER_IGNORE):
"""
Retrieve a definition by name or index that is supported by the controller version.

Args:
def_name_or_idx (LuxtronikDefinition | str | int):
Name, register index or the definition of the field.
definitions (LuxtronikDefinitionsDictionary | LuxtronikDefinitionsList):
A dictionary or list where the definition can be looked up (via `get()`)
by a name or a index
version (tuple[int]): Version of the controller.
The version check is not performed if this parameter is `VER_IGNORE`.

Returns:
LuxtronikDefinition | None: The matching definition, or None if not found.
"""
if isinstance(def_name_or_idx, LuxtronikDefinition):
definition = def_name_or_idx
else:
name_or_idx = def_name_or_idx
definition = definitions.get(name_or_idx)
if definition is None:
return None
if not definition.version.matches(version):
LOGGER.debug(f"Field {definition.name} not valid for {version}")
return None
return definition

def get_and_check_def_field_pair(def_field_name_or_idx, definitions, version=VER_IGNORE):
"""
Look-up a definition by name, index, a field instance or by the definition itself
that is supported by the controller version.

If `def_field_name_or_idx`
- is a definition -> lookup the definition by the definition's name
- is a field -> lookup the definition by the field's name
- is a name -> lookup the field by the name
- is a idx -> lookup the field by the index

Args:
def_field_name_or_idx (LuxtronikDefinition | Base | str | int):
Definition object, field object, field name or register index.
definitions (LuxtronikDefinitionsDictionary | LuxtronikDefinitionsList):
A dictionary or list where the definition can be looked up (via `get()`)
by a name or a index
version (tuple[int]): Version of the controller.
The version check is not performed if this parameter is `VER_IGNORE`.

Returns:
tuple[LuxtronikDefinition | None, Base | None]:
A definition-field-pair tuple:
Index 0: Return the found or given definitions, otherwise None
Index 1: Return the given field, otherwise None
"""
if isinstance(def_field_name_or_idx, Base):
# In case we got a field, search for the definition by the field name
field = def_field_name_or_idx
definition = get_and_check_def(field.name, definitions, version)
else:
def_name_or_idx = def_field_name_or_idx
definition = get_and_check_def(def_name_or_idx, definitions, version)
field = None
return definition, field

def get_check_and_create_def_field_pair(def_field_name_or_idx, definitions, version=VER_IGNORE):
"""
Retrieve a definition by definition, field, name or index that is supported by the controller.
On success, additionally returns either the passed field or a newly created field.

Args:
def_field_name_or_idx (LuxtronikDefinition | Base | str | int):
Name, register index or the definition of the field, or the field itself.
definitions (LuxtronikDefinitionsDictionary | LuxtronikDefinitionsList):
A dictionary or list where the definition can be looked up (via `get()`)
by a name or a index
version (tuple[int]): Version of the controller.
The version check is not performed if this parameter is `VER_IGNORE`.

Returns:
tuple[LuxtronikDefinition, Base]: The matching definition-field pair.
"""
definition, field = get_and_check_def_field_pair(def_field_name_or_idx, definitions, version)
if definition is not None and field is None and definition.valid:
field = definition.create_field()
return definition, field


class LuxtronikDefFieldPair:
"""
Combines a definition and a field into a single iterable object.
Expand Down Expand Up @@ -337,13 +425,7 @@ def get(self, def_field_name_or_idx, default=None):
If multiple fields added for the same index/name,
the last added takes precedence.
"""
def_name_or_idx = def_field_name_or_idx
if isinstance(def_name_or_idx, Base):
def_name_or_idx = def_name_or_idx.name
if isinstance(def_name_or_idx, LuxtronikDefinition):
definition = def_name_or_idx
else:
definition = self._def_lookup.get(def_name_or_idx)
definition, _ = get_and_check_def_field_pair(def_field_name_or_idx, self._def_lookup, VER_IGNORE)
if definition is not None:
return self._field_lookup.get(definition, default)
return default
61 changes: 10 additions & 51 deletions luxtronik/data_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import logging

from luxtronik.constants import LUXTRONIK_VERSION_IGNORE
from luxtronik.collections import LuxtronikFieldsDictionary
from luxtronik.collections import (
get_and_check_def_field_pair,
get_check_and_create_def_field_pair,
LuxtronikFieldsDictionary
)
from luxtronik.datatypes import Base, Unknown
from luxtronik.definitions import LuxtronikDefinition
from luxtronik.version import resolve_version_input
from luxtronik.version import VER_IGNORE, resolve_version_input


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -68,14 +71,8 @@ def create_any_field(cls, def_name_or_idx):
Returns:
Base | None: The created field, or None if not found or not valid.
"""
if isinstance(def_name_or_idx, LuxtronikDefinition):
definition = def_name_or_idx
else:
# The definitions object hold all available definitions
definition = cls.definitions.get(def_name_or_idx)
if definition is not None and definition.valid:
return definition.create_field()
return None
_, field = get_check_and_create_def_field_pair(def_name_or_idx, cls.definitions, VER_IGNORE)
return field

def create_field(self, def_name_or_idx):
"""
Expand All @@ -94,7 +91,7 @@ class variable `cls.definitions` and is valid for `self.version`).
Returns:
Base | None: The created field, or None if not found or not valid.
"""
definition, _ = self._get_definition(def_name_or_idx, False)
definition, _ = get_and_check_def_field_pair(def_name_or_idx, self._data.def_dict, VER_IGNORE)
if definition is not None and definition.valid:
return definition.create_field()
return None
Expand Down Expand Up @@ -241,7 +238,7 @@ def add(self, def_field_name_or_idx):
If multiple fields added for the same index/name, the last added takes precedence.
"""
# Look-up the related definition
definition, field = self._get_definition(def_field_name_or_idx, True)
definition, field = get_and_check_def_field_pair(def_field_name_or_idx, self.definitions, VER_IGNORE)
if definition is None:
return None

Expand All @@ -262,44 +259,6 @@ def add(self, def_field_name_or_idx):

# Get and set methods #########################################################

def _get_definition(self, def_field_name_or_idx, all_not_version_dependent):
"""
Look-up a definition by name, index, a field instance or by the definition itself.

If `def_field_name_or_idx`
- is a definition -> lookup the definition by the definition's name
- is a field -> lookup the definition by the field's name
- is a name -> lookup the field by the name
- is a idx -> lookup the field by the index

Args:
def_field_name_or_idx (LuxtronikDefinition | Base | str | int):
Definition object, field object, field name or register index.
all_not_version_dependent (bool): If true, look up the definition
within the `cls.definitions` otherwise within `self._data` (which
contain all definitions related to all added fields)

Returns:
tuple[LuxtronikDefinition | None, Base | None]:
A definition-field-pair tuple:
Index 0: Return the found or given definitions, otherwise None
Index 1: Return the given field, otherwise None
"""
definition = def_field_name_or_idx
field = None
if isinstance(def_field_name_or_idx, Base):
# In case we got a field, search for the description by the field name
definition = def_field_name_or_idx.name
field = def_field_name_or_idx
if not isinstance(def_field_name_or_idx, LuxtronikDefinition):
if all_not_version_dependent:
# definitions contains all available definitions
definition = self.definitions.get(definition)
else:
# _data.def_dict contains only valid and previously added definitions
definition = self._data.def_dict.get(definition)
return definition, field

def get(self, def_field_name_or_idx, default=None):
"""
Retrieve an added field by definition, field, name or register index.
Expand Down
35 changes: 29 additions & 6 deletions luxtronik/definitions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ def check_raw_not_none(self, raw):
return raw != LUXTRONIK_32BIT_FUNCTION_NOT_AVAILABLE
return True

###############################################################################
# Helper methods
###############################################################################

def resolve_name_or_idx_input(name_or_idx):
"""
Safeguards to ensure that only integers, strings and integers
as string are valid as an input to look-up a definition.

Args:
name_or_idx (str | int): Definition name or register index.

Returns:
int | str: The input as integer or string. Returns an empty
string, if the input is not valid
"""
if isinstance(name_or_idx, int):
return name_or_idx
if isinstance(name_or_idx, str):
try:
return int(name_or_idx)
except ValueError:
return name_or_idx
return '' # an empty string should not match any definition

###############################################################################
# LuxtronikDefinitionsDictionary
Expand Down Expand Up @@ -314,14 +338,13 @@ def _get(self, name_or_idx):
If multiple definitions added for the same index/name, the last added takes precedence.
"""
d = None
name_or_idx = resolve_name_or_idx_input(name_or_idx)
if isinstance(name_or_idx, int):
d = self._get_definition_by_idx(name_or_idx)
index = name_or_idx
d = self._get_definition_by_idx(index)
if isinstance(name_or_idx, str):
try:
# Numbers are not allowed as names, so it could be an index as string
d = self._get_definition_by_idx(int(name_or_idx))
except ValueError:
d = self._get_definition_by_name(name_or_idx)
name = name_or_idx
d = self._get_definition_by_name(name)
return d

def _get_definition_by_idx(self, idx):
Expand Down
61 changes: 9 additions & 52 deletions luxtronik/shi/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from luxtronik.common import classproperty
from luxtronik.constants import LUXTRONIK_VERSION_IGNORE
from luxtronik.collections import get_data_arr
from luxtronik.datatypes import Base
from luxtronik.collections import (
get_data_arr,
get_and_check_def,
get_check_and_create_def_field_pair
)
from luxtronik.definitions import (
LuxtronikDefinition,
LuxtronikDefinitionsList,
)
from luxtronik.interface import LuxtronikInterfaceInfo, LuxtronikBaseInterface
Expand Down Expand Up @@ -226,48 +228,6 @@ def read_version(self):

# Helper methods ##############################################################

def _get_definition(self, def_name_or_idx, definitions):
"""
Retrieve a definition by name or index that is supported by the controller.

Args:
def_name_or_idx (LuxtronikDefinition | str | int):
Name, register index or the definition of the field.

Returns:
LuxtronikDefinition | None: The matching definition, or None if not found.
"""
if isinstance(def_name_or_idx, LuxtronikDefinition):
definition = def_name_or_idx
else:
definition = definitions.get(def_name_or_idx)
if definition is None:
return None
if not definition.version.matches(self.__version):
LOGGER.debug(f"Field {definition.name} not valid for {self.__version}")
return None
return definition

def _get_def_field_pair(self, def_field_name_or_idx, definitions):
"""
Retrieve a definition by name or index that is supported by the controller.
On success, additionally returns either the passed field or a newly created field.

Args:
def_name_or_idx (LuxtronikDefinition | Base| str | int):
Name, register index or the definition of the field, or the field itself.

Returns:
tuple[LuxtronikDefinition, Base]: The matching definition-field pair.
"""
if isinstance(def_field_name_or_idx, Base):
definition = self._get_definition(def_field_name_or_idx.name, definitions)
field = def_field_name_or_idx if definition is not None else None
else:
definition = self._get_definition(def_field_name_or_idx, definitions)
field = definition.create_field() if definition is not None else None
return definition, field

def _get_index_from_name(self, name):
"""
Extract the index from an 'unknown' identifier (e.g. 'Unknown_Input_105').
Expand Down Expand Up @@ -298,10 +258,7 @@ def _try_create_definition(self, def_name_or_idx, definitions):
Returns:
LuxtronikDefinition | None: A valid definition, or None if not found.
"""
if isinstance(def_name_or_idx, LuxtronikDefinition):
definition = def_name_or_idx
else:
definition = definitions.get(def_name_or_idx)
definition = get_and_check_def(def_name_or_idx, definitions, VER_IGNORE)
if definition is not None:
return definition

Expand Down Expand Up @@ -517,7 +474,7 @@ def _collect_field(self, blocks_list, def_field_name_or_idx, definitions, \
if def_field_name_or_idx is None:
return None

definition, field = self._get_def_field_pair(def_field_name_or_idx, definitions)
definition, field = get_check_and_create_def_field_pair(def_field_name_or_idx, definitions, self.__version)

# create temporary definition for trial-and-error-mode
if self.__version is VER_IGNORE and definition is None and isinstance(def_field_name_or_idx, (int, str)):
Expand Down Expand Up @@ -833,7 +790,7 @@ def create_holding(self, def_name_or_idx):
Base | None: On success the created field, otherwise None.

"""
_, field = self._get_def_field_pair(def_name_or_idx, HOLDINGS_DEFINITIONS)
_, field = get_check_and_create_def_field_pair(def_name_or_idx, HOLDINGS_DEFINITIONS, self.__version)
return field

def create_holdings(self, safe=SAFE):
Expand Down Expand Up @@ -999,7 +956,7 @@ def create_input(self, def_name_or_idx):
Base | None: On success the created field, otherwise None.

"""
_, field = self._get_def_field_pair(def_name_or_idx, INPUTS_DEFINITIONS)
_, field = get_check_and_create_def_field_pair(def_name_or_idx, INPUTS_DEFINITIONS, self.__version)
return field

def create_inputs(self):
Expand Down
Loading
Loading