matrix-meshtastic-bridge/src/main.rs

57 lines
1.4 KiB
Rust
Raw Normal View History

use matrix_sdk::{config::SyncSettings, ruma::RoomId};
use tokio::signal;
2024-10-10 17:00:44 +00:00
use tokio::sync::mpsc;
mod bridge;
2024-10-10 17:00:44 +00:00
mod config;
mod db;
2024-10-10 17:00:44 +00:00
mod matrix;
mod meshtastic;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
2024-10-14 21:18:11 +00:00
env_logger::init();
2024-10-10 17:00:44 +00:00
let config = config::read_config().await;
let pool = db::setup(config.db).await.expect("error connecting to db");
2024-10-10 17:00:44 +00:00
let (matrix_tx, matrix_rx) = mpsc::channel(32);
let room_id = RoomId::parse(&config.matrix.room).expect("invalid room id");
log::debug!("matrix room: {:?}", room_id);
2024-10-10 17:00:44 +00:00
let matrix_client = matrix::build(config.matrix)
.await
.expect("error logging into matrix");
matrix_client.sync_once(SyncSettings::default()).await?;
let meshtastic_client = meshtastic::build(config.meshtastic)
.await
.expect("error connecting to meshtastic");
tokio::spawn(async {
meshtastic_client
.receive(matrix_tx)
.await
.expect("error receiving message from meshtastic");
2024-10-10 17:00:44 +00:00
});
tokio::spawn(matrix::sender(matrix_client.clone(), matrix_rx, room_id));
tokio::spawn(matrix::sync(matrix_client.clone()));
match signal::ctrl_c().await {
Ok(()) => {}
Err(err) => {
log::error!("Unable to listen for shutdown signal: {}", err);
}
}
log::debug!("shutting down database connection");
pool.close().await;
log::debug!("db connection closed");
2024-10-10 17:00:44 +00:00
Ok(())
}