diff --git a/src/meshcore/commands.py b/src/meshcore/commands.py index 862e7bd..7213940 100644 --- a/src/meshcore/commands.py +++ b/src/meshcore/commands.py @@ -294,10 +294,16 @@ class CommandHandler: else : out_path_hex = path out_path_len = int(len(path) / 2) + # reflect the change + contact["out_path"] = out_path_hex + contact["out_path_len"] = out_path_len out_path_hex = out_path_hex + (128-len(out_path_hex)) * "0" if flags is None : flags = contact["flags"] + else : + # reflect the change + contact["flags"] = flags adv_name_hex = contact["adv_name"].encode().hex() adv_name_hex = adv_name_hex + (64-len(adv_name_hex)) * "0" diff --git a/src/meshcore/meshcore.py b/src/meshcore/meshcore.py index e7e88d6..1178ea3 100644 --- a/src/meshcore/meshcore.py +++ b/src/meshcore/meshcore.py @@ -43,6 +43,8 @@ class MeshCore: # Initialize state (private) self._contacts = {} + self._contacts_ok = False + self._pending_contacts = {} self._self_info = {} self._time = 0 @@ -178,6 +180,14 @@ class MeshCore: """Set up event subscriptions to track data internally""" async def _update_contacts(event): self._contacts = event.payload + self._contacts_ok = True + + async def _add_pending_contact(event): + c = event.payload + self._pending_contacts[c["public_key"]] = c + + async def _contact_change(event): + self._contacts_ok = False async def _update_self_info(event): self._self_info = event.payload @@ -187,14 +197,27 @@ class MeshCore: # Subscribe to events to update internal state self.subscribe(EventType.CONTACTS, _update_contacts) + self.subscribe(EventType.NEW_CONTACT, _add_pending_contact) self.subscribe(EventType.SELF_INFO, _update_self_info) self.subscribe(EventType.CURRENT_TIME, _update_time) + self.subscribe(EventType.ADVERTISEMENT, _contact_change) + self.subscribe(EventType.PATH_UPDATE, _contact_change) # Getter methods for state @property def contacts(self): """Get the current contacts""" return self._contacts + + @property + def contacts_ok(self): + """Get wether contact list is in sync""" + return self._contacts_ok + + @property + def pending_contacts(self): + """Get pending contacts""" + return self._pending_contacts @property def self_info(self): @@ -323,7 +346,7 @@ class MeshCore: async def ensure_contacts(self): """Ensure contacts are fetched""" - if not self._contacts: + if not self._contacts or not self._contacts_ok : await self.commands.get_contacts() return True return False diff --git a/src/meshcore/reader.py b/src/meshcore/reader.py index c9d705a..b561d99 100644 --- a/src/meshcore/reader.py +++ b/src/meshcore/reader.py @@ -65,7 +65,6 @@ class MessageReader: elif packet_type_value == PacketType.CONTACT_END.value: await self.dispatcher.dispatch(Event(EventType.CONTACTS, self.contacts)) - elif packet_type_value == PacketType.SELF_INFO.value: self_info = {} self_info["adv_type"] = data[1]