56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
|
use matrix_sdk::ruma::events::room::message::RoomMessageEventContent;
|
||
|
use meshtastic::api::state::Connected;
|
||
|
use meshtastic::api::{ConnectedStreamApi, StreamApi};
|
||
|
use meshtastic::protobufs::FromRadio;
|
||
|
use meshtastic::utils;
|
||
|
use tokio::sync::mpsc::{Sender, UnboundedReceiver};
|
||
|
use tokio::time::Duration;
|
||
|
|
||
|
use crate::config;
|
||
|
|
||
|
pub struct MeshtasticClient {
|
||
|
decoded_listener: UnboundedReceiver<FromRadio>,
|
||
|
stream_api: ConnectedStreamApi<Connected>,
|
||
|
}
|
||
|
|
||
|
pub(crate) async fn build(
|
||
|
config: config::MeshtasticConfig,
|
||
|
) -> Result<MeshtasticClient, Box<dyn std::error::Error>> {
|
||
|
let stream_api = StreamApi::new();
|
||
|
let serial_stream = utils::stream::build_serial_stream(config.device, None, None, None)?;
|
||
|
let (decoded_listener, stream_api) = stream_api.connect(serial_stream).await;
|
||
|
|
||
|
println!("connected to meshtastic device");
|
||
|
|
||
|
Ok(MeshtasticClient {
|
||
|
decoded_listener: decoded_listener,
|
||
|
stream_api: stream_api,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
impl MeshtasticClient {
|
||
|
pub async fn receive(
|
||
|
mut self: MeshtasticClient,
|
||
|
matrix_sender: Sender<RoomMessageEventContent>,
|
||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||
|
// let config_id = utils::generate_rand_id();
|
||
|
// let stream_api = stream_api.configure(config_id).await?;
|
||
|
|
||
|
// This loop can be broken with ctrl+c, or by disconnecting
|
||
|
// the attached serial port.
|
||
|
|
||
|
println!("listening for messages from meshtastic");
|
||
|
while let Some(decoded) = self.decoded_listener.recv().await {
|
||
|
println!("Received: {:?}", decoded);
|
||
|
|
||
|
let msg = RoomMessageEventContent::text_plain(format!("{:?}", decoded));
|
||
|
|
||
|
matrix_sender
|
||
|
.send_timeout(msg, Duration::from_secs(10))
|
||
|
.await?;
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|