2 unstable releases

0.2.0 Jan 5, 2022
0.1.0 May 9, 2018

#163 in Unix APIs

Download history 41/week @ 2021-10-05 37/week @ 2021-10-12 42/week @ 2021-10-19 33/week @ 2021-10-26 39/week @ 2021-11-02 34/week @ 2021-11-09 38/week @ 2021-11-16 31/week @ 2021-11-23 42/week @ 2021-11-30 37/week @ 2021-12-07 43/week @ 2021-12-14 39/week @ 2021-12-21 26/week @ 2021-12-28 84/week @ 2022-01-04 53/week @ 2022-01-11 60/week @ 2022-01-18

230 downloads per month

MIT license

14KB
114 lines

upower-dbus

A Rust library which interfaces with UPower status information through dbus.

Examples

Detecting if the system is running on battery

extern crate upower_dbus;

use futures::stream::StreamExt;
use upower_dbus::UPowerProxy;

fn main() -> zbus::Result<()> {
    futures::executor::block_on(async move {
        let connection = zbus::Connection::system().await?;

        let upower = UPowerProxy::new(&connection).await?;

        println!("On Battery: {:?}", upower.on_battery().await);

        let mut stream = upower.receive_on_battery_changed().await;

        while let Some(event) = stream.next().await {
            println!("On Battery: {:?}", event.get().await);
        }

        Ok(())
    })
}

Getting the current battery status as a percentage

extern crate upower_dbus;

use upower_dbus::{DeviceProxy, UPowerProxy};

fn main() -> zbus::Result<()> {
    futures::executor::block_on(async move {
        let connection = zbus::Connection::system().await?;

        let upower = UPowerProxy::new(&connection).await?;

        let display_device = upower.get_display_device().await?;

        let device = DeviceProxy::builder(&connection)
            .path(display_device)?
            .build()
            .await?;

        println!("Battery: {:?}", device.percentage().await);

        Ok(())
    })
}

Dependencies

~7MB
~148K SLoC