hs added stub info grabber
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ edition = "2021"
|
||||
path = "src/hs.rs"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
default = []
|
||||
std = ["dep:sysinfo", "dep:watchdog-device"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
+3
-18
@@ -5,28 +5,13 @@ use msg::{HsCmd, HsHk, HsOutData, Msg, MsgKind, TargetMsg};
|
||||
use rfe::*;
|
||||
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
mod watchdog;
|
||||
use utils::ManualAuto;
|
||||
pub use watchdog::*;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod hs_std;
|
||||
#[cfg(feature = "std")]
|
||||
pub use hs_std::*;
|
||||
|
||||
#[cfg(not(any(feature = "std")))]
|
||||
mod hs_stub;
|
||||
#[cfg(not(any(feature = "std")))]
|
||||
pub use hs_stub::*;
|
||||
|
||||
pub trait SystemInfoGrabber {
|
||||
fn check_cpu_usage(&mut self) -> Vec<u8>;
|
||||
fn check_mem_usage(&mut self) -> u8;
|
||||
fn check_fs_usage(&mut self) -> Vec<u8>;
|
||||
fn check_temps(&mut self) -> Vec<i8>;
|
||||
}
|
||||
mod infograbber;
|
||||
pub use infograbber::*;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct HsData {
|
||||
@@ -58,12 +43,12 @@ impl<'a> Hs<'a> {
|
||||
grabber: &'a mut dyn SystemInfoGrabber,
|
||||
mut watchdog: WatchdogRef<'a>,
|
||||
) -> Self {
|
||||
watchdog.set_timeout(config.watchdog_timeout);
|
||||
if config.watchdog_enable {
|
||||
watchdog.enable();
|
||||
} else {
|
||||
watchdog.disable();
|
||||
}
|
||||
watchdog.set_timeout(config.watchdog_timeout);
|
||||
Self {
|
||||
data: Default::default(),
|
||||
config,
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
use sysinfo::{Components, CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System};
|
||||
|
||||
use crate::SystemInfoGrabber;
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub struct StdSystemInfoGrabber {
|
||||
system: System,
|
||||
}
|
||||
|
||||
impl StdSystemInfoGrabber {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
system: System::new_with_specifics(
|
||||
RefreshKind::new()
|
||||
.with_cpu(CpuRefreshKind::new().with_cpu_usage())
|
||||
.with_memory(MemoryRefreshKind::new().with_ram()),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SystemInfoGrabber for StdSystemInfoGrabber {
|
||||
fn check_cpu_usage(&mut self) -> Vec<u8> {
|
||||
self.system.refresh_cpu_usage();
|
||||
self.system
|
||||
.cpus()
|
||||
.iter()
|
||||
.map(|x| x.cpu_usage().round() as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn check_mem_usage(&mut self) -> u8 {
|
||||
self.system.refresh_memory();
|
||||
(self.system.used_memory() * 100 / self.system.total_memory()) as u8
|
||||
}
|
||||
|
||||
fn check_fs_usage(&mut self) -> Vec<u8> {
|
||||
let mut disks = Disks::new_with_refreshed_list();
|
||||
let disks = disks.list_mut();
|
||||
disks.sort_by(|x, y| x.name().cmp(y.name()));
|
||||
|
||||
disks
|
||||
.iter()
|
||||
.map(|x| ((x.total_space() - x.available_space()) * 100 / x.total_space()) as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn check_temps(&mut self) -> Vec<i8> {
|
||||
let mut components = Components::new_with_refreshed_list();
|
||||
let components = components.list_mut();
|
||||
components.sort_by(|x, y| x.label().cmp(y.label()));
|
||||
|
||||
components
|
||||
.iter()
|
||||
.map(|x| x.temperature().round() as i8)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub trait SystemInfoGrabber {
|
||||
fn check_cpu_usage(&mut self) -> Vec<u8>;
|
||||
fn check_mem_usage(&mut self) -> u8;
|
||||
fn check_fs_usage(&mut self) -> Vec<u8>;
|
||||
fn check_temps(&mut self) -> Vec<i8>;
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std"))]
|
||||
mod infograbber_std {
|
||||
use sysinfo::{Components, CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System};
|
||||
|
||||
use crate::SystemInfoGrabber;
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
pub struct StdSystemInfoGrabber {
|
||||
system: System,
|
||||
}
|
||||
|
||||
impl StdSystemInfoGrabber {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
system: System::new_with_specifics(
|
||||
RefreshKind::new()
|
||||
.with_cpu(CpuRefreshKind::new().with_cpu_usage())
|
||||
.with_memory(MemoryRefreshKind::new().with_ram()),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SystemInfoGrabber for StdSystemInfoGrabber {
|
||||
fn check_cpu_usage(&mut self) -> Vec<u8> {
|
||||
self.system.refresh_cpu_usage();
|
||||
self.system
|
||||
.cpus()
|
||||
.iter()
|
||||
.map(|x| x.cpu_usage().round() as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn check_mem_usage(&mut self) -> u8 {
|
||||
self.system.refresh_memory();
|
||||
(self.system.used_memory() * 100 / self.system.total_memory()) as u8
|
||||
}
|
||||
|
||||
fn check_fs_usage(&mut self) -> Vec<u8> {
|
||||
let mut disks = Disks::new_with_refreshed_list();
|
||||
let disks = disks.list_mut();
|
||||
disks.sort_by(|x, y| x.name().cmp(y.name()));
|
||||
|
||||
disks
|
||||
.iter()
|
||||
.map(|x| ((x.total_space() - x.available_space()) * 100 / x.total_space()) as u8)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn check_temps(&mut self) -> Vec<i8> {
|
||||
let mut components = Components::new_with_refreshed_list();
|
||||
let components = components.list_mut();
|
||||
components.sort_by(|x, y| x.label().cmp(y.label()));
|
||||
|
||||
components
|
||||
.iter()
|
||||
.map(|x| x.temperature().round() as i8)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std"))]
|
||||
pub use infograbber_std::*;
|
||||
|
||||
pub struct StubSystemInfoGrabber;
|
||||
impl SystemInfoGrabber for StubSystemInfoGrabber {
|
||||
fn check_cpu_usage(&mut self) -> Vec<u8> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn check_mem_usage(&mut self) -> u8 {
|
||||
255
|
||||
}
|
||||
|
||||
fn check_fs_usage(&mut self) -> Vec<u8> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn check_temps(&mut self) -> Vec<i8> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user