From 93e9c1c66ca949e331c9e50d1cdf74d47b5b6fe0 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Thu, 30 May 2024 13:50:52 -0700 Subject: [PATCH] Initialize unknown nodes more in line with meshtastic/design#16 --- meshtastic/mesh_interface.py | 25 +++++++++++++++++++------ meshtastic/tests/test_init.py | 2 +- meshtastic/tests/test_mesh_interface.py | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index e6848502..c19d4f77 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -166,7 +166,8 @@ def getTimeAgo(ts) -> Optional[str]: if not includeSelf and node["num"] == self.localNode.nodeNum: continue - row = {"N": 0, "User": f"UNK: {node['num']}", "ID": f"!{node['num']:08x}"} + presumptive_id = f"!{node['num']:08x}" + row = {"N": 0, "User": f"Meshtastic {presumptive_id[-4:]}", "ID": presumptive_id} user = node.get("user") if user: @@ -175,6 +176,7 @@ def getTimeAgo(ts) -> Optional[str]: "User": user.get("longName", "N/A"), "AKA": user.get("shortName", "N/A"), "ID": user["id"], + "Hardware": user.get("hwModel", "UNSET") } ) @@ -844,16 +846,18 @@ def _handleFromRadio(self, fromRadioBytes): logging.debug(f"Received device metadata: {stripnl(fromRadio.metadata)}") elif fromRadio.HasField("node_info"): - node = asDict["nodeInfo"] + logging.debug(f"Received nodeinfo: {asDict['nodeInfo']}") + + node = self._getOrCreateByNum(asDict["nodeInfo"]["num"]) + node.update(asDict["nodeInfo"]) try: newpos = self._fixupPosition(node["position"]) node["position"] = newpos except: logging.debug("Node without position") - logging.debug(f"Received nodeinfo: {node}") - - self.nodesByNum[node["num"]] = node + # no longer necessary since we're mutating directly in nodesByNum via _getOrCreateByNum + #self.nodesByNum[node["num"]] = node if "user" in node: # Some nodes might not have user/ids assigned yet if "id" in node["user"]: self.nodes[node["user"]["id"]] = node @@ -1005,7 +1009,16 @@ def _getOrCreateByNum(self, nodeNum): if nodeNum in self.nodesByNum: return self.nodesByNum[nodeNum] else: - n = {"num": nodeNum} # Create a minimal node db entry + presumptive_id = f"!{nodeNum:08x}" + n = { + "num": nodeNum, + "user": { + "id": presumptive_id, + "longName": f"Meshtastic {presumptive_id[-4:]}", + "shortName": f"{presumptive_id[-4:]}", + "hwModel": "UNSET" + } + } # Create a minimal node db entry self.nodesByNum[nodeNum] = n return n diff --git a/meshtastic/tests/test_init.py b/meshtastic/tests/test_init.py index 0f1dc5b5..ae19c6e5 100644 --- a/meshtastic/tests/test_init.py +++ b/meshtastic/tests/test_init.py @@ -44,7 +44,7 @@ def test_init_onNodeInfoReceive(caplog, iface_with_nodes): iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 packet = { - "from": "foo", + "from": 4808675309, "decoded": { "user": { "id": "bar", diff --git a/meshtastic/tests/test_mesh_interface.py b/meshtastic/tests/test_mesh_interface.py index dd7d2385..037e0bfb 100644 --- a/meshtastic/tests/test_mesh_interface.py +++ b/meshtastic/tests/test_mesh_interface.py @@ -588,7 +588,7 @@ def test_getOrCreateByNum_minimal(iface_with_nodes): iface = iface_with_nodes iface.myInfo.my_node_num = 2475227164 tmp = iface._getOrCreateByNum(123) - assert tmp == {"num": 123} + assert tmp == {"num": 123, "user": {"hwModel": "UNSET", "id": "!0000007b", "shortName": "007b", "longName": "Meshtastic 007b"}} @pytest.mark.unit