Skip to content

Commit

Permalink
handle request and repsonses
Browse files Browse the repository at this point in the history
  • Loading branch information
mdabrowski1990 committed Dec 6, 2024
1 parent 0f9642f commit 0eddd1c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion uds/database/abstract_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down
46 changes: 42 additions & 4 deletions uds/database/services/abstract_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."


Expand All @@ -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 <https://docs.python.org/3/tutorial/floatingpoint.html>`_.
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.
Expand All @@ -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
Expand Down

0 comments on commit 0eddd1c

Please sign in to comment.