Files
rust_device/src/system_values.rs
2021-06-04 23:45:38 +02:00

70 lines
2.1 KiB
Rust

use std::{
env,
process::Command,
};
use dotenv::dotenv;
pub fn get_temperature() -> f32 {
dotenv().ok();
let mut temperature = 0.0;
let system = env::var("SYSTEM").unwrap().to_string();
if system == "ubuntu" {
let temperature_command = Command::new("cat")
.arg("/sys/class/thermal/thermal_zone1/temp")
.output().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e)
});
let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..5];
temperature = temperature_string.parse::<f32>().unwrap();
temperature = temperature / 1000.0;
} else {
let temperature_command = Command::new("cat")
.arg("/sys/class/thermal/thermal_zone1/temp")
.output().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e)
});
let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..9];
println!("Raspi temp {:?}", temperature_string);
temperature += 1.0;
}
println!("Temperature {}", temperature);
return temperature;
}
pub fn get_storage() -> i8 {
let storage_command = Command::new("df")
.arg("-h")
.arg("/")
.output().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e)
});
let storage_string = &String::from_utf8_lossy(&storage_command.stdout);
let vec_storage : Vec<&str> = storage_string.split_whitespace().collect();
let storage = (vec_storage[vec_storage.len() - 2][..2]).parse::<i8>().unwrap();
return storage;
}
pub fn get_mem() -> f32 {
let mem_command = Command::new("free")
.arg("-m")
.output().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e)
});
let mem_string = &String::from_utf8_lossy(&mem_command.stdout);
let vec_mem : Vec<&str> = mem_string.split_whitespace().collect();
let total = vec_mem[7].parse::<f32>().unwrap();
let mem_use = vec_mem[8].parse::<f32>().unwrap();
let result = mem_use / total * 100.0 ;
return result;
}