import meshtastic
from pubsub import pub
import json
import time
def update(interface, packet):
print("data Received")
interface.showNodes()
if __name__ == '__main__':
pub.subscribe(update, "meshtastic.receive")
interface = meshtastic.SerialInterface()
while True:
time.sleep(1)
In this simple example I suppose that each time a new message arrives the nodes table is printed to the user screen. It seems to be ok but data in the table is update only at the first run. Next times the table is printed the data is always the same until the script is restarted and so updated again, what am I missing?
I’ve found that the node list was updated but only for nodeInfo and not for lastHeard Snr and hoplimit.
I don’t know if is the correct solution but modifying the function _onNodeInfoReceive it seems to update
ORIGINAL
def _onNodeInfoReceive(iface, asDict):
"""Special auto parsing for received messages"""
print("asdict:", asDict)
p = asDict["decoded"]["user"]
# decode user protobufs and update nodedb, provide decoded version as "position" in the published msg
# update node DB as needed
n = iface._getOrCreateByNum(asDict["from"])
print("update Db on info")
n["user"] = p
# We now have a node ID, make sure it is uptodate in that table
iface.nodes[p["id"]] = n
MODIFIED
def _onNodeInfoReceive(iface, asDict):
"""Special auto parsing for received messages"""
print("asdict:", asDict)
p = asDict["decoded"]["user"]
# decode user protobufs and update nodedb, provide decoded version as "position" in the published msg
# update node DB as needed
n = iface._getOrCreateByNum(asDict["from"])
print("update Db on info")
n["user"] = p
n["lastHeard"] = asDict["rxTime"]
n["snr"] = asDict["rxSnr"]
n["hopLimit"] = asDict["hopLimit"]
# We now have a node ID, make sure it is uptodate in that table
iface.nodes[p["id"]] = n
Please tell me if is ok or you prefer a different solution
Hmm. Probably cleaner and more generally useful to instead just store the most recently received packet in the python nodedb. So how about this instead:
n["lastRecieved’]= asDict
This would allow you (and others) to get all that you want, and in the future when packets have more context it will have that as well.
I’ve done the modification, it seems to work but I have one question:
In this way we’ll have a lastreceived key with the last received message but the nodeDB is not updated consistently with data received. I mean, The SNR, LastHeard and hoplimit will not be updated in the nodeDB when new packet is received. Maybe this is correct and as intended when developed.
IMHO it makes some confusion because when the device receive a packet an it contains updated information I expect that the nodeDB entry is updated with these information. So why update only “user” and “position” and not lastHeard, SNR and hoplimit?
update:
The rationale of the text above is:
if the node receives a packet it’ll have an rxTime and so this is the last heard
if the node receives a packet it’ll have an rxSnr and so this is the last snr value
it would be great to have the nodeDb with all values updated
I haven’t had a chance to look at your PR, but I know @geeksville is taking things easy for a little. I haven’t done too much with python or I’d love to review your work. Thanks for contributing!