mirror of
https://github.com/edu4rdshl/powerman.git
synced 2026-07-17 23:24:53 +00:00
Fix issue when the battery is low but charging.
This commit is contained in:
parent
726b963006
commit
c18ef72b47
1 changed files with 16 additions and 6 deletions
22
src/main.rs
22
src/main.rs
|
|
@ -28,22 +28,32 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn battery_critical_action(percentage: u32, action: &str) {
|
fn battery_critical_action(percentage: u32, action: &str) {
|
||||||
// Path battery capacity percentage
|
// Path for battery capacity percentage, battery charging status and systemd
|
||||||
let battery_capacity_path = "/sys/class/power_supply/BAT0/capacity";
|
let battery_capacity_path = "/sys/class/power_supply/BAT0/capacity";
|
||||||
|
let battery_charging_path = "/sys/class/power_supply/BAT0/status";
|
||||||
let systemctl_path = "/usr/bin/systemctl";
|
let systemctl_path = "/usr/bin/systemctl";
|
||||||
|
|
||||||
// Check if systemd and BAT0 device exist
|
// Check if systemd and BAT0 device exist
|
||||||
if Path::new(&battery_capacity_path).exists() && Path::new(&systemctl_path).exists() {
|
if Path::new(&battery_capacity_path).exists()
|
||||||
// Declare variable for get battery value into string
|
&& Path::new(&systemctl_path).exists()
|
||||||
|
&& Path::new(&battery_charging_path).exists()
|
||||||
|
{
|
||||||
|
// Declare variables
|
||||||
let mut bat_value = String::new();
|
let mut bat_value = String::new();
|
||||||
|
let mut bat_charging = String::new();
|
||||||
|
|
||||||
// Read the capacity from file
|
// Read the capacity from file
|
||||||
let mut b_capacity = File::open(&battery_capacity_path).expect("Error opening file.");
|
let mut b_capacity = File::open(&battery_capacity_path).expect("Error opening file.");
|
||||||
|
let mut b_status = File::open(&battery_charging_path).expect("Error opening file.");
|
||||||
|
|
||||||
// Convert percentage into string
|
// Convert percentage into string
|
||||||
b_capacity
|
b_capacity
|
||||||
.read_to_string(&mut bat_value)
|
.read_to_string(&mut bat_value)
|
||||||
.expect("Failed to read file.");
|
.expect("Failed to read file.");
|
||||||
|
// Convert status into string
|
||||||
|
b_status
|
||||||
|
.read_to_string(&mut bat_charging)
|
||||||
|
.expect("Failed to read file.");
|
||||||
|
|
||||||
// Convert percentage into integer
|
// Convert percentage into integer
|
||||||
let bat_percentage: u32 = bat_value
|
let bat_percentage: u32 = bat_value
|
||||||
|
|
@ -51,10 +61,10 @@ fn battery_critical_action(percentage: u32, action: &str) {
|
||||||
.parse()
|
.parse()
|
||||||
.expect("Error, battery percentage level can not be converted in a valid number.");
|
.expect("Error, battery percentage level can not be converted in a valid number.");
|
||||||
|
|
||||||
// Compare percentage and execute actions accordy to use input
|
// Compare percentagei, status and execute actions accordy to use input
|
||||||
if bat_percentage < percentage {
|
if bat_percentage <= percentage && bat_charging.trim() == "Discharging" {
|
||||||
let command_action = Command::new(&systemctl_path)
|
let command_action = Command::new(&systemctl_path)
|
||||||
// Get user argument 2 for action to execute
|
//Get user argument 2 for action to execute
|
||||||
.arg(&action)
|
.arg(&action)
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed to execute command.");
|
.expect("Failed to execute command.");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue