mod battery; mod disk; mod time; mod temperature; use comm; use comm::Channel; use config::Config; use std::thread; use self::battery::BatterySensor; use self::disk::DiskSensor; use self::temperature::TempSensor; use self::time::TimeSensor; pub trait Sensor { fn status(&mut self) -> String; } pub fn sensors(tx: &Channel, config: &Config) { let zone = config.lookup("sensors.thermal_zone").unwrap(); let zone = zone.as_str().unwrap(); let mut sensors: Vec> = vec![ Box::new(DiskSensor::new("/")), Box::new(TempSensor::new(zone)), Box::new(TimeSensor::new("UTC %H:%M", true)), Box::new(TimeSensor::new("%Y-%m-%d %H:%M", false)) ]; let bat = config.lookup("sensors.battery"); let bat = bat.map(|bat| bat.as_str().unwrap()); bat.map(|bat| sensors.insert(0, Box::new(BatterySensor::new(bat)))); loop { let status = sensors.iter_mut() .map(|sensor| sensor.status()) .collect::>(); comm::send(tx, "sensors", &reduce(status)); thread::sleep_ms(1000); } } fn reduce(arr: Vec) -> String { let result = arr.into_iter() .fold("".to_string(), |acc, elem| format!("{} | {}", acc, elem)); if result.len() > 0 { result[3..].to_string() } else { result } }