Debian Codegraph @

src/collection/temperature/sysinfo.rs

raw symbols SCIP
1 //! Gets temperature data via sysinfo.
2
3 use anyhow::Result;
4
5 use super::TempSensorData;
6 use crate::app::filter::Filter;
7
8 pub fn get_temperature_data(
9 components: &sysinfo::Components, filter: &Option<Filter>, graph_filter: &Option<Filter>,
10 ) -> Result<Option<Vec<TempSensorData>>> {
11 let mut temperatures: Vec<TempSensorData> = Vec::new();
12
13 for component in components {
14 let name = component.label().to_string();
15
16 if Filter::optional_should_keep(filter, &name)
17 || Filter::optional_should_keep(graph_filter, &name)
18 {
19 temperatures.push(TempSensorData {
20 name,
21 temperature: component.temperature(),
22 });
23 }
24 }
25
26 // For RockPro64 boards on FreeBSD, they apparently use "hw.temperature" for
27 // sensors.
28 #[cfg(target_os = "freebsd")]
29 {
30 use sysctl::Sysctl;
31
32 const KEY: &str = "hw.temperature";
33 if let Ok(root) = sysctl::Ctl::new(KEY) {
34 for ctl in sysctl::CtlIter::below(root).flatten() {
35 if let (Ok(name), Ok(temp)) = (ctl.name(), ctl.value()) {
36 if let Some(temp) = temp.as_temperature() {
37 temperatures.push(TempSensorData {
38 name,
39 temperature: Some(temp.celsius()),
40 });
41 }
42 }
43 }
44 }
45 }
46
47 // TODO: Should we instead use a hashmap -> vec to skip dupes?
48 Ok(Some(temperatures))
49 }