kopia lustrzana https://github.com/mate-dev/meshtastic-matrix-relay
custom plugin support
rodzic
0a89d9c84e
commit
b9b1d6d83a
|
@ -1,6 +1,7 @@
|
|||
.pyenv
|
||||
.vscode
|
||||
config.yaml
|
||||
custom_plugins/*
|
||||
meshtastic.sqlite
|
||||
__pycache__/
|
||||
./plugins/__pycache__/
|
|
@ -0,0 +1,13 @@
|
|||
from plugins.base_plugin import BasePlugin
|
||||
|
||||
|
||||
class Plugin(BasePlugin):
|
||||
plugin_name = "helloworld"
|
||||
|
||||
async def handle_meshtastic_message(
|
||||
self, packet, formatted_message, longname, meshnet_name
|
||||
):
|
||||
self.logger.debug("Hello world, Meshtastic")
|
||||
|
||||
async def handle_room_message(self, room, event, full_message):
|
||||
self.logger.debug("Hello world, Matrix")
|
5
main.py
5
main.py
|
@ -16,7 +16,7 @@ from matrix_utils import (
|
|||
on_room_message,
|
||||
logger as matrix_logger,
|
||||
)
|
||||
|
||||
from plugin_loader import load_plugins
|
||||
from config import relay_config
|
||||
from log_utils import get_logger
|
||||
from meshtastic_utils import (
|
||||
|
@ -35,6 +35,9 @@ async def main():
|
|||
# Initialize the SQLite database
|
||||
initialize_database()
|
||||
|
||||
# Load plugins early
|
||||
load_plugins()
|
||||
|
||||
matrix_client = await connect_matrix()
|
||||
|
||||
matrix_logger.info("Connecting ...")
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import sys
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from log_utils import get_logger
|
||||
|
||||
logger = get_logger(name="Plugins")
|
||||
|
||||
plugins = []
|
||||
|
||||
|
@ -11,15 +14,20 @@ def load_plugins():
|
|||
return plugins
|
||||
|
||||
plugins = []
|
||||
plugin_folder = Path("plugins")
|
||||
sys.path.insert(0, str(plugin_folder.resolve()))
|
||||
plugin_dirs = [Path("plugins"), Path("custom_plugins")]
|
||||
|
||||
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"):
|
||||
plugins.append(plugin_module.Plugin())
|
||||
for plugin_folder in plugin_dirs:
|
||||
sys.path.insert(0, str(plugin_folder.resolve()))
|
||||
|
||||
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.debug(f"Loaded plugin {plugin_folder/plugin_name}")
|
||||
plugins.append(plugin)
|
||||
|
||||
return plugins
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from log_utils import get_logger
|
||||
from config import relay_config
|
||||
|
||||
|
||||
class BasePlugin(ABC):
|
||||
plugin_name = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.logger = get_logger(f"Plugin:{self.plugin_name}")
|
||||
self.config = {"active": False}
|
||||
if "plugins" in relay_config and self.plugin_name in relay_config["plugins"]:
|
||||
self.config = relay_config["plugins"][self.plugin_name]
|
||||
|
||||
@abstractmethod
|
||||
async def handle_meshtastic_message(
|
||||
packet, formatted_message, longname, meshnet_name
|
||||
|
|
|
@ -7,6 +7,8 @@ from meshtastic_utils import connect_meshtastic
|
|||
|
||||
|
||||
class Plugin(BasePlugin):
|
||||
plugin_name = "health"
|
||||
|
||||
async def handle_meshtastic_message(
|
||||
self, packet, formatted_message, longname, meshnet_name
|
||||
):
|
||||
|
|
|
@ -81,6 +81,8 @@ async def send_image(client: AsyncClient, room_id: str, image: Image.Image):
|
|||
|
||||
|
||||
class Plugin(BasePlugin):
|
||||
plugin_name = "map"
|
||||
|
||||
async def handle_meshtastic_message(
|
||||
self, packet, formatted_message, longname, meshnet_name
|
||||
):
|
||||
|
|
Ładowanie…
Reference in New Issue