Skip to content

Commit

Permalink
Initialize unknown nodes more in line with meshtastic/design#16
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmcorvidae committed May 30, 2024
1 parent 8e641b3 commit 93e9c1c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions meshtastic/mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
}
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion meshtastic/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion meshtastic/tests/test_mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 93e9c1c

Please sign in to comment.