Skip to content

Commit

Permalink
More human friendly temperature sensor names
Browse files Browse the repository at this point in the history
This makes the names more human friendly, and possible to distinguish from each other
  • Loading branch information
DianaNites committed Sep 14, 2022
1 parent e4e4573 commit bf46e52
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/app/data_harvester/temperature/heim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,51 @@ pub async fn get_temperature_data(
let temp_label = path.join(name.replace("input", "label"));
let temp_label = fs::read_to_string(temp_label).ok();

let name = match (&hwmon_name, &temp_label) {
// Do some messing around to get a more sensible name for sensors
//
// - For GPUs, this will use the kernel device name, ex `card0`
// - For nvme drives, this will also use the kernel name, ex `nvme0`.
// This is found differently than for GPUs
// - For whatever acpitz is, on my machine this is now `thermal_zone0`.
// - For k10temp, this will still be k10temp, but it has to be handled special.
let human_hwmon_name = {
let device = path.join("device");
// This will exist for GPUs but not others, this is how
// we find their kernel name
let drm = device.join("drm");
if drm.exists() {
// This should never actually be empty
let mut gpu = String::new();
for card in drm.read_dir()? {
let card = card?;
let name = card.file_name().to_str().unwrap_or_default().to_owned();
if name.starts_with("card") {
gpu = name;
break;
}
}
Some(gpu)
} else {
// This little mess is to account for stuff like k10temp
// This is needed because the `device` symlink
// points to `nvme*` for nvme drives, but to PCI buses for anything else
// If the first character is alphabetic,
// its an actual name like k10temp or nvme0, not a PCI bus
let link = fs::read_link(device)?
.file_name()
.map(|f| f.to_str().unwrap_or_default().to_owned())
.unwrap();
if link.as_bytes()[0].is_ascii_alphabetic() {
Some(link)
} else {
// No idea why rust thinks this may have been moved
// in a previous loop iteration and needs a clone
hwmon_name.clone()
}
}
};

let name = match (&human_hwmon_name, &temp_label) {
(Some(name), Some(label)) => format!("{}: {}", name.trim(), label.trim()),
(None, Some(label)) => label.to_string(),
(Some(name), None) => name.to_string(),
Expand Down

0 comments on commit bf46e52

Please sign in to comment.