Skip to content

Commit

Permalink
Merge pull request #3 from jecsham/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jecsham authored May 22, 2022
2 parents bbb0c89 + 7905c25 commit 74ba0a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
39 changes: 27 additions & 12 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,61 @@
#![allow(non_snake_case)]
use serde::{Deserialize, Serialize};
use serde_json::json;
use wmi::variant::Variant;
use wmi::WMIConnection;

fn get_system_data() -> Result<String, Box<dyn std::error::Error>> {
// let com_con = COMLibrary::new().expect("Failed to load COM library");
// let wmi_con = WMIConnection::new(com_con.into()).expect("Failed to create WMI connection");
// let com_con = COMLibrary::new()?;
// let wmi_con = WMIConnection::new(com_con.into())?;
let wmi_con = unsafe { WMIConnection::with_initialized_com(Some("ROOT\\CIMV2"))? };
// println!("executed wmi_con");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_OperatingSystem {
Caption: String,
}
let result_os: Vec<Win32_OperatingSystem> = wmi_con.query()?;
// println!("executed result_os");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_Processor {
Name: String,
}
let result_cpu: Vec<Win32_Processor> = wmi_con.query()?;
// println!("executed result_cpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_VideoController {
Name: String,
}
let result_gpu: Vec<Win32_VideoController> = wmi_con.query()?;
// println!("executed result_gpu");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_DiskDrive {
Caption: String,
Size: u64,
}
let result_disk: Vec<Win32_DiskDrive> = wmi_con.query()?;
// println!("executed result_disk");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_PhysicalMemory {
Capacity: u64,
Speed: u32,
Speed: Variant,
Manufacturer: String,
ConfiguredClockSpeed: u32,
ConfiguredClockSpeed: Variant,
}
let result_ram: Vec<Win32_PhysicalMemory> = wmi_con.query()?;
// println!("executed result_ram");

#[derive(Serialize, Deserialize, Debug)]
struct Win32_BaseBoard {
Manufacturer: String,
Product: String,
}
let result_motherboard: Vec<Win32_BaseBoard> = wmi_con.query()?;

// used to test not available data
// #[derive(Serialize, Deserialize, Debug)]
// struct Win32_TapeDrive {
// Name: String,
// }
// let not_found: Vec<Win32_TapeDrive> = wmi_con.query()?;
// println!("executed result_motherboard");

let json_response = json!({
"os": result_os,
Expand All @@ -70,11 +71,25 @@ fn get_system_data() -> Result<String, Box<dyn std::error::Error>> {
Ok(json_response.to_string())
}

fn error_response(e: String) -> String {
let empty_vec: Vec<String> = vec![];
return json!({
"os": empty_vec,
"cpu": empty_vec,
"gpu": empty_vec,
"disk": empty_vec,
"ram": empty_vec,
"motherboard": empty_vec,
"error": vec![e],
})
.to_string();
}

#[tauri::command]
fn get_system_data_command() -> String {
match get_system_data() {
Ok(result) => result,
Err(e) => format!("{}", e),
Err(e) => error_response(e.to_string()),
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function App() {
data-tauri-drag-region
className="uk-text-small uk-text-center p-n"
>
<span data-tauri-drag-region id="text-title">msi.jecsham.com</span>
<span data-tauri-drag-region id="text-title">
msi.jecsham.com
</span>
</div>
</div>
<button
Expand Down
16 changes: 13 additions & 3 deletions src/libs/std_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ const _formatRam = (jsonData: any) => {
let orgSize = convert(e.Capacity, "bytes").to("best");
let quantity = Number(orgSize.quantity).toFixed(0);
let unit = orgSize.unit.replace("i", "");
let speed = e.Speed || 0;
let configuredClockSpeed = e.ConfiguredClockSpeed || 0;
return `${quantity} ${unit} ${
e.ConfiguredClockSpeed && e.ConfiguredClockSpeed === e.Speed
? e.Speed
: `${e.ConfiguredClockSpeed}/${e.Speed}`
configuredClockSpeed === speed
? speed
: `${configuredClockSpeed}/${speed}`
} MHz | ${e.Manufacturer}`;
});
return formattedContent;
Expand Down Expand Up @@ -62,6 +64,14 @@ const _formatOs = (jsonData: any) => {
};

const dataStandardFormat = (jsonData: any) => {
if (jsonData.error) {
return [
{
title: "Error",
content: jsonData.error,
},
];
}
let formattedContent = [
{
title: "CPU",
Expand Down

0 comments on commit 74ba0a8

Please sign in to comment.