Try running queries and monitor at the same time

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-07-19 09:15:48 +02:00
parent 3e8951dd24
commit 58d096ef13
Signed by: pep
GPG Key ID: DEDA74AEECA9D0F2
2 changed files with 34 additions and 4 deletions

View File

@ -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"

View File

@ -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<i32> 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<dyn std::error::Error>> {
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(())
}