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

app/dynamic_node_id: avoid spurious reallocation #71

Merged
merged 2 commits into from
Nov 13, 2024
Merged
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
13 changes: 10 additions & 3 deletions dronecan/app/dynamic_node_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class CentralizedServer(object):

class AllocationTable(object):
def __init__(self, path):
# Disabling same thread check on the assumption that the developer knows what they are doing.
self.db = sqlite3.connect(path, check_same_thread=False) # @UndefinedVariable
self.db = sqlite3.connect(path) # @UndefinedVariable

self._modify('''CREATE TABLE IF NOT EXISTS `allocation` (
`node_id` INTEGER NOT NULL UNIQUE,
Expand Down Expand Up @@ -117,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.
Expand Down
Loading