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