diff --git a/Cargo.toml b/Cargo.toml index f831da3..89f34da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ async-trait = "0.1.56" mmdbus = "1.18.6" dbus-tokio = "0.7.5" tokio = { version = "1.19.2", features = [ "time", "macros", "rt-multi-thread" ] } +futures = "0.3" +futures-util = "0.3" diff --git a/src/main.rs b/src/main.rs index 722e5f0..1132457 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use mm::modem::Modem as ModemAccess; use std::fmt; use std::sync::Arc; +use futures_util::stream::StreamExt; use tokio::{ task::JoinHandle, time::Duration, @@ -26,6 +27,7 @@ use tokio::{ use mmdbus::{ dbus::{ + message::MatchRule, nonblock::{ SyncConnection, Proxy, stdintf::org_freedesktop_dbus::ObjectManager, @@ -176,16 +178,42 @@ impl From for ModemState { } } -#[tokio::main] -async fn main() -> Result<(), DBusError> { - let modems = ModemManager::connect()?.modems().await?; +async fn print_foo(mm: ModemManager) -> Result<(), DBusError> { + let modems = mm.modems().await?; println!("Modems: {:?}", modems); - for modem in modems { println!("Modem: {:?}", modem); println!("Enabled: {:?}", modem.enabled().await); println!("Model: {:?}", modem.model().await); } + Ok(()) +} + +#[tokio::main] +pub async fn main() -> Result<(), Box> { + let dbus = DBus::connect()?; + + let mut interval = tokio::time::interval(Duration::from_secs(2)); + + let calls = async { + loop { + let mm = ModemManager { dbus: dbus.clone() }; + interval.tick().await; + let _ = print_foo(mm).await; + } + }; + + let mr = MatchRule::new_signal("org.freedesktop.DBus.Properties", "PropertiesChanged"); + + let (_in_signal, stream) = dbus.conn.add_match(mr).await?.stream(); + let stream = stream.for_each(|(foo, (source,)): (_, (String,))| { + println!("Foo {}; {:?}", source, foo); + async {} + }); + + stream.await; + + futures::join!(stream, calls); Ok(()) }