Load plugins directly

feature/plugins-parameters
Geoff Whittington 2023-05-13 22:05:32 -04:00
rodzic b531f91cb4
commit 0c2053e4ad
8 zmienionych plików z 40 dodań i 218 usunięć

Wyświetl plik

@ -1,36 +1,34 @@
import os
import sys
import importlib
from pathlib import Path
from plugins.health_plugin import Plugin as HealthPlugin
from plugins.map_plugin import Plugin as MapPlugin
from plugins.mesh_relay_plugin import Plugin as MeshRelayPlugin
from plugins.ping_plugin import Plugin as PingPlugin
from plugins.telemetry_plugin import Plugin as TelemetryPlugin
from plugins.weather_plugin import Plugin as WeatherPlugin
from log_utils import get_logger
logger = get_logger(name="Plugins")
plugins = []
active_plugins = []
def load_plugins():
global plugins
if plugins:
return plugins
if active_plugins:
return active_plugins
plugins = []
plugin_dirs = [Path("plugins")]
plugins = [
HealthPlugin(),
MapPlugin(),
MeshRelayPlugin(),
PingPlugin(),
TelemetryPlugin(),
WeatherPlugin(),
]
for plugin_folder in plugin_dirs:
sys.path.insert(0, str(plugin_folder.resolve()))
for plugin in plugins:
if plugin.config["active"]:
logger.info(f"Loaded {plugin.plugin_name}")
active_plugins.append(plugin)
for plugin_file in plugin_folder.glob("*.py"):
plugin_name = plugin_file.stem
if plugin_name == "__init__":
continue
plugin_module = importlib.import_module(plugin_name)
if hasattr(plugin_module, "Plugin"):
plugin = plugin_module.Plugin()
if plugin.config["active"]:
logger.info(
f"Loaded {os.path.basename(plugin_folder)}/{plugin_name}"
)
plugins.append(plugin)
return plugins
return active_plugins

Wyświetl plik

@ -7,7 +7,6 @@ from db_utils import (
get_plugin_data_for_node,
delete_plugin_data,
)
from matrix_utils import bot_command
class BasePlugin(ABC):
@ -44,6 +43,8 @@ class BasePlugin(ABC):
return get_plugin_data(self.plugin_name)
def matches(self, payload):
from matrix_utils import bot_command
if type(payload) == str:
return bot_command(self.plugin_name, payload)
return False

Wyświetl plik

@ -1,15 +1,14 @@
import re
import statistics
from base_plugin import BasePlugin
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
from plugins.base_plugin import BasePlugin
class Plugin(BasePlugin):
plugin_name = "health"
def generate_response(self):
from meshtastic_utils import connect_meshtastic
meshtastic_client = connect_meshtastic()
battery_levels = []
air_util_tx = []
@ -44,6 +43,8 @@ SNR: {avg_snr:.2f} / {mdn_snr:.2f} (avg / median)
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

Wyświetl plik

@ -5,11 +5,7 @@ import io
import re
from PIL import Image
from nio import AsyncClient, UploadResponse
from base_plugin import BasePlugin
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
from plugins.base_plugin import BasePlugin
def anonymize_location(lat, lon, radius=1000):
@ -93,6 +89,9 @@ class Plugin(BasePlugin):
if not self.matches(full_message):
return False
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
matrix_client = await connect_matrix()
meshtastic_client = connect_meshtastic()

Wyświetl plik

@ -8,8 +8,6 @@ from meshtastic import mesh_pb2
from plugins.base_plugin import BasePlugin
from config import relay_config
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
matrix_rooms: List[dict] = relay_config["matrix_rooms"]
@ -57,6 +55,8 @@ class Plugin(BasePlugin):
async def handle_meshtastic_message(
self, packet, formatted_message, longname, meshnet_name
):
from matrix_utils import connect_matrix
packet = self.process(packet)
matrix_client = await connect_matrix()
@ -121,6 +121,8 @@ class Plugin(BasePlugin):
self.logger.error(f"Error processing embedded packet: {e}")
return
from meshtastic_utils import connect_meshtastic
meshtastic_client = connect_meshtastic()
meshPacket = mesh_pb2.MeshPacket()
meshPacket.channel = channel

Wyświetl plik

@ -1,43 +0,0 @@
import re
from plugins.base_plugin import BasePlugin
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
class Plugin(BasePlugin):
plugin_name = "ping"
async def handle_meshtastic_message(
self, packet, formatted_message, longname, meshnet_name
):
if (
"decoded" in packet
and "portnum" in packet["decoded"]
and packet["decoded"]["portnum"] == "TEXT_MESSAGE_APP"
and "text" in packet["decoded"]
):
message = packet["decoded"]["text"]
message = message.strip()
if f"!{self.plugin_name}" not in message:
return
meshtastic_client = connect_meshtastic()
meshtastic_client.sendText(text="pong!", destinationId=packet["fromId"])
return True
async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
return False
matrix_client = await connect_matrix()
response = await matrix_client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "pong!",
},
)
return True

Wyświetl plik

@ -1,136 +0,0 @@
import json
import io
import re
import matplotlib.pyplot as plt
from PIL import Image
from datetime import datetime, timedelta
from plugins.base_plugin import BasePlugin
from matrix_utils import bot_command, connect_matrix, upload_image, send_room_image
class Plugin(BasePlugin):
plugin_name = "telemetry"
max_data_rows_per_node = 50
def _generate_timeperiods(self, hours=12):
# Calculate the start and end times
end_time = datetime.now()
start_time = end_time - timedelta(hours=hours)
# Create a list of hourly intervals for the last 12 hours
hourly_intervals = []
current_time = start_time
while current_time <= end_time:
hourly_intervals.append(current_time)
current_time += timedelta(hours=1)
return hourly_intervals
async def handle_meshtastic_message(
self, packet, formatted_message, longname, meshnet_name
):
# Support deviceMetrics only for now
if (
"decoded" in packet
and "portnum" in packet["decoded"]
and packet["decoded"]["portnum"] == "TELEMETRY_APP"
and "telemetry" in packet["decoded"]
and "deviceMetrics" in packet["decoded"]["telemetry"]
):
telemetry_data = []
data = self.get_node_data(meshtastic_id=packet["fromId"])
if data:
telemetry_data = data
packet_data = packet["decoded"]["telemetry"]
telemetry_data.append(
{
"time": packet_data["time"],
"batteryLevel": packet_data["deviceMetrics"]["batteryLevel"],
"voltage": packet_data["deviceMetrics"]["voltage"],
"airUtilTx": packet_data["deviceMetrics"]["airUtilTx"],
}
)
self.set_node_data(meshtastic_id=packet["fromId"], node_data=telemetry_data)
return False
def matches(self, payload):
if type(payload) == str:
for option in ["batteryLevel", "voltage", "airUtilTx"]:
if bot_command(option, payload):
return True
return False
async def handle_room_message(self, room, event, full_message):
full_message = full_message.strip()
if not self.matches(full_message):
return False
match = re.match(r"^.*: !(batteryLevel|voltage|airUtilTx)$", full_message)
if not match:
return False
telemetry_option = match.group(1)
hourly_intervals = self._generate_timeperiods()
matrix_client = await connect_matrix()
# Compute the hourly averages for each node
hourly_averages = {}
for node_data_json in self.get_data():
node_data_rows = json.loads(node_data_json[0])
for record in node_data_rows:
record_time = datetime.fromtimestamp(
record["time"]
) # Replace with your timestamp field name
telemetry_value = record[
telemetry_option
] # Replace with your battery level field name
for i in range(len(hourly_intervals) - 1):
if hourly_intervals[i] <= record_time < hourly_intervals[i + 1]:
if i not in hourly_averages:
hourly_averages[i] = []
hourly_averages[i].append(telemetry_value)
break
# Compute the final hourly averages
final_averages = {}
for i, interval in enumerate(hourly_intervals[:-1]):
if i in hourly_averages:
final_averages[interval] = sum(hourly_averages[i]) / len(
hourly_averages[i]
)
else:
final_averages[interval] = 0.0
# Extract the hourly intervals and average values into separate lists
hourly_intervals = list(final_averages.keys())
average_values = list(final_averages.values())
# Convert the hourly intervals to strings
hourly_strings = [hour.strftime("%H") for hour in hourly_intervals]
# Create the plot
fig, ax = plt.subplots()
ax.plot(hourly_strings, average_values)
# Set the plot title and axis labels
ax.set_title(f"Hourly {telemetry_option} Averages")
ax.set_xlabel("Hour")
ax.set_ylabel(f"{telemetry_option}")
# Rotate the x-axis labels for readability
plt.xticks(rotation=45)
# Save the plot as a PIL image
buf = io.BytesIO()
fig.canvas.print_png(buf)
buf.seek(0)
img = Image.open(buf)
pil_image = Image.frombytes(mode="RGBA", size=img.size, data=img.tobytes())
upload_response = await upload_image(matrix_client, pil_image, "graph.png")
await send_room_image(matrix_client, room.room_id, upload_response)
return True

Wyświetl plik

@ -2,8 +2,6 @@ import re
import requests
from plugins.base_plugin import BasePlugin
from matrix_utils import connect_matrix
from meshtastic_utils import connect_meshtastic
class Plugin(BasePlugin):
@ -80,6 +78,8 @@ class Plugin(BasePlugin):
if f"!{self.plugin_name}" not in message:
return False
from meshtastic_utils import connect_meshtastic
meshtastic_client = connect_meshtastic()
if packet["fromId"] in meshtastic_client.nodes:
weather_notice = "Cannot determine location"