Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: add framer msg type filter #1321

Merged
merged 5 commits into from
Apr 25, 2023
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
2 changes: 1 addition & 1 deletion haskell/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM debian:stretch-slim
FROM debian:buster-slim

RUN \
apt-get update && \
Expand Down
Binary file added python/data/filter.bin
Binary file not shown.
25 changes: 18 additions & 7 deletions python/sbp/client/framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ class Framer(six.Iterator):
Stream of bytes to read from.
write : port
Stream of bytes to write to.
verbose: boolean
verbose : boolean
Increased verbosity of stdout / stderr. Default False
dispatcher: function
dispatcher : function
function to call on each deframed message
skip_medata: boolean
skip_medata : boolean
do not add "time" and "session-uuid" metadata
sender_id_filter: list
list of integer sender_ids to frame, with the exclusion of all others
sender_id_filter_list : list
list of integer sender_ids to handle, at the exclusion of all others. If the list is empty, will handle all senders.
message_type_filter : list
list of integer message types to handle, at the exclusion of all others. If the list is empty, will handle all senders.
"""

def __init__(self,
Expand All @@ -48,7 +50,8 @@ def __init__(self,
dispatcher=dispatch,
into_buffer=True,
skip_metadata=False,
sender_id_filter_list=[]):
sender_id_filter_list=[],
message_type_filter=[]):
self._read = read
self._write = write
self._verbose = verbose
Expand All @@ -59,6 +62,13 @@ def __init__(self,
self._into_buffer = into_buffer
self._skip_metadata = skip_metadata
self._sender_id_filter_list = sender_id_filter_list
self._message_type_filter = message_type_filter

def __enter__(self):
return self

def __exit__(self, *args):
self.breakiter()

def __iter__(self):
self._broken = False
Expand Down Expand Up @@ -142,7 +152,8 @@ def _receive(self):
if self._verbose:
print("crc mismatch: 0x%04X 0x%04X" % (msg_crc, crc))
return None
if (len(self._sender_id_filter_list) == 0 or sender in self._sender_id_filter_list):
if ((len(self._sender_id_filter_list) == 0 or sender in self._sender_id_filter_list) and
(len(self._message_type_filter) == 0 or msg_type in self._message_type_filter)):
msg = SBP(msg_type, sender, msg_len, data, crc)
try:
msg = self._dispatch(msg)
Expand Down
1 change: 0 additions & 1 deletion python/sbp/client/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def gen_messages():
for msg, metadata in self._source:
if msg.msg_type:
yield (msg, metadata)

messages = gen_messages()
while True:
msg_and_metadata = None
Expand Down
12 changes: 12 additions & 0 deletions python/tests/sbp/client/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import gc

from sbp.client import Framer, Handler
from sbp.client.drivers.file_driver import FileDriver
from sbp.msg import SBP
from sbp.logging import MsgLog
from sbp.navigation import SBP_MSG_VEL_NED_GNSS

class CallbackCounter(object):
"""
Expand Down Expand Up @@ -56,6 +58,16 @@ def test_framer_ok():
assert msg.length == 13
assert msg.crc == 0x4feb

def test_framer_filter():
with open("./data/filter.bin", "rb") as fp:
with FileDriver(fp) as driver:
with Framer(driver.read, driver.write, message_type_filter=[SBP_MSG_VEL_NED_GNSS]) as framer:
assert sum(1 for _ in framer) == 160
with open("./data/filter.bin", "rb") as fp:
with FileDriver(fp) as driver:
with Framer(driver.read, driver.write) as framer:
assert sum(1 for _ in framer) == 7056

def until(p, limit=1000):
for i in itertools.count():
if p():
Expand Down
1 change: 1 addition & 0 deletions python/tests/sbp/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_message_case_count():
missing_messages == EXPECTED_MISSING_MESSAGES
), "Missing tests for %d out of %d messages" % (missing_messages, total_messages)


@pytest.mark.parametrize("case", CASES)
def test_message(case):
assert_package(ROOTPATH + "/" + case)