diff --git a/src/meshcore/commands.py b/src/meshcore/commands.py index d49e8ec..7e055bb 100644 --- a/src/meshcore/commands.py +++ b/src/meshcore/commands.py @@ -253,9 +253,12 @@ class CommandHandler: return await self.send(b"\x25" \ + int(pin).to_bytes(4, 'little'), [EventType.OK, EventType.ERROR]) - async def get_contacts(self) -> Event: + async def get_contacts(self, lastmod=0) -> Event: logger.debug("Getting contacts") - return await self.send(b"\x04", [EventType.CONTACTS, EventType.ERROR]) + data=b"\x04" + if lastmod > 0: + data = data + lastmod.to_bytes(4, 'little') + return await self.send(data, [EventType.CONTACTS, EventType.ERROR]) async def reset_path(self, key: DestinationType) -> Event: key_bytes = _validate_destination(key, prefix_length=32) diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index 108da2b..6a37829 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -47,6 +47,7 @@ class MeshCore: self._pending_contacts = {} self._self_info = {} self._time = 0 + self._lastmod = 0 # Set up event subscriptions to track data self._setup_data_tracking() @@ -179,7 +180,9 @@ class MeshCore: def _setup_data_tracking(self): """Set up event subscriptions to track data internally""" async def _update_contacts(event): - self._contacts = event.payload + self._contacts.update(event.payload) + if "lastmod" in event.attributes : + self._lastmod = event.attributes['lastmod'] self._contacts_dirty = False async def _add_pending_contact(event): @@ -353,6 +356,6 @@ class MeshCore: async def ensure_contacts(self, follow=False): """Ensure contacts are fetched""" if not self._contacts or (follow and self._contacts_dirty) : - await self.commands.get_contacts() + await self.commands.get_contacts(lastmod = self._lastmod) return True return False diff --git a/src/meshcore/reader.py b/src/meshcore/reader.py index 4b8a190..aefcce7 100644 --- a/src/meshcore/reader.py +++ b/src/meshcore/reader.py @@ -68,7 +68,11 @@ class MessageReader: self.contacts[c["public_key"]] = c elif packet_type_value == PacketType.CONTACT_END.value: - await self.dispatcher.dispatch(Event(EventType.CONTACTS, self.contacts)) + lastmod = int.from_bytes(data[1:5], byteorder='little') + attributes = { + "lastmod": lastmod, + } + await self.dispatcher.dispatch(Event(EventType.CONTACTS, self.contacts, attributes)) elif packet_type_value == PacketType.SELF_INFO.value: self_info = {}