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