Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
mdabrowski1990 committed Dec 19, 2024
1 parent 8445745 commit f919811
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
26 changes: 12 additions & 14 deletions uds/database/data_record/abstract_data_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
int, # physical value is the same as raw value
float, # physical value calculated through formula
str, # decoded text value
Tuple[Tuple["DecodedDataRecord", ...], ...] # decoded container value, each element is another record
Tuple[Tuple["DecodedDataRecord", ...], ...] # decoded container value, each element is another entry
]
"""
Alias of Data Records' physical value.
Expand All @@ -59,7 +59,12 @@ class DecodedDataRecord(TypedDict):


class AbstractDataRecord(ABC):
"""Common implementation and interface for all Data Records."""
"""
Common implementation and interface for all Data Records.
Data Records are fragments of diagnostic messages.
Each Data Record defines how to interpret a single element/signal.
"""

def __init__(self, name: str) -> None:
"""
Expand Down Expand Up @@ -92,35 +97,28 @@ def max_raw_value(self):
"""
return (1 << self.length) - 1

@property # noqa: F841
@abstractmethod
def is_reoccurring(self) -> bool:
"""
Whether this Data Record might occur multiple times.
Values meaning:
- False - exactly one occurrence in every diagnostic message
- True - number of occurrences might vary
- True - number of occurrences might vary
- False - constant number of occurrences in every diagnostic message
"""
return self.min_occurrences != self.max_occurrences

@property # noqa: F841
@abstractmethod
def min_occurrences(self) -> int:
"""
Minimal number of this Data Record occurrences.
.. note:: Relevant only if :attr:`~uds.database.abstract_data_record.AbstractDataRecord.is_reoccurring`
equals True.
"""
"""Minimal number of this Data Record occurrences."""

@property # noqa: F841
@abstractmethod
def max_occurrences(self) -> Optional[int]:
"""
Maximal number of this Data Record occurrences.
.. note:: Relevant only if :attr:`~uds.database.abstract_data_record.AbstractDataRecord.is_reoccurring`
equals True.
.. warning:: No maximal number (infinite number of occurrences) is represented by None value.
"""

Expand All @@ -140,7 +138,7 @@ def decode(self, raw_value: int) -> DecodedDataRecord: # noqa: F841
"""

@abstractmethod
def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int: # noqa: F841
def encode(self, physical_value: DataRecordValueAlias) -> int: # noqa: F841
"""
Encode raw value for provided physical value.
Expand Down
28 changes: 25 additions & 3 deletions uds/database/data_record/container_data_record.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__all__ = ["ContainerDataRecord"]

from typing import Sequence, Optional, Tuple
from .abstract_data_record import AbstractDataRecord, DecodedDataRecord
from typing import Sequence, Optional, Tuple, Dict
from .abstract_data_record import AbstractDataRecord, DecodedDataRecord, DataRecordValueAlias
from uds.utilities import InconsistentArgumentsError


Expand Down Expand Up @@ -104,4 +104,26 @@ def decode(self, raw_value: int) -> DecodedDataRecord:
:return: Dictionary with physical value for this Data Record.
"""
# TODO
# TODO

def encode(self, physical_value: DataRecordValueAlias) -> int: # noqa: F841
"""
Encode raw value for provided physical value.
:param physical_value: Physical (meaningful e.g. float, str type) value of this Data Record.
:return: Raw Value of this Data Record.
"""
if not isinstance(physical_value, Sequence):
raise TypeError
combined_raw_value = 0
for single_record_value in physical_value:
if isinstance(single_record_value, int):
if 0 <= single_record_value <= self.max_raw_value:
entry_raw_value = single_record_value
elif isinstance(single_record_value, dict):
entry_raw_value = ... # TODO
else:
raise ValueError
combined_raw_value = (combined_raw_value << self.length) + entry_raw_value
return combined_raw_value
18 changes: 4 additions & 14 deletions uds/database/data_record/raw_data_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Optional, Tuple

from .abstract_data_record import AbstractDataRecord, DataRecordPhysicalValueAlias, DecodedDataRecord
from .abstract_data_record import AbstractDataRecord, DataRecordValueAlias, DecodedDataRecord


class RawDataRecord(AbstractDataRecord):
Expand Down Expand Up @@ -44,17 +44,6 @@ def length(self, value: int) -> None:
raise ValueError("Length must be a positive integer.")
self.__length = value

@property # noqa: F841
def is_reoccurring(self) -> bool:
"""
Whether this Data Record might occur multiple times.
Values meaning:
- False - exactly one occurrence in every diagnostic message
- True - number of occurrences might vary
"""
return False

@property # noqa: F841
def min_occurrences(self) -> int:
"""
Expand Down Expand Up @@ -102,11 +91,12 @@ def decode(self, raw_value: int) -> DecodedDataRecord:
)
return DecodedDataRecord(name=self.name, raw_value=raw_value, physical_value=raw_value)

def encode(self, physical_value: DataRecordPhysicalValueAlias) -> int:
def encode(self, physical_value: DataRecordValueAlias) -> int:
"""
Encode raw value for provided physical value.
:param physical_value: Physical (meaningful e.g. float, str type) value of this Data Record.
:param physical_value: Physical value of this Data Record.
For this Data Record type, it is the same as raw value.
:return: Raw Value of this Data Record.
"""
Expand Down

0 comments on commit f919811

Please sign in to comment.