2023-04-20 20:21:09 +00:00
|
|
|
"""
|
|
|
|
This script connects a Meshtastic mesh network to Matrix chat rooms by relaying messages between them.
|
|
|
|
It uses Meshtastic-python and Matrix nio client library to interface with the radio and the Matrix server respectively.
|
|
|
|
"""
|
2023-04-17 23:50:28 +00:00
|
|
|
import asyncio
|
2023-04-23 00:25:37 +00:00
|
|
|
from nio import (
|
|
|
|
RoomMessageText,
|
|
|
|
RoomMessageNotice,
|
|
|
|
)
|
2023-04-17 23:50:28 +00:00
|
|
|
from pubsub import pub
|
2023-04-27 17:04:54 +00:00
|
|
|
from typing import List
|
2023-06-09 03:37:55 +00:00
|
|
|
from db_utils import initialize_database, update_longnames, update_shortnames
|
2023-04-27 17:04:54 +00:00
|
|
|
from matrix_utils import (
|
|
|
|
connect_matrix,
|
|
|
|
join_matrix_room,
|
|
|
|
on_room_message,
|
|
|
|
logger as matrix_logger,
|
2023-04-27 14:15:49 +00:00
|
|
|
)
|
2023-04-27 23:58:29 +00:00
|
|
|
from plugin_loader import load_plugins
|
2023-04-27 17:04:54 +00:00
|
|
|
from config import relay_config
|
|
|
|
from log_utils import get_logger
|
|
|
|
from meshtastic_utils import (
|
|
|
|
connect_meshtastic,
|
|
|
|
on_meshtastic_message,
|
2023-05-17 23:31:33 +00:00
|
|
|
on_lost_meshtastic_connection,
|
2023-04-27 17:04:54 +00:00
|
|
|
logger as meshtastic_logger,
|
|
|
|
)
|
2023-04-17 23:50:28 +00:00
|
|
|
|
2023-04-27 17:04:54 +00:00
|
|
|
logger = get_logger(name="M<>M Relay")
|
|
|
|
meshtastic_interface = connect_meshtastic()
|
2023-04-20 18:27:04 +00:00
|
|
|
matrix_rooms: List[dict] = relay_config["matrix_rooms"]
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_access_token = relay_config["matrix"]["access_token"]
|
2023-04-17 23:50:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2023-04-18 16:25:42 +00:00
|
|
|
# Initialize the SQLite database
|
|
|
|
initialize_database()
|
|
|
|
|
2023-04-27 23:58:29 +00:00
|
|
|
# Load plugins early
|
|
|
|
load_plugins()
|
|
|
|
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_client = await connect_matrix()
|
2023-04-21 06:00:42 +00:00
|
|
|
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_logger.info("Connecting ...")
|
2023-04-20 19:18:04 +00:00
|
|
|
try:
|
|
|
|
login_response = await matrix_client.login(matrix_access_token)
|
|
|
|
except Exception as e:
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_logger.error(f"Error connecting to Matrix server: {e}")
|
2023-04-20 19:18:04 +00:00
|
|
|
return
|
2023-04-23 00:25:37 +00:00
|
|
|
|
2023-04-20 20:21:09 +00:00
|
|
|
# Join the rooms specified in the config.yaml
|
|
|
|
for room in matrix_rooms:
|
|
|
|
await join_matrix_room(matrix_client, room["id"])
|
2023-04-20 19:18:04 +00:00
|
|
|
|
2023-04-17 23:50:28 +00:00
|
|
|
# Register the Meshtastic message callback
|
2023-04-27 17:04:54 +00:00
|
|
|
meshtastic_logger.info(f"Listening for inbound radio messages ...")
|
2023-04-17 23:50:28 +00:00
|
|
|
pub.subscribe(
|
|
|
|
on_meshtastic_message, "meshtastic.receive", loop=asyncio.get_event_loop()
|
|
|
|
)
|
2023-05-17 23:31:33 +00:00
|
|
|
pub.subscribe(
|
|
|
|
on_lost_meshtastic_connection,
|
|
|
|
"meshtastic.connection.lost",
|
|
|
|
)
|
2023-04-17 23:50:28 +00:00
|
|
|
# Register the message callback
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_logger.info(f"Listening for inbound matrix messages ...")
|
2023-04-23 00:25:37 +00:00
|
|
|
matrix_client.add_event_callback(
|
|
|
|
on_room_message, (RoomMessageText, RoomMessageNotice)
|
|
|
|
)
|
2023-04-17 23:50:28 +00:00
|
|
|
|
|
|
|
# Start the Matrix client
|
2023-04-18 16:25:42 +00:00
|
|
|
while True:
|
2023-04-20 19:18:04 +00:00
|
|
|
try:
|
2023-06-08 22:26:32 +00:00
|
|
|
# Update longnames & shortnames
|
2023-04-27 14:24:32 +00:00
|
|
|
update_longnames(meshtastic_interface.nodes)
|
2023-06-09 03:37:55 +00:00
|
|
|
update_shortnames(meshtastic_interface.nodes)
|
2023-04-20 19:18:04 +00:00
|
|
|
|
2023-04-28 07:04:09 +00:00
|
|
|
matrix_logger.info("Syncing with server...")
|
2023-04-20 19:18:04 +00:00
|
|
|
await matrix_client.sync_forever(timeout=30000)
|
2023-04-27 17:04:54 +00:00
|
|
|
matrix_logger.info("Sync completed.")
|
2023-04-20 19:18:04 +00:00
|
|
|
except Exception as e:
|
2023-04-28 07:04:09 +00:00
|
|
|
matrix_logger.error(f"Error syncing with server: {e}")
|
2023-04-17 23:50:28 +00:00
|
|
|
|
2023-06-08 22:26:32 +00:00
|
|
|
await asyncio.sleep(60) # Update longnames & shortnames every 60 seconds
|
2023-04-18 16:45:19 +00:00
|
|
|
|
2023-04-23 00:25:37 +00:00
|
|
|
|
2023-04-25 21:29:17 +00:00
|
|
|
asyncio.run(main())
|