mirror of
https://github.com/edu4rdshl/powerman.git
synced 2026-07-17 23:24:53 +00:00
Initial commit.
This commit is contained in:
commit
229ada8f79
8 changed files with 125 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
**/*.rs.bk
|
||||
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "powerman"
|
||||
version = "0.1.0"
|
||||
|
||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "powerman"
|
||||
version = "0.1.0"
|
||||
authors = ["Eduard Toloza <tolosaeduard@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
20
archlinux/PKGBUILD
Normal file
20
archlinux/PKGBUILD
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Maintainer: Eduard Toloza <tolosaeduard@gmail.com>
|
||||
|
||||
pkgname=powerman
|
||||
pkgver=0.1.0
|
||||
pkgrel=1
|
||||
pkgdesc='Power Management framework for Linux written in Rust..'
|
||||
arch=('x86_64')
|
||||
url='https://gitlab.com/edu4rdshl/powerman'
|
||||
license=('GPL3')
|
||||
source=("https://gitlab.com/edu4rdshl/${pkgname}/-/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz")
|
||||
sha512sums=('SKIP')
|
||||
|
||||
package() {
|
||||
cd "${pkgname}-${pkgver}"
|
||||
install -dm 755 "${pkgdir}/usr/lib/systemd/system/"
|
||||
|
||||
install -Dm 755 bin/"${pkgname}" "${pkgdir}/usr/bin/${pkgname}"
|
||||
install -Dm 755 README.md "${pkgdir}/usr/share/doc/${pkgname}/README"
|
||||
cp "${pkgname}".{service,timer} "${pkgdir}/usr/lib/systemd/system/"
|
||||
}
|
||||
BIN
bin/powerman
Executable file
BIN
bin/powerman
Executable file
Binary file not shown.
12
powerman.service
Normal file
12
powerman.service
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[Unit]
|
||||
Description=Check battery status and make actions based in arguments.
|
||||
After=default.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/bin/powerman 5 hibernate
|
||||
KillMode=process
|
||||
KillSignal=SIGINT
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
10
powerman.timer
Normal file
10
powerman.timer
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[Unit]
|
||||
Description=Check for battery status every minute.
|
||||
|
||||
[Timer]
|
||||
OnBootSec=1min
|
||||
OnUnitActiveSec=1min
|
||||
Unit=powerman.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
70
src/main.rs
Normal file
70
src/main.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
// Get user arguments, first argument is the level of battery
|
||||
// when the user want to take a action, second argument is the
|
||||
// action to take using systemctl. For example, if you want to
|
||||
// hibernate when the battery level is less than 5%, you should
|
||||
// pass the following aguments: 5 hibernate
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() == 3 {
|
||||
// Convert user agument into integer
|
||||
let bat_discharging_action: u32 = args[1]
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Error, battery percentage capacity can not be converted in a valid number.");
|
||||
battery_critical_action(bat_discharging_action, &args[2]);
|
||||
} else {
|
||||
println!("Usage:
|
||||
powerman <Minimun level of battery before ation>, <Action>
|
||||
Example: powerman 5 hibernate <- hibernate the computer when the battery level is less than 5%.
|
||||
You need to enable powerman.timer init.")
|
||||
}
|
||||
}
|
||||
|
||||
fn battery_critical_action(percentage: u32, action: &str) {
|
||||
// Path battery capacity percentage
|
||||
let battery_capacity_path = "/sys/class/power_supply/BAT0/capacity";
|
||||
let systemctl_path = "/usr/bin/systemctl";
|
||||
|
||||
// Check if systemd and BAT0 device exist
|
||||
if Path::new(&battery_capacity_path).exists() && Path::new(&systemctl_path).exists() {
|
||||
// Declare variable for get battery value into string
|
||||
let mut bat_value = String::new();
|
||||
|
||||
// Read the capacity from file
|
||||
let mut b_capacity = File::open(&battery_capacity_path).expect("Error opening file.");
|
||||
|
||||
// Convert percentage into string
|
||||
b_capacity
|
||||
.read_to_string(&mut bat_value)
|
||||
.expect("Failed to read file.");
|
||||
|
||||
// Convert percentage into integer
|
||||
let bat_percentage: u32 = bat_value
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Error, battery percentage level can not be converted in a valid number.");
|
||||
|
||||
// Compare percentage and execute actions accordy to use input
|
||||
if bat_percentage < percentage {
|
||||
let command_action = Command::new(&systemctl_path)
|
||||
// Get user argument 2 for action to execute
|
||||
.arg(&action)
|
||||
.output()
|
||||
.expect("Failed to execute command.");
|
||||
if command_action.status.success() == false {
|
||||
eprintln!("Process exited with: {}", &command_action.status);
|
||||
eprintln!(
|
||||
"\nMore information about the error:\n\n {:#?}",
|
||||
&command_action
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue