Skip to content

Commit c975a6c

Browse files
authored
Integrate argument to ignore specific IDs (#102)
* Added argument to ignore IDs * Fixed type hint documentation
1 parent ec53ad5 commit c975a6c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pymavswarm/mavswarm.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def __init__(
5959
log_level: int = logging.INFO,
6060
log_to_file: bool = False,
6161
log_filename: str | None = None,
62+
ignore_ids: list[tuple[int | None, int | None]]
63+
| tuple[int | None, int | None]
64+
| None = None,
6265
) -> None:
6366
"""
6467
Construct MavSwarm interface.
@@ -73,13 +76,43 @@ def __init__(
7376
:type log_to_file: bool, optional
7477
:param log_filename: name of the file to log messages to, defaults to None
7578
:type log_filename: str | None, optional
79+
:param ignore_ids: agent IDs that should be ignored. This can be a list of agent
80+
IDs or a single agent ID. To specify a system ID that should be ignored, use
81+
(SYSTEM_ID, None). To specify a component ID that should be ignored, use
82+
(None, COMPONENT_ID). Note that if messages are being logged, messages from
83+
the blocked agents will still be logged, defaults to None
84+
:type ignore_ids: list[tuple[int | None, int | None]] |
85+
tuple[int | None, int | None] | None, optional
7686
"""
7787
super().__init__()
7888

7989
self._logger = init_logger(__name__, log_level=log_level)
8090
self.__agent_list_changed = Event()
8191
self._agents = NotifierDict(self.__agent_list_changed)
8292

93+
# Create a list of IDs to ignore
94+
self.__ignore_agent_ids: list[tuple[int | None, int | None]] = []
95+
self.__ignore_system_ids: list[int | None] = []
96+
self.__ignore_component_ids: list[int | None] = []
97+
98+
# Populate the list of IDs specified by the user
99+
if ignore_ids is not None:
100+
if isinstance(ignore_ids, list):
101+
for agent_id in ignore_ids:
102+
if agent_id[1] is None:
103+
self.__ignore_system_ids.append(agent_id[0])
104+
elif agent_id[0] is None:
105+
self.__ignore_component_ids.append(agent_id[1])
106+
else:
107+
self.__ignore_agent_ids.append(agent_id)
108+
else:
109+
if ignore_ids[1] is None:
110+
self.__ignore_system_ids.append(ignore_ids[0])
111+
elif ignore_ids[0] is None:
112+
self.__ignore_component_ids.append(ignore_ids[1])
113+
else:
114+
self.__ignore_agent_ids.append(ignore_ids)
115+
83116
# Register a listener to update the system time publish rate
84117
self.__agent_list_changed.add_listener(self.__request_system_time)
85118

@@ -2401,6 +2434,16 @@ def __receive_message(self) -> None:
24012434
message.to_dict(),
24022435
)
24032436

2437+
# Don't handle the message if it's from an ignored agent
2438+
agent_id = (message.get_srcSystem(), message.get_srcComponent())
2439+
2440+
if (
2441+
agent_id[0] in self.__ignore_system_ids
2442+
or agent_id[1] in self.__ignore_component_ids
2443+
or agent_id in self.__ignore_agent_ids
2444+
):
2445+
continue
2446+
24042447
# Execute the respective message handler(s)
24052448
if message.get_type() in self.__message_receivers.receivers:
24062449
for function in self.__message_receivers.receivers[message.get_type()]:

0 commit comments

Comments
 (0)