Skip to content

Commit

Permalink
Merge pull request #5 from Lenivaya/optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenivaya authored Oct 28, 2024
2 parents 9aa753e + 59c3a0d commit 265cf6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::common::PowerState::{Plugged, Unplugged};
use std::ffi::OsStr;

#[derive(Clone)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PowerState {
Plugged,
Unplugged,
Expand All @@ -10,8 +9,8 @@ pub enum PowerState {
impl From<&OsStr> for PowerState {
fn from(value: &OsStr) -> Self {
match value.to_str() {
Some("0") => Unplugged,
_ => Plugged,
Some("0") => PowerState::Unplugged,
_ => PowerState::Plugged,
}
}
}
Expand All @@ -20,7 +19,7 @@ impl From<Option<&OsStr>> for PowerState {
fn from(value: Option<&OsStr>) -> Self {
match value {
Some(value) => PowerState::from(value),
_ => Plugged,
_ => PowerState::Plugged,
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod common;
pub mod config;
pub mod handlers;
pub mod power_state_change_manager;
pub mod power_state_tracker;
pub mod systemd;
pub mod traits;
pub mod udev_power_monitor;
25 changes: 25 additions & 0 deletions src/power_state_tracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::common::PowerState;

#[derive(Debug, PartialEq, Clone, Default)]
pub struct PowerStateTracker {
last_state: Option<PowerState>,
}

impl PowerStateTracker {
pub fn new() -> Self {
Self { last_state: None }
}

pub fn should_handle(&mut self, new_state: &PowerState) -> bool {
let should_handle = match &self.last_state {
None => true, // First event, which is always handled
Some(last) => last != new_state, // Handle only if state changed
};

if should_handle {
self.last_state = Some(new_state.clone());
}

should_handle
}
}
25 changes: 19 additions & 6 deletions src/udev_power_monitor.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use crate::common::PowerState;
use crate::power_state_change_manager::PowerStateChangeManager;
use crate::power_state_tracker::PowerStateTracker;
use anyhow::{Context, Result};
use log::info;
use mio::{Events, Interest, Poll, Token};
use std::sync::Mutex;
use std::time::Duration;
use udev::MonitorBuilder;

const UDEV: Token = Token(0);

pub struct UdevPowerMonitor {
power_state_change_manager: PowerStateChangeManager,
power_state_tracker: Mutex<PowerStateTracker>,
}

impl UdevPowerMonitor {
pub fn new(power_state_change_manager: PowerStateChangeManager) -> Self {
UdevPowerMonitor {
power_state_change_manager,
power_state_tracker: Mutex::new(PowerStateTracker::new()),
}
}

Expand Down Expand Up @@ -50,12 +54,21 @@ impl UdevPowerMonitor {

fn handle_event(&self, event: &udev::Event) {
let power_state = PowerState::from(event.attribute_value("online"));
match power_state {
PowerState::Plugged => info!("Power plugged in!"),
PowerState::Unplugged => info!("Power unplugged!"),
}
if let Err(e) = self.power_state_change_manager.handle(power_state) {
eprintln!("Error handling power state change: {:?}", e);

if self
.power_state_tracker
.lock()
.unwrap()
.should_handle(&power_state)
{
match power_state {
PowerState::Plugged => info!("Power plugged in!"),
PowerState::Unplugged => info!("Power unplugged!"),
}

if let Err(e) = self.power_state_change_manager.handle(power_state) {
eprintln!("Error handling power state change: {:?}", e);
}
}
}
}

0 comments on commit 265cf6d

Please sign in to comment.