Initial commit
This commit is contained in:
commit
c8eaf5c045
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
/target
|
||||
/.direnv
|
||||
|
||||
|
||||
# Added by cargo
|
||||
#
|
||||
# already existing elements were commented out
|
||||
|
||||
#/target
|
1336
Cargo.lock
generated
Normal file
1336
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "upower-battery-logger"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.30"
|
||||
upower_dbus = "0.3.2"
|
||||
zbus = "3.14.1"
|
27
flake.lock
Normal file
27
flake.lock
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1706550542,
|
||||
"narHash": "sha256-UcsnCG6wx++23yeER4Hg18CXWbgNpqNXcHIo5/1Y+hc=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "97b17f32362e475016f942bbdfda4a4a72a8a652",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
27
flake.nix
Normal file
27
flake.nix
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
}: let
|
||||
inherit (self) outputs;
|
||||
supportedSystems = ["x86_64-linux"];
|
||||
forEachSupportedSystem = nixpkgs.lib.genAttrs supportedSystems;
|
||||
in {
|
||||
devShells = forEachSupportedSystem (system: let
|
||||
pkgs = import nixpkgs {inherit system;};
|
||||
in {
|
||||
rust-dev = pkgs.mkShell {
|
||||
buildInputs = with pkgs; [
|
||||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
rustPackages.clippy
|
||||
rust-analyzer
|
||||
];
|
||||
};
|
||||
|
||||
default = outputs.devShells.${system}.rust-dev;
|
||||
});
|
||||
};
|
||||
}
|
79
src/main.rs
Normal file
79
src/main.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use futures::stream::StreamExt;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt::Display,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use upower_dbus::{BatteryLevel, UPowerProxy};
|
||||
use zbus::zvariant::OwnedObjectPath;
|
||||
|
||||
enum ChargeState {
|
||||
Charging,
|
||||
Discharging,
|
||||
}
|
||||
|
||||
enum EnergyRate {
|
||||
Watts(f64),
|
||||
}
|
||||
|
||||
impl From<f64> for EnergyRate {
|
||||
fn from(value: f64) -> Self {
|
||||
Self::Watts(value)
|
||||
}
|
||||
}
|
||||
|
||||
enum BatteryCharge {
|
||||
Voltage(f64),
|
||||
}
|
||||
|
||||
impl From<f64> for BatteryCharge {
|
||||
fn from(value: f64) -> Self {
|
||||
Self::Voltage(value)
|
||||
}
|
||||
}
|
||||
|
||||
struct BatteryStatus {
|
||||
charge: BatteryCharge,
|
||||
charge_percentage: f64,
|
||||
charge_state: ChargeState,
|
||||
rate: EnergyRate,
|
||||
}
|
||||
|
||||
type BatteryIdentifier = OwnedObjectPath;
|
||||
|
||||
struct Entry {
|
||||
timestamp: SystemTime,
|
||||
batteries: HashMap<BatteryIdentifier, BatteryStatus>,
|
||||
}
|
||||
|
||||
fn main() -> zbus::Result<()> {
|
||||
futures::executor::block_on(async move {
|
||||
let connection = zbus::Connection::system().await?;
|
||||
|
||||
let upower = UPowerProxy::new(&connection).await?;
|
||||
let device_enumerator = upower.enumerate_devices().await?;
|
||||
for d in device_enumerator {
|
||||
eprintln!("Device: {d}");
|
||||
}
|
||||
|
||||
let device = upower.get_display_device().await?;
|
||||
eprintln!("Display Device: {device:?}");
|
||||
|
||||
println!("Battery Percentage: {:?}", device.percentage().await);
|
||||
|
||||
let tracker = Entry {
|
||||
timestamp: SystemTime::now(),
|
||||
batteries: HashMap::new(),
|
||||
};
|
||||
|
||||
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 Changed: {:?}", event.get().await);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue