From 2d93601d2940599324b75881eb8cd14deb74456b Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Mon, 11 Nov 2024 23:29:26 -0600 Subject: [PATCH] app/dynamic_node_id: avoid spurious reallocation Don't set a None unique ID in the DNA database for a node ID if the node goes offline or if its unique ID is not available yet. This prevents the node being allocated another ID while still allowing the node ID to be reserved if a unique ID will never be available. --- dronecan/app/dynamic_node_id.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dronecan/app/dynamic_node_id.py b/dronecan/app/dynamic_node_id.py index dc535c6..12ce66c 100644 --- a/dronecan/app/dynamic_node_id.py +++ b/dronecan/app/dynamic_node_id.py @@ -116,8 +116,16 @@ def get_allocation_table(self): return self._allocation_table.get_entries() def _handle_monitor_event(self, event): + if event.event_id not in (event.EVENT_ID_NEW, event.EVENT_ID_INFO_UPDATE): + return # don't care about nodes going offline or other such things + # unique ID might not be available if we see a node not participating in + # DNA and haven't got it or it didn't share that unique_id = event.entry.info.hardware_version.unique_id.to_bytes() if event.entry.info else None - self._allocation_table.set(unique_id, event.entry.node_id) + # set unique ID for this node ID (possibly to None in case we never get + # one) if we don't have one yet (though maybe we should raise a + # conflict if we do) + if self._allocation_table.get_unique_id(event.entry.node_id) is None: + self._allocation_table.set(unique_id, event.entry.node_id) def close(self): """Stops the instance and closes the allocation table storage.