-
Notifications
You must be signed in to change notification settings - Fork 3
/
nodes.py
88 lines (78 loc) · 2.76 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from meshtastic.serial_interface import SerialInterface
from datetime import datetime
try:
from meshtastic.mesh_interface import _timeago
tg_underscore = True
except ImportError:
tg_underscore = False
import timeago
import json
client = SerialInterface()
def formatFloat(value, precision=2, unit=""):
"""Format a float value with precision."""
return f"{value:.{precision}f}{unit}" if value else None
def getLH(ts):
"""Format last heard"""
return (
datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") if ts else None
)
def getTimeAgo(ts):
"""Format how long ago have we heard from this node (aka timeago)."""
if tg_underscore and ts is not None:
return _timeago(int((datetime.now() - datetime.fromtimestamp(ts)).total_seconds()))
else:
return (
timeago.format(datetime.fromtimestamp(ts), datetime.now())
if ts
else None
)
if client.nodesByNum:
for node in client.nodesByNum.values():
row = {"User": f"UNK: {node['num']}", "ID": f"!{node['num']:x}"}
user = node.get("user")
if user:
row.update(
{
"User": user.get("longName", "N/A"),
"AKA": user.get("shortName", "N/A"),
"ID": user["id"],
}
)
pos = node.get("position")
if pos:
row.update(
{
"Latitude": formatFloat(pos.get("latitude"), 4, "°"),
"Longitude": formatFloat(pos.get("longitude"), 4, "°"),
"Altitude": formatFloat(pos.get("altitude"), 0, " m"),
}
)
metrics = node.get("deviceMetrics")
if metrics:
batteryLevel = metrics.get("batteryLevel")
if batteryLevel is not None:
if batteryLevel == 0:
batteryString = "Powered"
else:
batteryString = str(batteryLevel) + "%"
row.update({"Battery": batteryString})
row.update(
{
"Channel util.": formatFloat(
metrics.get("channelUtilization"), 2, "%"
),
"Tx air util.": formatFloat(
metrics.get("airUtilTx"), 2, "%"
),
}
)
row.update(
{
"SNR": formatFloat(node.get("snr"), 2, " dB"),
"Hops Away": node.get("hopsAway", "unknown"),
"Channel": node.get("channel"),
"LastHeard": getLH(node.get("lastHeard")),
"Since": getTimeAgo(node.get("lastHeard")),
}
)
print(json.dumps(row))