2023-05-17 23:31:33 +00:00
|
|
|
import markdown
|
2023-04-25 21:29:17 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2023-04-27 23:58:29 +00:00
|
|
|
from log_utils import get_logger
|
|
|
|
from config import relay_config
|
2023-05-12 13:43:57 +00:00
|
|
|
from db_utils import (
|
|
|
|
store_plugin_data,
|
|
|
|
get_plugin_data,
|
|
|
|
get_plugin_data_for_node,
|
|
|
|
delete_plugin_data,
|
|
|
|
)
|
2023-04-25 21:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BasePlugin(ABC):
|
2023-04-27 23:58:29 +00:00
|
|
|
plugin_name = None
|
2023-05-07 18:17:23 +00:00
|
|
|
max_data_rows_per_node = 100
|
2023-05-27 18:02:52 +00:00
|
|
|
priority = 10
|
2023-04-27 23:58:29 +00:00
|
|
|
|
2023-05-17 23:31:33 +00:00
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
return f""
|
|
|
|
|
2023-04-27 23:58:29 +00:00
|
|
|
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]
|
|
|
|
|
2023-05-27 18:02:52 +00:00
|
|
|
def strip_raw(self, data):
|
|
|
|
if type(data) is not dict:
|
|
|
|
return data
|
|
|
|
|
|
|
|
if "raw" in data:
|
|
|
|
del data["raw"]
|
|
|
|
|
|
|
|
for k, v in data.items():
|
|
|
|
data[k] = self.strip_raw(v)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2023-05-17 23:31:33 +00:00
|
|
|
def get_matrix_commands(self):
|
|
|
|
return [self.plugin_name]
|
|
|
|
|
2023-05-18 01:06:11 +00:00
|
|
|
async def send_matrix_message(self, room_id, message, formatted=True):
|
2023-05-17 23:31:33 +00:00
|
|
|
from matrix_utils import connect_matrix
|
|
|
|
|
|
|
|
matrix_client = await connect_matrix()
|
|
|
|
|
|
|
|
return await matrix_client.room_send(
|
|
|
|
room_id=room_id,
|
|
|
|
message_type="m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2023-05-18 01:06:11 +00:00
|
|
|
"format": "org.matrix.custom.html" if formatted else None,
|
2023-05-17 23:31:33 +00:00
|
|
|
"body": message,
|
|
|
|
"formatted_body": markdown.markdown(message),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_mesh_commands(self):
|
|
|
|
return []
|
|
|
|
|
2023-05-12 13:43:57 +00:00
|
|
|
def store_node_data(self, meshtastic_id, node_data):
|
|
|
|
data = self.get_node_data(meshtastic_id=meshtastic_id)
|
2023-04-28 07:04:09 +00:00
|
|
|
data = data[-self.max_data_rows_per_node :]
|
2023-05-12 13:43:57 +00:00
|
|
|
if type(node_data) is list:
|
|
|
|
data.extend(node_data)
|
|
|
|
else:
|
|
|
|
data.append(node_data)
|
2023-04-28 07:04:09 +00:00
|
|
|
store_plugin_data(self.plugin_name, meshtastic_id, data)
|
|
|
|
|
2023-05-12 13:43:57 +00:00
|
|
|
def set_node_data(self, meshtastic_id, node_data):
|
|
|
|
node_data = node_data[-self.max_data_rows_per_node :]
|
|
|
|
store_plugin_data(self.plugin_name, meshtastic_id, node_data)
|
|
|
|
|
|
|
|
def delete_node_data(self, meshtastic_id):
|
|
|
|
return delete_plugin_data(self.plugin_name, meshtastic_id)
|
|
|
|
|
2023-04-28 07:04:09 +00:00
|
|
|
def get_node_data(self, meshtastic_id):
|
|
|
|
return get_plugin_data_for_node(self.plugin_name, meshtastic_id)
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return get_plugin_data(self.plugin_name)
|
|
|
|
|
2023-05-07 18:17:23 +00:00
|
|
|
def matches(self, payload):
|
2023-05-14 02:05:32 +00:00
|
|
|
from matrix_utils import bot_command
|
|
|
|
|
2023-05-07 18:17:23 +00:00
|
|
|
if type(payload) == str:
|
|
|
|
return bot_command(self.plugin_name, payload)
|
|
|
|
return False
|
|
|
|
|
2023-04-25 21:29:17 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def handle_meshtastic_message(
|
|
|
|
packet, formatted_message, longname, meshnet_name
|
|
|
|
):
|
|
|
|
print("Base plugin: handling Meshtastic message")
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def handle_room_message(room, event, full_message):
|
|
|
|
print("Base plugin: handling room message")
|