From 5ddee95b63f98b7ac8c5821e81c7ef93bcb5b9d3 Mon Sep 17 00:00:00 2001 From: mate-dev <67105053+mate-dev@users.noreply.github.com> Date: Wed, 29 Nov 2023 02:21:58 +0000 Subject: [PATCH] Adding additional conditions for lastHeard, voltage, batteryLevel and changing the output format --- plugins/nodes_plugin.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/plugins/nodes_plugin.py b/plugins/nodes_plugin.py index df87d04..8fbae57 100644 --- a/plugins/nodes_plugin.py +++ b/plugins/nodes_plugin.py @@ -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">
\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):