kopia lustrzana https://github.com/mate-dev/meshtastic-matrix-relay
custom plugin support
rodzic
0a89d9c84e
commit
b9b1d6d83a
|
@ -1,6 +1,7 @@
|
||||||
.pyenv
|
.pyenv
|
||||||
.vscode
|
.vscode
|
||||||
config.yaml
|
config.yaml
|
||||||
|
custom_plugins/*
|
||||||
meshtastic.sqlite
|
meshtastic.sqlite
|
||||||
__pycache__/
|
__pycache__/
|
||||||
./plugins/__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,
|
on_room_message,
|
||||||
logger as matrix_logger,
|
logger as matrix_logger,
|
||||||
)
|
)
|
||||||
|
from plugin_loader import load_plugins
|
||||||
from config import relay_config
|
from config import relay_config
|
||||||
from log_utils import get_logger
|
from log_utils import get_logger
|
||||||
from meshtastic_utils import (
|
from meshtastic_utils import (
|
||||||
|
@ -35,6 +35,9 @@ async def main():
|
||||||
# Initialize the SQLite database
|
# Initialize the SQLite database
|
||||||
initialize_database()
|
initialize_database()
|
||||||
|
|
||||||
|
# Load plugins early
|
||||||
|
load_plugins()
|
||||||
|
|
||||||
matrix_client = await connect_matrix()
|
matrix_client = await connect_matrix()
|
||||||
|
|
||||||
matrix_logger.info("Connecting ...")
|
matrix_logger.info("Connecting ...")
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import sys
|
import sys
|
||||||
import importlib
|
import importlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from log_utils import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(name="Plugins")
|
||||||
|
|
||||||
plugins = []
|
plugins = []
|
||||||
|
|
||||||
|
@ -11,15 +14,20 @@ def load_plugins():
|
||||||
return plugins
|
return plugins
|
||||||
|
|
||||||
plugins = []
|
plugins = []
|
||||||
plugin_folder = Path("plugins")
|
plugin_dirs = [Path("plugins"), Path("custom_plugins")]
|
||||||
sys.path.insert(0, str(plugin_folder.resolve()))
|
|
||||||
|
|
||||||
for plugin_file in plugin_folder.glob("*.py"):
|
for plugin_folder in plugin_dirs:
|
||||||
plugin_name = plugin_file.stem
|
sys.path.insert(0, str(plugin_folder.resolve()))
|
||||||
if plugin_name == "__init__":
|
|
||||||
continue
|
for plugin_file in plugin_folder.glob("*.py"):
|
||||||
plugin_module = importlib.import_module(plugin_name)
|
plugin_name = plugin_file.stem
|
||||||
if hasattr(plugin_module, "Plugin"):
|
if plugin_name == "__init__":
|
||||||
plugins.append(plugin_module.Plugin())
|
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
|
return plugins
|
||||||
|
|
|
@ -1,7 +1,18 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from log_utils import get_logger
|
||||||
|
from config import relay_config
|
||||||
|
|
||||||
|
|
||||||
class BasePlugin(ABC):
|
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
|
@abstractmethod
|
||||||
async def handle_meshtastic_message(
|
async def handle_meshtastic_message(
|
||||||
packet, formatted_message, longname, meshnet_name
|
packet, formatted_message, longname, meshnet_name
|
||||||
|
|
|
@ -7,6 +7,8 @@ from meshtastic_utils import connect_meshtastic
|
||||||
|
|
||||||
|
|
||||||
class Plugin(BasePlugin):
|
class Plugin(BasePlugin):
|
||||||
|
plugin_name = "health"
|
||||||
|
|
||||||
async def handle_meshtastic_message(
|
async def handle_meshtastic_message(
|
||||||
self, packet, formatted_message, longname, meshnet_name
|
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):
|
class Plugin(BasePlugin):
|
||||||
|
plugin_name = "map"
|
||||||
|
|
||||||
async def handle_meshtastic_message(
|
async def handle_meshtastic_message(
|
||||||
self, packet, formatted_message, longname, meshnet_name
|
self, packet, formatted_message, longname, meshnet_name
|
||||||
):
|
):
|
||||||
|
|
Ładowanie…
Reference in New Issue