mod.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. mod battery;
  2. mod disk;
  3. mod time;
  4. mod temperature;
  5. use comm;
  6. use comm::Channel;
  7. use config::Config;
  8. use std::thread;
  9. use self::battery::BatterySensor;
  10. use self::disk::DiskSensor;
  11. use self::temperature::TempSensor;
  12. use self::time::TimeSensor;
  13. pub trait Sensor {
  14. fn status(&mut self) -> String;
  15. }
  16. pub fn sensors(tx: &Channel, config: &Config) {
  17. let zone = config.lookup("sensors.thermal_zone").unwrap();
  18. let zone = zone.as_str().unwrap();
  19. let mut sensors: Vec<Box<Sensor>> = vec![
  20. Box::new(DiskSensor::new("/")),
  21. Box::new(TempSensor::new(zone)),
  22. Box::new(TimeSensor::new("UTC %H:%M", true)),
  23. Box::new(TimeSensor::new("%Y-%m-%d %H:%M", false))
  24. ];
  25. let bat = config.lookup("sensors.battery");
  26. let bat = bat.map(|bat| bat.as_str().unwrap());
  27. bat.map(|bat| sensors.insert(0, Box::new(BatterySensor::new(bat))));
  28. loop {
  29. let status = sensors.iter_mut()
  30. .map(|sensor| sensor.status())
  31. .collect::<Vec<String>>();
  32. comm::send(tx, "sensors", &reduce(status));
  33. thread::sleep_ms(1000);
  34. }
  35. }
  36. fn reduce(arr: Vec<String>) -> String {
  37. let result = arr.into_iter()
  38. .fold("".to_string(), |acc, elem| format!("{} | {}", acc, elem));
  39. if result.len() > 0 { result[3..].to_string() } else { result }
  40. }