diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs new file mode 100644 index 0000000..683c0e3 --- /dev/null +++ b/src-tauri/src/commands/mod.rs @@ -0,0 +1 @@ +pub mod system_data; \ No newline at end of file diff --git a/src-tauri/src/commands/system_data.rs b/src-tauri/src/commands/system_data.rs new file mode 100644 index 0000000..3fb0e6e --- /dev/null +++ b/src-tauri/src/commands/system_data.rs @@ -0,0 +1,92 @@ +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +use serde::{Deserialize, Serialize}; +use serde_json::json; +use wmi::variant::Variant; +use wmi::WMIConnection; + +fn get_system_data() -> Result> { + // 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 = wmi_con.query()?; + // println!("executed result_os"); + + #[derive(Serialize, Deserialize, Debug)] + struct Win32_Processor { + Name: String, + } + let result_cpu: Vec = wmi_con.query()?; + // println!("executed result_cpu"); + + #[derive(Serialize, Deserialize, Debug)] + struct Win32_VideoController { + Name: String, + } + let result_gpu: Vec = wmi_con.query()?; + // println!("executed result_gpu"); + + #[derive(Serialize, Deserialize, Debug)] + struct Win32_DiskDrive { + Caption: String, + Size: u64, + } + let result_disk: Vec = wmi_con.query()?; + // println!("executed result_disk"); + + #[derive(Serialize, Deserialize, Debug)] + struct Win32_PhysicalMemory { + Capacity: u64, + Speed: Variant, + Manufacturer: String, + ConfiguredClockSpeed: Variant, + } + let result_ram: Vec = wmi_con.query()?; + // println!("executed result_ram"); + + #[derive(Serialize, Deserialize, Debug)] + struct Win32_BaseBoard { + Manufacturer: String, + Product: String, + } + let result_motherboard: Vec = wmi_con.query()?; + // println!("executed result_motherboard"); + + let json_response = json!({ + "os": result_os, + "cpu": result_cpu, + "gpu": result_gpu, + "disk": result_disk, + "ram": result_ram, + "motherboard": result_motherboard, + }); + + Ok(json_response.to_string()) +} + +fn error_response(e: String) -> String { + let empty_vec: Vec = vec![]; + 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] +pub fn system_data_command() -> String { + match get_system_data() { + Ok(result) => result, + Err(e) => error_response(e.to_string()), + } +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 735bdf8..3d9a289 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,101 +1,10 @@ -#![windows_subsystem = "windows"] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -use serde::{Deserialize, Serialize}; -use serde_json::json; -use wmi::variant::Variant; -use wmi::WMIConnection; - -fn get_system_data() -> Result> { - // 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 = wmi_con.query()?; - // println!("executed result_os"); - - #[derive(Serialize, Deserialize, Debug)] - struct Win32_Processor { - Name: String, - } - let result_cpu: Vec = wmi_con.query()?; - // println!("executed result_cpu"); - - #[derive(Serialize, Deserialize, Debug)] - struct Win32_VideoController { - Name: String, - } - let result_gpu: Vec = wmi_con.query()?; - // println!("executed result_gpu"); - - #[derive(Serialize, Deserialize, Debug)] - struct Win32_DiskDrive { - Caption: String, - Size: u64, - } - let result_disk: Vec = wmi_con.query()?; - // println!("executed result_disk"); - - #[derive(Serialize, Deserialize, Debug)] - struct Win32_PhysicalMemory { - Capacity: u64, - Speed: Variant, - Manufacturer: String, - ConfiguredClockSpeed: Variant, - } - let result_ram: Vec = wmi_con.query()?; - // println!("executed result_ram"); - - #[derive(Serialize, Deserialize, Debug)] - struct Win32_BaseBoard { - Manufacturer: String, - Product: String, - } - let result_motherboard: Vec = wmi_con.query()?; - // println!("executed result_motherboard"); - - let json_response = json!({ - "os": result_os, - "cpu": result_cpu, - "gpu": result_gpu, - "disk": result_disk, - "ram": result_ram, - "motherboard": result_motherboard, - }); - - Ok(json_response.to_string()) -} - -fn error_response(e: String) -> String { - let empty_vec: Vec = 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) => error_response(e.to_string()), - } -} +// #![windows_subsystem = "windows"] +mod commands; +use commands::system_data::system_data_command; fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![get_system_data_command]) + .invoke_handler(tauri::generate_handler![system_data_command]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/App.tsx b/src/App.tsx index e91a32f..a580946 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,7 +23,7 @@ function App() { }; const getSystemDataFromAPI = async () => { - let val: any = await invoke("get_system_data_command"); + let val: any = await invoke("system_data_command"); if (val) { try { let decodedJson = dataStandardFormat(JSON.parse(val));