-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
- Loading branch information
1 parent
f738600
commit 6e276fe
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod kernel; | ||
pub mod kernel_websocket; | ||
pub mod model; | ||
pub mod netstat; | ||
pub mod platform; | ||
pub mod serial; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::fs; | ||
use std::process::Command; | ||
use paperclip::actix::Apiv2Schema; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Serialize, Apiv2Schema)] | ||
pub struct HardwareModel { | ||
model: String, | ||
arch: String, | ||
cpu_name: String, | ||
} | ||
|
||
impl HardwareModel { | ||
pub fn new() -> Self { | ||
Self { | ||
model: Self::get_model(), | ||
arch: Self::get_arch(), | ||
cpu_name: Self::get_cpu_name(), | ||
} | ||
} | ||
|
||
fn get_model() -> String { | ||
if let Ok(model) = fs::read_to_string("/proc/device-tree/model") { | ||
return model.trim().trim_matches(char::from(0)).to_string(); | ||
} | ||
|
||
fs::read_to_string("/sys/devices/virtual/dmi/id/product_name") | ||
.unwrap_or_else(|_| "Unknown".to_string()) | ||
.trim().trim_matches(char::from(0)) | ||
.to_string() | ||
} | ||
|
||
fn get_arch() -> String { | ||
String::from_utf8(match Command::new("uname").arg("-m").output(){ | ||
Ok(output) => output.stdout, | ||
Err(_) => return "Unknown".to_string(), | ||
}).unwrap().trim().trim_matches(char::from(0)).to_string() | ||
} | ||
|
||
fn get_cpu_name() -> String { | ||
let stdout = String::from_utf8(match Command::new("lscpu") | ||
.output() { | ||
Ok(output) => output.stdout, | ||
Err(_) => return "Unknown".to_string(), | ||
}).unwrap_or_else(|_| "Unknown".to_string()); | ||
|
||
stdout | ||
.lines() | ||
.find(|line| line.starts_with("Model name:")) | ||
.and_then(|line| line.split(':').nth(1)) | ||
.map(|s| s.trim().trim_matches(char::from(0)).to_string()) | ||
.unwrap_or_else(|| "Unknown".to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters