ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
//! Battery monitoring module
//!
//! Provides cross-platform battery information by delegating to the PowerSupplyMonitor.
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Battery charging state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChargingState {
    Charging,
    Discharging,
    Full,
    NotCharging,
    Unknown,
}

/// Battery health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BatteryHealth {
    Good,
    Fair,
    Poor,
    Unknown,
}

/// Battery information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatteryInfo {
    pub name: String,
    pub manufacturer: Option<String>,
    pub model: Option<String>,
    pub technology: Option<String>,
    pub serial_number: Option<String>,
    /// State of charge, or `None` where it was not read.
    ///
    /// This was `f32` and an unread charge became `0.0`, via an
    /// `unwrap_or(0)` over the `Option<u8>` that `power_supply.rs` models
    /// correctly. **Zero is not a neutral wrong answer here**: it means "about
    /// to shut down", and `power.battery.percentage` published it as a
    /// measurement, so an agent asked whether it could start a long job on a
    /// laptop with an unreadable gauge would have been told the battery was
    /// flat.
    pub charge_percent: Option<f32>,
    /// Charging state, or `None` where it was not read.
    pub state: Option<ChargingState>,
    /// Health, or `None` where it was not read.
    pub health: Option<BatteryHealth>,
    pub voltage_mv: Option<u32>,
    pub power_w: Option<f32>,
    pub energy_wh: Option<f32>,
    pub energy_full_wh: Option<f32>,
    pub energy_full_design_wh: Option<f32>,
    pub time_to_empty: Option<Duration>,
    pub time_to_full: Option<Duration>,
    pub cycle_count: Option<u32>,
    pub temperature_c: Option<f32>,
    pub wear_level_percent: Option<f32>,
}

/// Battery monitor
///
/// `Default` is derived now that `ac_connected` is an `Option`: the hand-written
/// impl existed to seed it with `true`, an unread machine reported as plugged in.
#[derive(Default)]
pub struct BatteryMonitor {
    batteries: Vec<BatteryInfo>,
    /// Whether mains power is connected, or `None` if that was not read.
    ac_connected: Option<bool>,
}

impl BatteryMonitor {
    pub fn new() -> Result<Self, crate::error::IronError> {
        let mut monitor = Self {
            batteries: Vec::new(),
            ac_connected: None,
        };
        monitor.refresh()?;
        Ok(monitor)
    }

    pub fn refresh(&mut self) -> Result<(), crate::error::IronError> {
        self.batteries.clear();
        self.ac_connected = None;

        // Delegate to the comprehensive PowerSupplyMonitor. A failed enumeration
        // is an error, not a machine with no batteries.
        let ps_monitor = crate::power_supply::PowerSupplyMonitor::new()?;
        self.ac_connected = ps_monitor.on_ac_power();

        for supply in ps_monitor.supplies() {
            if !supply.is_battery() {
                continue;
            }

            // `None` where the platform did not report it.
            let state = supply.status.map(|s| match s {
                crate::power_supply::ChargingStatus::Charging => ChargingState::Charging,
                crate::power_supply::ChargingStatus::Discharging => ChargingState::Discharging,
                crate::power_supply::ChargingStatus::Full => ChargingState::Full,
                crate::power_supply::ChargingStatus::NotCharging => ChargingState::NotCharging,
                crate::power_supply::ChargingStatus::Unknown => ChargingState::Unknown,
            });

            // Overheat, over-voltage and cold describe the battery's condition at
            // the moment it was read, not a grade of its wear. They used to map
            // to `Fair`, which told a hot battery it was degraded. They are `None`
            // now, so health is not reported rather than given a verdict.
            let health = supply.health.and_then(|h| match h {
                crate::power_supply::BatteryHealth::Good => Some(BatteryHealth::Good),
                crate::power_supply::BatteryHealth::Dead
                | crate::power_supply::BatteryHealth::UnspecifiedFailure => {
                    Some(BatteryHealth::Poor)
                }
                crate::power_supply::BatteryHealth::Overheat
                | crate::power_supply::BatteryHealth::OverVoltage
                | crate::power_supply::BatteryHealth::Cold => None,
                _ => Some(BatteryHealth::Unknown),
            });

            self.batteries.push(BatteryInfo {
                name: supply.name.clone(),
                manufacturer: supply.manufacturer.clone(),
                model: supply.model_name.clone(),
                technology: supply.technology.clone(),
                serial_number: supply.serial_number.clone(),
                // The `Option` that `power_supply` went to the trouble of
                // producing, kept rather than flattened to a sentinel.
                charge_percent: supply.capacity_percent.map(f32::from),
                state,
                health,
                voltage_mv: supply.voltage_now_mv,
                power_w: supply.power_w(),
                energy_wh: supply.energy_wh(),
                energy_full_wh: supply.energy_full_wh(),
                energy_full_design_wh: supply.energy_full_design_wh(),
                time_to_empty: supply
                    .time_to_empty_min
                    .map(|m| Duration::from_secs(m as u64 * 60)),
                time_to_full: supply
                    .time_to_full_min
                    .map(|m| Duration::from_secs(m as u64 * 60)),
                cycle_count: supply.cycle_count,
                temperature_c: supply.temperature_celsius(),
                wear_level_percent: supply.wear_level_percent(),
            });
        }

        Ok(())
    }

    pub fn batteries(&self) -> &[BatteryInfo] {
        &self.batteries
    }
    pub fn ac_connected(&self) -> Option<bool> {
        self.ac_connected
    }
    pub fn has_battery(&self) -> bool {
        !self.batteries.is_empty()
    }
    pub fn primary_battery(&self) -> Option<&BatteryInfo> {
        self.batteries.first()
    }
}