From 629b69f30044d8e0a45b4a00b6554cb3168afd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= <33373714+Levminer@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:30:43 +0200 Subject: [PATCH] Linux daemon interval --- platforms/unix/daemon/src/main.rs | 17 ++++++++++------- platforms/unix/hardwareinfo/src/lib.rs | 6 +++--- platforms/unix/hardwareinfo/src/linux/mod.rs | 14 ++++++++------ platforms/unix/hardwareinfo/src/main.rs | 1 + platforms/unix/hardwareinfo/src/settings.rs | 4 ++-- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/platforms/unix/daemon/src/main.rs b/platforms/unix/daemon/src/main.rs index 5ac942d..e45ebb2 100644 --- a/platforms/unix/daemon/src/main.rs +++ b/platforms/unix/daemon/src/main.rs @@ -11,7 +11,7 @@ use axum::{ use ezrtc::host::EzRTCHost; use ezrtc::socket::DataChannelHandler; use futures::{sink::SinkExt, stream::StreamExt}; -use hardwareinfo::settings::get_settings; +use hardwareinfo::settings::{get_settings, Settings}; use hardwareinfo::{refresh_hardware_info, Data, HardwareInfo, Networks, Nvml, System}; use log::{error, info, warn, LevelFilter}; use serde::{Deserialize, Serialize}; @@ -41,6 +41,7 @@ pub struct AppState { hardware_info_receiver: async_channel::Receiver, last_60s_hardware_info: Mutex>, last_60m_hardware_info: Mutex>, + settings: Settings, } #[tokio::main] @@ -54,6 +55,10 @@ async fn main() { )]) .unwrap(); + // Get settings + let settings = get_settings(); + info!("Connection code: {:?}", settings.connection_code); + // Hardware info channel let (s, r) = async_channel::unbounded(); @@ -64,12 +69,14 @@ async fn main() { hw_info: HardwareInfo::default(), nvml: Nvml::init(), nvml_available: true, + interval: settings.interval as f64, }; let app_state = Arc::new(AppState { hardware_info_receiver: r.clone(), last_60s_hardware_info: Mutex::new(Vec::new()), last_60m_hardware_info: Mutex::new(Vec::new()), + settings: settings.clone(), }); // Setup HTTP server routes @@ -127,7 +134,7 @@ async fn main() { .push(data.hw_info.clone()); } - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + tokio::time::sleep(std::time::Duration::from_secs(settings.interval as u64)).await; } }); @@ -347,10 +354,6 @@ async fn main() { } } - // Get settings - let settings = get_settings(); - info!("Connection code: {:?}", settings.connection_code); - // Start the connection let _host = EzRTCHost::new( "wss://rtc-usw.levminer.com/one-to-many".to_string(), @@ -485,7 +488,7 @@ async fn handle_socket(mut socket: WebSocket, addr: SocketAddr, state: Arc, pub nvml_available: bool, + pub interval: f64, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -303,7 +304,6 @@ fn compare_sensor(prev_sensor: &CoresSensor, value: f64) -> CoresSensor { pub fn refresh_hardware_info(data: &mut Data) { let gb = 1024_f64.powi(3); let _mb = 1024_f64.powi(2); - let interval = 5.0; // OS Info if data.first_run { @@ -605,8 +605,8 @@ pub fn refresh_hardware_info(data: &mut Data) { let download_data = (net_data.total_received() as f64 / gb).fmt_num(); let upload_data = (net_data.total_transmitted() as f64 / gb).fmt_num(); - let throughput_download = net_data.received() as f64 / interval; - let throughput_upload = net_data.transmitted() as f64 / interval; + let throughput_download = net_data.received() as f64 / data.interval; + let throughput_upload = net_data.transmitted() as f64 / data.interval; data.hw_info.system.network.interfaces[0].download_data = download_data; data.hw_info.system.network.interfaces[0].upload_data = upload_data; diff --git a/platforms/unix/hardwareinfo/src/linux/mod.rs b/platforms/unix/hardwareinfo/src/linux/mod.rs index 79e8b4c..4040859 100644 --- a/platforms/unix/hardwareinfo/src/linux/mod.rs +++ b/platforms/unix/hardwareinfo/src/linux/mod.rs @@ -31,12 +31,14 @@ pub fn linux_hardware_info(data: &mut Data) { if let Ok(mem) = mem { for mem_device in mem { - data.hw_info.ram.info.push(CoresRAMInfo { - manufacturer_name: mem_device.manufacturer.unwrap_or("Drive".to_string()), - configured_speed: mem_device.speed_mts.unwrap_or(0), - configured_voltage: mem_device.configured_voltage.unwrap_or(0.0) * 1000.0, - size: mem_device.size.unwrap_or(1) / 1024 / 1024, - }); + if mem_device.size.unwrap_or(0) > 0 { + data.hw_info.ram.info.push(CoresRAMInfo { + manufacturer_name: mem_device.manufacturer.unwrap_or("Drive".to_string()), + configured_speed: mem_device.speed_mts.unwrap_or(0), + configured_voltage: mem_device.configured_voltage.unwrap_or(0.0) * 1000.0, + size: mem_device.size.unwrap_or(1) / 1024 / 1024, + }); + } } } diff --git a/platforms/unix/hardwareinfo/src/main.rs b/platforms/unix/hardwareinfo/src/main.rs index 70cf7f2..9d58f5c 100644 --- a/platforms/unix/hardwareinfo/src/main.rs +++ b/platforms/unix/hardwareinfo/src/main.rs @@ -10,6 +10,7 @@ fn main() { hw_info: HardwareInfo::default(), nvml: Nvml::init(), nvml_available: true, + interval: 5.0, }; loop { diff --git a/platforms/unix/hardwareinfo/src/settings.rs b/platforms/unix/hardwareinfo/src/settings.rs index e8117db..10a30b2 100644 --- a/platforms/unix/hardwareinfo/src/settings.rs +++ b/platforms/unix/hardwareinfo/src/settings.rs @@ -33,14 +33,14 @@ pub fn default_connection_code() -> String { format!("crs_{}", id) } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct ConnectionCode { pub name: String, pub code: String, pub mac: String, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Settings { #[serde(rename = "interval", default = "default_value")] pub interval: u32,