2024-10-14 20:47:45 +00:00
|
|
|
use matrix_sdk::{config::SyncSettings, ruma::RoomId};
|
|
|
|
use tokio::signal;
|
2024-10-10 17:00:44 +00:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2024-10-14 20:47:45 +00:00
|
|
|
mod bridge;
|
2024-10-10 17:00:44 +00:00
|
|
|
mod config;
|
2024-10-14 20:47:45 +00:00
|
|
|
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-14 20:47:45 +00:00
|
|
|
|
2024-10-10 17:00:44 +00:00
|
|
|
let config = config::read_config().await;
|
|
|
|
|
2024-10-14 20:47:45 +00:00
|
|
|
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");
|
2024-10-14 20:47:45 +00:00
|
|
|
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 {
|
2024-10-14 20:47:45 +00:00
|
|
|
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));
|
2024-10-14 20:47:45 +00:00
|
|
|
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(())
|
|
|
|
}
|