summaryrefslogtreecommitdiffstats
path: root/src/data_collection/temperature/sysinfo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/data_collection/temperature/sysinfo.rs')
-rw-r--r--src/data_collection/temperature/sysinfo.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/data_collection/temperature/sysinfo.rs b/src/data_collection/temperature/sysinfo.rs
new file mode 100644
index 00000000..9a2ea403
--- /dev/null
+++ b/src/data_collection/temperature/sysinfo.rs
@@ -0,0 +1,53 @@
+//! Gets temperature data via sysinfo.
+
+use anyhow::Result;
+
+use super::{is_temp_filtered, TempHarvest, TemperatureType};
+use crate::app::filter::Filter;
+
+pub fn get_temperature_data(
+ sys: &sysinfo::System, temp_type: &TemperatureType, filter: &Option<Filter>,
+) -> Result<Option<Vec<TempHarvest>>> {
+ use sysinfo::{ComponentExt, SystemExt};
+
+ let mut temperature_vec: Vec<TempHarvest> = Vec::new();
+
+ let sensor_data = sys.components();
+ for component in sensor_data {
+ let name = component.label().to_string();
+
+ if is_temp_filtered(filter, &name) {
+ temperature_vec.push(TempHarvest {
+ name,
+ temperature: Some(temp_type.convert_temp_unit(component.temperature())),
+ });
+ }
+ }
+
+ // For RockPro64 boards on FreeBSD, they apparently use "hw.temperature" for sensors.
+ #[cfg(target_os = "freebsd")]
+ {
+ use sysctl::Sysctl;
+
+ const KEY: &str = "hw.temperature";
+ if let Ok(root) = sysctl::Ctl::new(KEY) {
+ for ctl in sysctl::CtlIter::below(root).flatten() {
+ if let (Ok(name), Ok(temp)) = (ctl.name(), ctl.value()) {
+ if let Some(temp) = temp.as_temperature() {
+ temperature_vec.push(TempHarvest {
+ name,
+ temperature: Some(match temp_type {
+ TemperatureType::Celsius => temp.celsius(),
+ TemperatureType::Kelvin => temp.kelvin(),
+ TemperatureType::Fahrenheit => temp.fahrenheit(),
+ }),
+ });
+ }
+ }
+ }
+ }
+ }
+
+ // TODO: Should we instead use a hashmap -> vec to skip dupes?
+ Ok(Some(temperature_vec))
+}