From ac85c42ce909a1561d1e88a532997133532cbd0c Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sat, 7 Sep 2019 22:30:15 -0400 Subject: [PATCH] Added temperature support for data polling. --- src/main.rs | 14 ++++++++- src/widgets/network.rs | 14 +++++++-- src/widgets/processes.rs | 13 ++++----- src/widgets/temperature.rs | 58 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5a98053c..7b25ff3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ async fn main() -> Result<(), Box> { let mut list_of_timed_physical_io : Vec> = Vec::new(); let mut list_of_timed_memory : Vec = Vec::new(); let mut list_of_timed_swap : Vec = Vec::new(); + let mut list_of_timed_temperature : Vec = Vec::new(); loop { println!("Start data loop..."); @@ -24,7 +25,10 @@ async fn main() -> Result<(), Box> { // TODO: Get data, potentially store? Use a result to check! let list_of_processes = processes::get_sorted_processes_list(processes::ProcessSorting::CPU, true).await; for process in list_of_processes { - println!("Process: {} with PID {}, CPU: {}, MEM: {}", process.command, process.pid, process.cpu_usage, process.mem_usage,); + println!( + "Process: {} with PID {}, CPU: {}%, MEM: {} MB", + process.command, process.pid, process.cpu_usage_percent, process.mem_usage_in_mb, + ); } let list_of_disks = disks::get_disk_usage_list().await?; @@ -70,6 +74,14 @@ async fn main() -> Result<(), Box> { println!("Memory usage: {} out of {} is used, at {:?}", current_mem.mem_used, current_mem.mem_total, current_mem.time); } + list_of_timed_temperature.push(temperature::get_temperature_data().await?); + if !list_of_timed_temperature.is_empty() { + let current_time = list_of_timed_temperature.last().unwrap().time; + for sensor in &list_of_timed_temperature.last().unwrap().temperature_vec { + println!("Sensor for {} is at {} degrees Celsius at timestamp {:?}!", sensor.component_name, sensor.temperature, current_time); + } + } + // Send to drawing module println!("End data loop..."); window::draw_terminal(); diff --git a/src/widgets/network.rs b/src/widgets/network.rs index 1f35ca7f..6b9c54a9 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -1,3 +1,13 @@ -fn get_timestamped_network_data() {} +pub struct TimedNetworkData { + pub rx : u32, + pub tx : u32, + pub time : std::time::SystemTime, +} -fn get_network_data_list() {} +pub fn get_network_data() -> TimedNetworkData { + TimedNetworkData { + rx : 0, + tx : 0, + time : std::time::SystemTime::now(), + } +} diff --git a/src/widgets/processes.rs b/src/widgets/processes.rs index 5d3946b5..e3c1aecf 100644 --- a/src/widgets/processes.rs +++ b/src/widgets/processes.rs @@ -14,8 +14,8 @@ pub enum ProcessSorting { #[derive(Debug)] pub struct ProcessInfo { pub pid : u32, - pub cpu_usage : f32, - pub mem_usage : u64, + pub cpu_usage_percent : f32, + pub mem_usage_in_mb : u64, pub command : String, } @@ -52,7 +52,6 @@ async fn cpu_usage(process : heim::process::Process) -> heim::process::ProcessRe pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_order : bool) -> Vec { let mut process_stream = heim::process::processes().map_ok(cpu_usage).try_buffer_unordered(std::usize::MAX); - // TODO: Evaluate whether this is too slow! // TODO: Group together processes let mut process_vector : Vec = Vec::new(); @@ -64,15 +63,15 @@ pub async fn get_sorted_processes_list(sorting_method : ProcessSorting, reverse_ process_vector.push(ProcessInfo { command : process.name().await.unwrap_or_else(|_| "".to_string()), pid : process.pid() as u32, - cpu_usage : cpu_usage.get::(), - mem_usage : mem_measurement.rss().get::(), + cpu_usage_percent : cpu_usage.get::(), + mem_usage_in_mb : mem_measurement.rss().get::(), }); } } } match sorting_method { - ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage, b.cpu_usage, reverse_order)), - ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.mem_usage, b.mem_usage, reverse_order)), + ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage_percent, b.cpu_usage_percent, reverse_order)), + ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.mem_usage_in_mb, b.mem_usage_in_mb, reverse_order)), ProcessSorting::PID => process_vector.sort_by(|a, b| get_ordering(a.pid, b.pid, reverse_order)), ProcessSorting::NAME => process_vector.sort_by(|a, b| get_ordering(&a.command, &b.command, reverse_order)), } diff --git a/src/widgets/temperature.rs b/src/widgets/temperature.rs index 2617bab9..d18a7c01 100644 --- a/src/widgets/temperature.rs +++ b/src/widgets/temperature.rs @@ -1,3 +1,57 @@ -fn get_timestamped_temperature() {} +use heim_common::{prelude::StreamExt, units::thermodynamic_temperature}; -fn get_temps_list() {} +pub struct TempData { + pub component_name : Box, + pub temperature : f32, +} + +pub struct TimedTempData { + pub temperature_vec : Vec, + pub time : std::time::SystemTime, +} + +pub async fn get_temperature_data() -> Result { + let mut temperature_vec : Vec = Vec::new(); + + let mut sensor_data = heim::sensors::temperatures(); + while let Some(sensor) = sensor_data.next().await { + if let Ok(sensor) = sensor { + temperature_vec.push(TempData { + component_name : Box::from(sensor.unit()), + temperature : sensor.current().get::(), + }); + } + } + + // By default, sort temperature, then by alphabetically! Allow for configuring this... + + // Note we sort in reverse here; we want greater temps to be higher priority. + temperature_vec.sort_by(|a, b| { + if a.temperature > b.temperature { + std::cmp::Ordering::Less + } + else if a.temperature < b.temperature { + std::cmp::Ordering::Greater + } + else { + std::cmp::Ordering::Equal + } + }); + + temperature_vec.sort_by(|a, b| { + if a.component_name > b.component_name { + std::cmp::Ordering::Greater + } + else if a.component_name < b.component_name { + std::cmp::Ordering::Less + } + else { + std::cmp::Ordering::Equal + } + }); + + Ok(TimedTempData { + temperature_vec, + time : std::time::SystemTime::now(), + }) +}