Skip to content

Commit

Permalink
Add nodes command (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoff Whittington <[email protected]>
  • Loading branch information
geoffwhittington and berticus2016 authored May 18, 2023
1 parent 1efa58d commit d8dd310
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def load_plugins():
from plugins.telemetry_plugin import Plugin as TelemetryPlugin
from plugins.weather_plugin import Plugin as WeatherPlugin
from plugins.help_plugin import Plugin as HelpPlugin
from plugins.nodes_plugin import Plugin as NodesPlugin

global plugins
if active_plugins:
Expand All @@ -26,6 +27,7 @@ def load_plugins():
TelemetryPlugin(),
WeatherPlugin(),
HelpPlugin(),
NodesPlugin(),
]

for plugin in plugins:
Expand Down
4 changes: 2 additions & 2 deletions plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self) -> None:
def get_matrix_commands(self):
return [self.plugin_name]

async def send_matrix_message(self, room_id, message):
async def send_matrix_message(self, room_id, message, formatted=True):
from matrix_utils import connect_matrix

matrix_client = await connect_matrix()
Expand All @@ -38,7 +38,7 @@ async def send_matrix_message(self, room_id, message):
message_type="m.room.message",
content={
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"format": "org.matrix.custom.html" if formatted else None,
"body": message,
"formatted_body": markdown.markdown(message),
},
Expand Down
77 changes: 77 additions & 0 deletions plugins/nodes_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import re
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
elif days >= 1:
return f"{days} days ago"
elif seconds >= 3600:
hours = seconds // 3600
return f"{hours} hours ago"
elif seconds >= 60:
minutes = seconds // 60
return f"{minutes} minutes ago"
else:
return "Just now"


class Plugin(BasePlugin):
plugin_name = "nodes"

@property
def description(self):
return """Show mesh radios and node data
$shortname $longname / $devicemodel / $battery $voltage / $snr / $lastseen
"""

def generate_response(self):
from meshtastic_utils import connect_meshtastic

meshtastic_client = connect_meshtastic()

response = f"Nodes: {len(meshtastic_client.nodes)}\n"

for node, info in meshtastic_client.nodes.items():
if "snr" in info:
snr = f"{info['snr']} dB"
else:
snr = ""
response += f"{info['user']['shortName']} {info['user']['longName']} / {info['user']['hwModel']} / {info['deviceMetrics']['batteryLevel']}% {info['deviceMetrics']['voltage']}V / {snr} / {get_relative_time(info['lastHeard'])}\n"

return response

async def handle_meshtastic_message(
self, packet, formatted_message, longname, meshnet_name
):
return False

async def handle_room_message(self, room, event, full_message):
from matrix_utils import connect_matrix

full_message = full_message.strip()
if not self.matches(full_message):
return False

response = await self.send_matrix_message(
room_id=room.room_id, message=self.generate_response(), formatted=False
)

return True

0 comments on commit d8dd310

Please sign in to comment.