12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 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<Box<Sensor>> = 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::<Vec<String>>();
- comm::send(tx, "sensors", &reduce(status));
- thread::sleep_ms(1000);
- }
- }
- fn reduce(arr: Vec<String>) -> String {
- let result = arr.into_iter()
- .fold("".to_string(), |acc, elem| format!("{} | {}", acc, elem));
- if result.len() > 0 { result[3..].to_string() } else { result }
- }
|