Skip to content

Commit

Permalink
Linux daemon interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Oct 1, 2024
1 parent 13f0a3f commit 629b69f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
17 changes: 10 additions & 7 deletions platforms/unix/daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -41,6 +41,7 @@ pub struct AppState {
hardware_info_receiver: async_channel::Receiver<HardwareInfo>,
last_60s_hardware_info: Mutex<Vec<HardwareInfo>>,
last_60m_hardware_info: Mutex<Vec<HardwareInfo>>,
settings: Settings,
}

#[tokio::main]
Expand All @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
});

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -485,7 +488,7 @@ async fn handle_socket(mut socket: WebSocket, addr: SocketAddr, state: Arc<AppSt
break;
}

tokio::time::sleep(std::time::Duration::from_secs(5)).await;
tokio::time::sleep(std::time::Duration::from_secs(state.settings.interval as u64)).await;
}

match sender
Expand Down
6 changes: 3 additions & 3 deletions platforms/unix/hardwareinfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Data {
pub first_run: bool,
pub nvml: Result<Nvml, nvml_wrapper::error::NvmlError>,
pub nvml_available: bool,
pub interval: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions platforms/unix/hardwareinfo/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
}

Expand Down
1 change: 1 addition & 0 deletions platforms/unix/hardwareinfo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
hw_info: HardwareInfo::default(),
nvml: Nvml::init(),
nvml_available: true,
interval: 5.0,
};

loop {
Expand Down
4 changes: 2 additions & 2 deletions platforms/unix/hardwareinfo/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 629b69f

Please sign in to comment.