I'm trying to listen to changes of UPower device. From terminal this easily done by
$ dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/org/freedesktop/UPower/devices/battery_BAT0'"
I'm trying to write a Rust program using zbus
crate. I tried many different approaches (PropertiesProxy, DBusProxy), but the only somewhat working solution for me is this (build on obsolete zbus
version 1.8
):
let connection = Connection::new_system()?;
let object_path = "/org/freedesktop/UPower/devices/battery_BAT0";
let match_rule = format!(
"type='signal',interface='org.freedesktop.DBus.Properties',path='{}',member='PropertiesChanged'",
object_path
);
let dbus_proxy = DBusProxy::new(&connection)?;
dbus_proxy.add_match(&match_rule)?;
while let Some(message) = dbus_proxy.next_signal()? {
dbg!(message);
}
My attempt with recent version
let connection = Connection::system().await?;
let object_path = "/org/freedesktop/UPower/devices/battery_BAT0";
let match_rule = zbus::MatchRule::builder() // .to_string() shows rule, identical to version above
.msg_type(MessageType::Signal)
.path(object_path)?
.interface("org.freedesktop.DBus.Properties")?
.member("PropertiesChanged")?
.build();
let proxy: DBusProxy = DBusProxy::new(&connection).await?;
proxy.add_match_rule(match_rule).await?;
let mut signals_stream = proxy.receive_all_signals().await?; // I tried to enable caching in builder as per documentation, no success
while let Some(message) = signals_stream.next().await {
dbg!(message);
}
Ok(())
Seems like I missing some fundamental knowledge about DBus (or crate, or both), but at this point I haven't a slightest idea how to do that.