Adding additional conditions for lastHeard, voltage, batteryLevel and changing the output format

feature/plugins-parameters
mate-dev 2023-11-29 02:21:58 +00:00
rodzic fb57b5701e
commit 5ddee95b63
1 zmienionych plików z 9 dodań i 21 usunięć

Wyświetl plik

@ -3,23 +3,15 @@ import statistics
from plugins.base_plugin import BasePlugin
from datetime import datetime
def get_relative_time(timestamp):
now = datetime.now()
dt = datetime.fromtimestamp(timestamp)
# Calculate the time difference between the current time and the given timestamp
delta = now - dt
# Extract the relevant components from the time difference
days = delta.days
seconds = delta.seconds
# Convert the time difference into a relative timeframe
if days > 7:
return dt.strftime(
"%b %d, %Y"
) # Return the timestamp in a specific format if it's older than 7 days
return dt.strftime("%b %d, %Y")
elif days >= 1:
return f"{days} days ago"
elif seconds >= 3600:
@ -31,7 +23,6 @@ def get_relative_time(timestamp):
else:
return "Just now"
class Plugin(BasePlugin):
plugin_name = "nodes"
@ -51,32 +42,29 @@ $shortname $longname / $devicemodel / $battery $voltage / $snr / $lastseen
for node, info in meshtastic_client.nodes.items():
snr = ""
if "snr" in info:
if info['snr'] is not None:
snr = f"{info['snr']} dB "
if "snr" in info and info['snr'] is not None:
snr = f"{info['snr']} dB "
last_heard = None
if "lastHeard" in info:
if "lastHeard" in info and info["lastHeard"] is not None:
last_heard = get_relative_time(info["lastHeard"])
voltage = ""
battery = ""
if "deviceMetrics" in info:
if "voltage" in info["deviceMetrics"]:
if "voltage" in info["deviceMetrics"] and info["deviceMetrics"]["voltage"] is not None:
voltage = f"{info['deviceMetrics']['voltage']}V "
if "batteryLevel" in info["deviceMetrics"]:
if "batteryLevel" in info["deviceMetrics"] and info["deviceMetrics"]["batteryLevel"] is not None:
battery = f"{info['deviceMetrics']['batteryLevel']}% "
response += f"><hr/>\n\n"\
f">**{info['user']['shortName']}** {info['user']['longName']}\n\n"\
f">{info['user']['hwModel']} {battery}{voltage}\n\n"\
f">**[{info['user']['shortName']} - {info['user']['longName']}]**\n"\
f">{info['user']['hwModel']} {battery}{voltage}\n"\
f">{snr}{last_heard}\n\n"
return response
async def handle_meshtastic_message(
self, packet, formatted_message, longname, meshnet_name
):
async def handle_meshtastic_message(self, packet, formatted_message, longname, meshnet_name):
return False
async def handle_room_message(self, room, event, full_message):