51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
use matrix_sdk::config::SyncSettings;
|
|
use meshtastic::api::StreamApi;
|
|
use meshtastic::utils;
|
|
use tokio::sync::mpsc::UnboundedReceiver;
|
|
|
|
use crate::{config, db, matrix};
|
|
|
|
pub(crate) struct Bridge {
|
|
db: db::DB,
|
|
|
|
meshtastic_listener: UnboundedReceiver<meshtastic::protobufs::FromRadio>,
|
|
meshtastic_stream_api: meshtastic::api::ConnectedStreamApi<meshtastic::api::state::Connected>,
|
|
|
|
matrix_client: matrix_sdk::Client,
|
|
}
|
|
|
|
impl Bridge {
|
|
pub async fn setup(config: config::Config) -> Result<Bridge, Box<dyn std::error::Error>> {
|
|
log::info!("starting matrix meshtastic bridge");
|
|
|
|
// setup database
|
|
let db = db::setup(config.db).await.expect("error connecting to db");
|
|
|
|
// setup meshtastic connection
|
|
let stream_api = StreamApi::new();
|
|
let tcp_stream = utils::stream::build_tcp_stream(config.meshtastic.address).await?;
|
|
let (meshtastic_listener, meshtastic_stream_api) = stream_api.connect(tcp_stream).await;
|
|
|
|
// setup matrix client
|
|
let matrix_client = matrix::build(config.matrix)
|
|
.await
|
|
.expect("error logging into matrix");
|
|
matrix_client.sync_once(SyncSettings::default()).await?;
|
|
|
|
Ok(Bridge {
|
|
db,
|
|
meshtastic_listener,
|
|
meshtastic_stream_api,
|
|
matrix_client,
|
|
})
|
|
}
|
|
|
|
pub async fn run(self: Bridge) -> Result<(), Box<dyn std::error::Error>> {
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn shutdown(mut self: Bridge) {
|
|
self.meshtastic_listener.close();
|
|
self.db.close().await;
|
|
}
|
|
}
|