meshtastic-matrix-relay/main.py

81 wiersze
2.4 KiB
Python
Czysty Zwykły widok Historia

"""
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
from db_utils import initialize_database, update_longnames
from matrix_utils import (
connect_matrix,
join_matrix_room,
on_room_message,
logger as matrix_logger,
)
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,
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 17:04:54 +00:00
matrix_client = await connect_matrix()
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)
2023-04-27 17:04:54 +00:00
matrix_logger.info(f"Login response: {login_response}")
2023-04-20 19:18:04 +00:00
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
# 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()
)
# 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:
# Update longnames
2023-04-27 14:24:32 +00:00
update_longnames(meshtastic_interface.nodes)
2023-04-20 19:18:04 +00:00
2023-04-27 17:04:54 +00:00
matrix_logger.info("Syncing with Matrix 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-27 17:04:54 +00:00
matrix_logger.error(f"Error syncing with Matrix server: {e}")
2023-04-17 23:50:28 +00:00
2023-04-18 16:25:42 +00:00
await asyncio.sleep(60) # Update longnames every 60 seconds
2023-04-23 00:25:37 +00:00
2023-04-25 21:29:17 +00:00
asyncio.run(main())