From 0eddd1ceed8f45e03a3bdf0da4ca507d18fad93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20D=C4=85browski?= Date: Fri, 6 Dec 2024 15:07:50 +0100 Subject: [PATCH] handle request and repsonses --- uds/database/abstract_database.py | 2 +- uds/database/services/abstract_service.py | 46 +++++++++++++++++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/uds/database/abstract_database.py b/uds/database/abstract_database.py index 10e24df..7d9ae0c 100644 --- a/uds/database/abstract_database.py +++ b/uds/database/abstract_database.py @@ -57,7 +57,7 @@ def encode(self, raise TypeError("Provided SID value is not int type.") if sid not in self.services: raise ValueError("Database has no encoding defined for provided SID value.") - return self.services[sid].encode(**data_records_values) + return self.services[sid].encode(sid=sid, **data_records_values) def decode(self, message: Union[UdsMessage, UdsMessageRecord]) -> List[DecodedDataRecord]: """ diff --git a/uds/database/services/abstract_service.py b/uds/database/services/abstract_service.py index c106ab3..f26536a 100644 --- a/uds/database/services/abstract_service.py +++ b/uds/database/services/abstract_service.py @@ -3,14 +3,20 @@ __all__ = ["AbstractService", "DataRecordValueAlias"] from abc import ABC, abstractmethod -from typing import Dict, Iterable, List, Union +from typing import Dict, Sequence, List, Union from uds.message import RequestSID, ResponseSID from uds.utilities import RawBytesAlias, RawBytesListAlias from ..data_record import DecodedDataRecord -DataRecordValueAlias = Union[int, float, str, Iterable[Dict[str, "DataRecordValueAlias"]]] +DataRecordValueAlias = Union[ + int, # raw value + float, # physical value calculated through formula + str, # text value decoded either through raw value mapping or text encoding + Dict[str, "DataRecordValueAlias"], # value of container's children + Sequence[Dict[str, "DataRecordValueAlias"]], # multiple records' values +] "Alias of input with Data Records values." @@ -37,8 +43,40 @@ def decode(self, payload: RawBytesAlias) -> List[DecodedDataRecord]: :return: Decoded Data Records values from provided diagnostic message. """ + def encode(self, sid: int, **data_records_values: DataRecordValueAlias) -> RawBytesListAlias: + # TODO + if sid == self.request_sid: + return self.encode_request(**data_records_values) + if sid == self.response_sid: + return self.encode_response(**data_records_values) + raise ValueError + + @abstractmethod + def encode_request(self, **data_records_values: DataRecordValueAlias) -> RawBytesListAlias: + """ + Encode diagnostic message payload from data records values. + + :param data_records_values: Value for each Data Record that is part a service message. + + Each type represent other data: + + - int type - raw value of a Data Record + - float type - physical value of a Data Record + - str type - text value of a Data Record + - sequence - contains values for children Data Records + - dict type - values of children Data Records + + .. warning:: Providing physical value as float might sometime cause issues due + `floating-point precision `_. + The closest raw value would be evaluated and put into a payload. + + To avoid rounding, provide raw value (int type). + + :return: Payload of a diagnostic message. + """ + @abstractmethod - def encode(self, **data_records_values: DataRecordValueAlias) -> RawBytesListAlias: # noqa: F841 + def encode_response(self, **data_records_values: DataRecordValueAlias) -> RawBytesListAlias: """ Encode diagnostic message payload from data records values. @@ -49,7 +87,7 @@ def encode(self, **data_records_values: DataRecordValueAlias) -> RawBytesListAli - int type - raw value of a Data Record - float type - physical value of a Data Record - str type - text value of a Data Record - - iterable type - contains values for children Data Records + - sequence - contains values for children Data Records - dict type - values of children Data Records .. warning:: Providing physical value as float might sometime cause issues due