From 19ac176d6c9d00f47c9ed7f99c84e58d8d3d9ec8 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 Only set a None unique ID in the DNA database if there's no existing unique ID for the node ID and the node's unique ID is not available. This prevents erasing an existing allocation and causing the node to be allocated another ID, while still allowing the node ID to be reserved if a unique ID never becomes 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.