use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FanInfo {
pub speed: Option<u32>,
pub rpm: Option<Vec<u32>>,
pub profile: Option<String>,
pub governor: Option<String>,
pub control: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FanStats {
fans: HashMap<String, FanInfo>,
}
impl FanStats {
pub fn new() -> Self {
Self {
fans: HashMap::new(),
}
}
pub fn fans(&self) -> &HashMap<String, FanInfo> {
&self.fans
}
pub fn get_fan(&self, name: &str) -> Option<&FanInfo> {
self.fans.get(name)
}
pub fn fans_mut(&mut self) -> &mut HashMap<String, FanInfo> {
&mut self.fans
}
pub fn set_speed(&mut self, name: &str, speed: u32, index: usize) -> Result<()> {
if speed > 100 {
return Err(IronError::InvalidValue(format!(
"Fan speed must be 0-100, got {}",
speed
)));
}
if !self.fans.contains_key(name) {
return Err(IronError::DeviceNotFound(format!(
"Fan '{}' not found",
name
)));
}
#[cfg(target_os = "linux")]
{
use crate::platform::linux::jetson::set_fan_speed;
set_fan_speed(name, speed, index)
}
#[cfg(not(target_os = "linux"))]
{
let _ = index; Err(IronError::UnsupportedPlatform(
"Fan control not yet implemented on Windows".to_string(),
))
}
}
pub fn set_profile(&mut self, name: &str, profile: &str) -> Result<()> {
if !self.fans.contains_key(name) {
return Err(IronError::DeviceNotFound(format!(
"Fan '{}' not found",
name
)));
}
#[cfg(target_os = "linux")]
{
use crate::platform::linux::jetson::set_fan_profile;
set_fan_profile(name, profile)
}
#[cfg(not(target_os = "linux"))]
{
let _ = profile; Err(IronError::UnsupportedPlatform(
"Fan profile control only available on Linux Jetson devices".to_string(),
))
}
}
}
impl Default for FanStats {
fn default() -> Self {
Self::new()
}
}