Skip to content

Commit

Permalink
feat: com aux able to receive timestamps (#466)
Browse files Browse the repository at this point in the history
* feat: com aux able to receive timestamps

---------

Co-authored-by: Sebastian Clerson <58192998+sebclrsn@users.noreply.github.com>
  • Loading branch information
TedRio and sebclrsn committed Mar 28, 2024
1 parent baa9320 commit fb6c362
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/whats_new/version_ongoing.rst
@@ -1,6 +1,12 @@
Version ongoing
---------------

Communication Auxiliary
^^^^^^^^^^^^^^^^^^^^^^^

Receive method of the Communication Auxiliary can now also return the message timestamp
if the corresponding parameter is set to True.

PCAN Connector
^^^^^^^^^^^^^^

Expand Down
11 changes: 8 additions & 3 deletions src/pykiso/lib/auxiliaries/communication_auxiliary.py
Expand Up @@ -27,7 +27,7 @@
import queue
import threading
from contextlib import ContextDecorator
from typing import Any, Optional
from typing import Any, Optional, Tuple

from pykiso import CChannel, Message
from pykiso.auxiliary import AuxiliaryInterface, close_connector, open_connector
Expand Down Expand Up @@ -136,11 +136,13 @@ def receive_message(
self,
blocking: bool = True,
timeout_in_s: float = None,
) -> Optional[bytes]:
receive_timestamp: bool = False,
) -> Optional[bytes | Tuple[bytes, int] | Tuple[bytes, int, float]]:
"""Receive a raw message.
:param blocking: wait for message till timeout elapses?
:param timeout_in_s: maximum time in second to wait for a response
:param receive_timestamp: True if timestamp should be returned, False otherwise
:returns: raw message
"""
Expand All @@ -167,9 +169,12 @@ def receive_message(

msg = response.get("msg")
remote_id = response.get("remote_id")
timestamp = response.get("timestamp")

# stay with the old return type to not making a breaking change
if remote_id is not None:
if receive_timestamp:
return (msg, remote_id, timestamp)
elif remote_id and not receive_timestamp:
return (msg, remote_id)
return msg

Expand Down
40 changes: 33 additions & 7 deletions tests/test_com_aux.py
Expand Up @@ -12,10 +12,7 @@
import pytest

from pykiso import Message
from pykiso.lib.auxiliaries.communication_auxiliary import (
CommunicationAuxiliary,
queue,
)
from pykiso.lib.auxiliaries.communication_auxiliary import CommunicationAuxiliary, queue
from pykiso.test_setup.dynamic_loader import DynamicImportLinker


Expand Down Expand Up @@ -97,17 +94,46 @@ def test_com_aux_receive_messaging_without_contextmanager(caplog, com_aux_inst,
com_aux_inst.delete_instance()


def test_com_aux_receive_with_id_messaging_without_contextmanager(caplog, com_aux_inst, mocker):
msg = {"remote_id": 1, "msg": "test"}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)

com_aux_inst.create_instance()

payload, remote_id = com_aux_inst.receive_message()

assert payload == msg.get("msg")
assert remote_id== msg.get("remote_id")
com_aux_inst.delete_instance()


def test_com_aux_receive_with_timestamp_messaging_without_contextmanager(caplog, com_aux_inst, mocker):
msg = {"timestamp": 1005, "msg": "test"}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)

com_aux_inst.create_instance()

payload, remote_id, timestamp = com_aux_inst.receive_message(receive_timestamp=True)

assert payload == msg["msg"]
assert timestamp == msg["timestamp"]
assert remote_id == None
com_aux_inst.delete_instance()


def test_com_aux_receive_full_messaging_with_contextmanager(
caplog, com_aux_inst, mocker
):
msg = {"id": 1, "msg": "test"}
msg = {"remote_id": 1, "msg": "test", "timestamp": 52265}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)
com_aux_inst.create_instance()

with com_aux_inst.collect_messages():
ret = com_aux_inst.receive_message()
payload, remote_id, timestamp = com_aux_inst.receive_message(receive_timestamp=True)

assert ret == msg["msg"]
assert payload == msg["msg"]
assert remote_id == msg["remote_id"]
assert timestamp == msg["timestamp"]
com_aux_inst.delete_instance()


Expand Down

0 comments on commit fb6c362

Please sign in to comment.