Skip to content

Commit 665dea3

Browse files
committed
use lastmod to speedup contact updates
1 parent 742c498 commit 665dea3

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/meshcore/commands.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,12 @@ async def set_devicepin(self, pin: int) -> Event:
253253
return await self.send(b"\x25" \
254254
+ int(pin).to_bytes(4, 'little'), [EventType.OK, EventType.ERROR])
255255

256-
async def get_contacts(self) -> Event:
256+
async def get_contacts(self, lastmod=0) -> Event:
257257
logger.debug("Getting contacts")
258-
return await self.send(b"\x04", [EventType.CONTACTS, EventType.ERROR])
258+
data=b"\x04"
259+
if lastmod > 0:
260+
data = data + lastmod.to_bytes(4, 'little')
261+
return await self.send(data, [EventType.CONTACTS, EventType.ERROR])
259262

260263
async def reset_path(self, key: DestinationType) -> Event:
261264
key_bytes = _validate_destination(key, prefix_length=32)

src/meshcore/meshcore.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(self, cx, debug=False, default_timeout=None, auto_reconnect=False,
4747
self._pending_contacts = {}
4848
self._self_info = {}
4949
self._time = 0
50+
self._lastmod = 0
5051

5152
# Set up event subscriptions to track data
5253
self._setup_data_tracking()
@@ -179,7 +180,9 @@ async def wait_for_event(self, event_type: EventType, attribute_filters: Optiona
179180
def _setup_data_tracking(self):
180181
"""Set up event subscriptions to track data internally"""
181182
async def _update_contacts(event):
182-
self._contacts = event.payload
183+
self._contacts.update(event.payload)
184+
if "lastmod" in event.attributes :
185+
self._lastmod = event.attributes['lastmod']
183186
self._contacts_dirty = False
184187

185188
async def _add_pending_contact(event):
@@ -353,6 +356,6 @@ async def stop_auto_message_fetching(self):
353356
async def ensure_contacts(self, follow=False):
354357
"""Ensure contacts are fetched"""
355358
if not self._contacts or (follow and self._contacts_dirty) :
356-
await self.commands.get_contacts()
359+
await self.commands.get_contacts(lastmod = self._lastmod)
357360
return True
358361
return False

src/meshcore/reader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ async def handle_rx(self, data: bytearray):
6868
self.contacts[c["public_key"]] = c
6969

7070
elif packet_type_value == PacketType.CONTACT_END.value:
71-
await self.dispatcher.dispatch(Event(EventType.CONTACTS, self.contacts))
71+
lastmod = int.from_bytes(data[1:5], byteorder='little')
72+
attributes = {
73+
"lastmod": lastmod,
74+
}
75+
await self.dispatcher.dispatch(Event(EventType.CONTACTS, self.contacts, attributes))
7276

7377
elif packet_type_value == PacketType.SELF_INFO.value:
7478
self_info = {}

0 commit comments

Comments
 (0)