Skip to content

Commit

Permalink
Add model endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jun 2, 2024
1 parent f738600 commit 6e276fe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/features/mod.rs
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;
Expand Down
54 changes: 54 additions & 0 deletions src/features/model.rs
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())
}
}
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn run(server_address: &str) {
web::get().to(pages::root),
)
.route("/kernel_buffer", web::get().to(pages::kernel_buffer))
.route("/model", web::get().to(pages::model))
.route("/netstat", web::get().to(pages::netstat))
.route("/platform", web::get().to(pages::platform))
.route("/serial", web::get().to(pages::serial))
Expand Down
10 changes: 10 additions & 0 deletions src/server/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ pub async fn platform(req: HttpRequest) -> HttpResponse {
}
}

#[api_v2_operation]
/// Provide hardware model information
pub async fn model(req: HttpRequest) -> HttpResponse {
debug!("{:#?}", req);

HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string_pretty(&features::model::HardwareModel::new()).unwrap())
}

pub fn websocket_kernel_buffer(req: HttpRequest, stream: web::Payload) -> HttpResponse {
debug!("{:#?}", req);

Expand Down

0 comments on commit 6e276fe

Please sign in to comment.