From 853da3974c82e3a02d530014e4018367a2ad861a Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 17 Oct 2024 22:44:10 -0700 Subject: [PATCH] use tcp for connection to meshtastic --- src/bridge.rs | 10 +++------- src/config.rs | 10 +++++++++- src/meshtastic.rs | 5 +++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/bridge.rs b/src/bridge.rs index aaff398..2b14454 100644 --- a/src/bridge.rs +++ b/src/bridge.rs @@ -1,5 +1,6 @@ use matrix_sdk::config::SyncSettings; use meshtastic::api::StreamApi; +use meshtastic::utils; use tokio::sync::mpsc::UnboundedReceiver; use crate::{config, db, matrix}; @@ -22,13 +23,8 @@ impl Bridge { // setup meshtastic connection let stream_api = StreamApi::new(); - let serial_stream = meshtastic::utils::stream::build_serial_stream( - config.meshtastic.device, - None, - None, - None, - )?; - let (meshtastic_listener, meshtastic_stream_api) = stream_api.connect(serial_stream).await; + let tcp_stream = utils::stream::build_tcp_stream(config.meshtastic.hostname).await?; + let (meshtastic_listener, meshtastic_stream_api) = stream_api.connect(tcp_stream).await; // setup matrix client let matrix_client = matrix::build(config.matrix) diff --git a/src/config.rs b/src/config.rs index fc7989f..ebc14f9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,7 +20,8 @@ pub(crate) struct MatrixConfig { #[derive(serde::Deserialize, Debug)] pub(crate) struct MeshtasticConfig { - pub(crate) device: String, + #[serde(default = "get_meshtastic_hostname")] + pub(crate) hostname: String, } fn get_device_name() -> String { @@ -38,6 +39,13 @@ fn get_matrix_password() -> String { } } +fn get_meshtastic_hostname() -> String { + match std::env::var("MESHTASTIC_HOSTNAME") { + Ok(p) => p, + Err(_) => "meshtastic.local".to_string(), + } +} + pub(crate) async fn read_config() -> Config { let filename = match std::env::var("CONFIG_PATH") { Ok(value) => value, diff --git a/src/meshtastic.rs b/src/meshtastic.rs index f5e3f00..f4dc21d 100644 --- a/src/meshtastic.rs +++ b/src/meshtastic.rs @@ -18,8 +18,9 @@ pub(crate) async fn build( config: config::MeshtasticConfig, ) -> Result> { 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; + + let tcp_stream = utils::stream::build_tcp_stream(config.hostname).await?; + let (decoded_listener, stream_api) = stream_api.connect(tcp_stream).await; log::info!("connected to meshtastic device");