Compare commits
2 Commits
d0a6b6db77
...
4c76d2b618
Author | SHA1 | Date | |
---|---|---|---|
4c76d2b618 | |||
979573d7d6 |
@ -1,3 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use dbus::arg;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
@ -32,7 +33,7 @@ impl BluetoothInfo {
|
||||
|
||||
pub fn get_all_bluetooth_devices(
|
||||
connection: &dbus::blocking::Connection,
|
||||
) -> Vec<BluetoothInfo> {
|
||||
) -> Result<Vec<BluetoothInfo>> {
|
||||
let proxy = connection.with_proxy("org.bluez", "/", Duration::from_millis(5000));
|
||||
let (result,): (HashMap<dbus::Path<'static>, HashMap<String, arg::PropMap>>,) = proxy
|
||||
.method_call(
|
||||
@ -46,5 +47,5 @@ pub fn get_all_bluetooth_devices(
|
||||
.filter(|(_, value)| value.contains_key("org.bluez.Device1"))
|
||||
.flat_map(|(_, value)| BluetoothInfo::new(value))
|
||||
.collect();
|
||||
result
|
||||
Ok(result)
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ struct SystemInfo {
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let connection = Connection::new_system()?;
|
||||
let bluetooth = bluetooth::get_all_bluetooth_devices(&connection);
|
||||
let user = UserInfo::new(&connection).unwrap_or_default();
|
||||
let power = PowerInfo::new(&connection);
|
||||
let bluetooth = bluetooth::get_all_bluetooth_devices(&connection)?;
|
||||
let user = UserInfo::new(&connection)?;
|
||||
let power = PowerInfo::new(&connection)?;
|
||||
let info = SystemInfo { bluetooth, user, power };
|
||||
println!("{}", serde_json::to_string(&info)?);
|
||||
Ok(())
|
||||
|
31
src/power.rs
31
src/power.rs
@ -70,10 +70,9 @@ impl BatteryInfo {
|
||||
fn get_batteries(
|
||||
connection: &blocking::Connection,
|
||||
proxy: &blocking::Proxy<'_, &blocking::Connection>,
|
||||
) -> Vec<Battery> {
|
||||
let (batteries,): (Vec<dbus::strings::Path>,) = proxy
|
||||
.method_call(UPOWER_DEST, "EnumerateDevices", ())
|
||||
.unwrap_or_default();
|
||||
) -> anyhow::Result<Vec<Battery>> {
|
||||
let (batteries,): (Vec<dbus::strings::Path>,) =
|
||||
proxy.method_call(UPOWER_DEST, "EnumerateDevices", ())?;
|
||||
let batteries: Vec<Battery> = batteries
|
||||
.iter()
|
||||
.filter_map(|path| match path.clone().into_cstring().into_string() {
|
||||
@ -88,18 +87,18 @@ impl BatteryInfo {
|
||||
})
|
||||
.flat_map(|path| Battery::new(connection, path).ok())
|
||||
.collect();
|
||||
batteries
|
||||
Ok(batteries)
|
||||
}
|
||||
|
||||
pub fn new(connection: &blocking::Connection) -> Self {
|
||||
pub fn new(connection: &blocking::Connection) -> anyhow::Result<Self> {
|
||||
let proxy = connection.with_proxy(UPOWER_DEST, "/org/freedesktop/UPower", TIMEOUT);
|
||||
use blocking::stdintf::org_freedesktop_dbus::Properties;
|
||||
let on_battery: bool = proxy.get(UPOWER_DEST, "OnBattery").unwrap_or(false);
|
||||
let batteries = Self::get_batteries(connection, &proxy);
|
||||
Self {
|
||||
let on_battery: bool = proxy.get(UPOWER_DEST, "OnBattery")?;
|
||||
let batteries = Self::get_batteries(connection, &proxy)?;
|
||||
Ok(Self {
|
||||
on_battery,
|
||||
batteries,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +127,7 @@ impl Battery {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, Default)]
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct PowerProfile {
|
||||
pub current: String,
|
||||
pub available: Vec<String>,
|
||||
@ -161,12 +160,12 @@ pub struct PowerInfo {
|
||||
}
|
||||
|
||||
impl PowerInfo {
|
||||
pub fn new(connection: &blocking::Connection) -> Self {
|
||||
let power_profile = PowerProfile::new(connection).unwrap_or_default();
|
||||
let batteries = BatteryInfo::new(connection);
|
||||
Self {
|
||||
pub fn new(connection: &blocking::Connection) -> anyhow::Result<Self> {
|
||||
let power_profile = PowerProfile::new(connection)?;
|
||||
let batteries = BatteryInfo::new(connection)?;
|
||||
Ok(Self {
|
||||
power_profile,
|
||||
batteries,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use dbus::blocking;
|
||||
|
||||
#[derive(serde::Serialize, Default)]
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct UserInfo {
|
||||
pub real_name: String,
|
||||
pub username: String,
|
||||
|
Loading…
x
Reference in New Issue
Block a user