From 3edf4aa95ac34de1e8bc66f63a4bcaee95bf241a Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 07:35:03 -0700 Subject: [PATCH 1/9] fixed utf8 parse error with lossy conversion --- status_cloud/src/main.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 6614975..1a68b99 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -60,15 +60,6 @@ fn main() -> Result<(), Box> { } } - match Command::new(args.hddtemp_executable.clone()).output() { - Ok(val) => val, - Err(e) => { - println!("Error running hddtemp at all: {}", e.to_string()); - println!("hddtemp = {}", args.hddtemp_executable.clone()); - return Ok(()); - } - }; - { let mut drive_temps: Vec = vec![]; @@ -82,13 +73,7 @@ fn main() -> Result<(), Box> { } }; - let tmp = match std::str::from_utf8(&output.stdout) { - Ok(val) => val, - Err(e) => { - println!("Error parsing hddtemp output: {}", e.to_string()); - return Ok(()); - } - }.to_string(); + let tmp = String::from_utf8_lossy(&output.stdout).into_owned(); if !tmp.contains("sensor") { drive_temps.push(tmp.replace("\n", "Not running as sudo?")); From e4ff4eca09550dda42161ef08f0f6c02aae1ebab Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 07:40:57 -0700 Subject: [PATCH 2/9] removed incorrect replace --- status_cloud/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 1a68b99..085f076 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -76,7 +76,7 @@ fn main() -> Result<(), Box> { let tmp = String::from_utf8_lossy(&output.stdout).into_owned(); if !tmp.contains("sensor") { - drive_temps.push(tmp.replace("\n", "Not running as sudo?")); + drive_temps.push(tmp.replace("\n", "")); } else { drive_temps.push(tmp[0..9].to_string() + " No Sensor"); } From 5681f4a28557228cd56b293c5f6a81d1b1be348a Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:30:58 -0700 Subject: [PATCH 3/9] added logging and cleaned code up --- Cargo.lock | 78 ++++++++++++++++++++++- status_cloud/Cargo.toml | 2 + status_cloud/src/main.rs | 132 ++++++++++++++++++++++----------------- 3 files changed, 152 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d84fb0..2fab129 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,7 +156,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", - "time", + "time 0.1.45", "wasm-bindgen", "winapi", ] @@ -224,6 +224,12 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "deranged" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946" + [[package]] name = "encoding_rs" version = "0.8.32" @@ -631,6 +637,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.31.1" @@ -887,6 +902,17 @@ dependencies = [ "serde", ] +[[package]] +name = "simplelog" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acee08041c5de3d5048c8b3f6f13fafb3026b24ba43c6a695a0c76179b844369" +dependencies = [ + "log", + "termcolor", + "time 0.3.27", +] + [[package]] name = "slab" version = "0.4.8" @@ -922,9 +948,11 @@ version = "0.1.0" dependencies = [ "chrono", "clap", + "log", "reqwest", "serde", "serde_json", + "simplelog", "toml", ] @@ -958,6 +986,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "time" version = "0.1.45" @@ -969,6 +1006,36 @@ dependencies = [ "winapi", ] +[[package]] +name = "time" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb39ee79a6d8de55f48f2293a830e040392f1c5f16e336bdd1788cd0aadce07" +dependencies = [ + "deranged", + "itoa", + "libc", + "num_threads", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" + +[[package]] +name = "time-macros" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733d258752e9303d392b94b75230d07b0b9c489350c69b851fc6c065fde3e8f9" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1247,6 +1314,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/status_cloud/Cargo.toml b/status_cloud/Cargo.toml index 00ca130..f9bfa66 100644 --- a/status_cloud/Cargo.toml +++ b/status_cloud/Cargo.toml @@ -8,7 +8,9 @@ edition = "2021" [dependencies] chrono = "0.4.26" clap = { version = "4.4.0", features = ["derive"] } +log = "0.4.20" reqwest = { version = "0.11.18", features = ["blocking", "json"] } serde = { version = "1.0.184", features = ["serde_derive"] } serde_json = "1.0.105" +simplelog = "0.12.1" toml = "0.7.6" diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 085f076..a41d70b 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -1,103 +1,114 @@ -use serde::{Deserialize, Serialize}; -use reqwest::blocking::Client; - -use toml; use clap::Parser; - +use log::{debug, error, warn}; +use reqwest::blocking::Client; +use serde::{Deserialize, Serialize}; use std::process::Command; - -// TODO: logging - fn main() -> Result<(), Box> { - let args = CliArgs::parse(); - println!("Opening Config file: {}", args.config_file); - println!("File exists: {}", std::fs::metadata(args.config_file.clone()).is_ok()); + if args.debug { + let _ = simplelog::SimpleLogger::init( + simplelog::LevelFilter::Debug, + simplelog::Config::default(), + ); + } else { + let _ = simplelog::SimpleLogger::init( + simplelog::LevelFilter::Info, + simplelog::Config::default(), + ); + } + + debug!("Opening Config file: {}", args.config_file); + debug!( + "Config file exists: {}", + std::fs::metadata(args.config_file.clone()).is_ok() + ); let file_contents = match std::fs::read_to_string(args.config_file) { Ok(val) => val, Err(e) => { - println!("Could not read file: {}", e.to_string()); - return Ok(()); + error!("Could not read config file: {}", e.to_string()); + panic!("{}", e); } }; let cfg: Config = match toml::from_str(file_contents.as_str()) { Ok(val) => val, Err(e) => { - println!("Could not parse file: {}", e.to_string()); - return Ok(()); + error!("Could not parse config file: {}", e.to_string()); + panic!("{}", e); } }; - let mut body_content: String = format!("*Last Updated:* {} \n", chrono::Local::now().format("%D - %H:%M:%S")); - - let mut drives: Vec = vec![]; - - { - let rust_drives = match std::fs::read_dir("/dev/") { - Ok(val) => val, - Err(e) => { - println!("Error opening /dev/: {}", e.to_string()); - return Ok(()); - } - }; + let mut body_content: String = format!( + "*Last Updated:* {} \n", + chrono::Local::now().format("%D - %H:%M:%S") + ); - for path in rust_drives { - match path { - Ok(ref val) => { - let tmp = val.path().to_string_lossy().to_string(); - if tmp.starts_with("/dev/sd") { - if tmp.len() == 8 { - drives.push(tmp); - } - } - }, - _ => {} + let mut drives: Vec = vec![]; + + match std::fs::read_dir("/dev/") { + Ok(paths) => { + for path in paths.flatten() { + let tmp = path.path().to_string_lossy().to_string(); + if tmp.starts_with("/dev/sd") && tmp.len() == 8 { + drives.push(tmp); + } } } - } + Err(e) => { + error!("Error opening /dev/: {}", e.to_string()); + panic!("{}", e); + } + }; { let mut drive_temps: Vec = vec![]; for drive in drives { - let output = match Command::new(args.hddtemp_executable.clone()).arg(drive.clone()).output() { - Ok(val) => val, + let output = match Command::new(args.hddtemp_executable.clone()) + .arg(drive.clone()) + .output() + { + Ok(val) => String::from_utf8_lossy(&val.stdout).into_owned(), Err(e) => { - println!("Error running hddtemp: {}", e.to_string()); - println!("Drive was: '{}'", drive); - return Ok(()); + warn!("Error running hddtemp: {}", e.to_string()); + warn!("Drive was: '{}'", drive); + "".to_string() } }; - let tmp = String::from_utf8_lossy(&output.stdout).into_owned(); - - if !tmp.contains("sensor") { - drive_temps.push(tmp.replace("\n", "")); + if !output.contains("sensor") { + drive_temps.push(output.replace('\n', "?")); } else { - drive_temps.push(tmp[0..9].to_string() + " No Sensor"); + drive_temps.push(output[0..9].to_string() + " No Sensor"); } } body_content.push_str("## Hard Drive Temps\n"); body_content.push_str(drive_temps.join("\n").as_str()); } - Client::new() - .put(format!("https://{}/index.php/apps/notes/api/v1/notes/{}", cfg.server_url.clone(), cfg.note_id.clone())) + .put(format!( + "https://{}/index.php/apps/notes/api/v1/notes/{}", + cfg.server_url.clone(), + cfg.note_id.clone() + )) .header("Accept", "application/json") .header("Content-Type", "application/json") .basic_auth(cfg.user.clone(), Some(cfg.pswd.clone())) - .body(serde_json::to_string(&NoteUpdate {content: body_content}).unwrap()) - .send()?; + .body( + serde_json::to_string(&NoteUpdate { + content: body_content, + }) + .unwrap(), + ) + .send()?; - Ok(()) + Ok(()) } - #[derive(Serialize, Deserialize, Debug)] struct Note { id: usize, @@ -107,7 +118,7 @@ struct Note { title: String, category: String, content: String, - favorite: bool + favorite: bool, } #[derive(Serialize, Deserialize, Debug)] @@ -120,13 +131,13 @@ struct Config { user: String, pswd: String, note_id: String, - server_url: String + server_url: String, } impl Default for Config { fn default() -> Self { Self { - user: "".to_string(), + user: "".to_string(), pswd: "".to_string(), note_id: "".to_string(), server_url: "".to_string(), @@ -137,10 +148,13 @@ impl Default for Config { #[derive(Parser, Debug)] #[command(author, version, about, long_about=None)] struct CliArgs { - /// Path to config .toml file + /// Path to config .toml file #[arg(short, long)] config_file: String, #[arg(short, long)] hddtemp_executable: String, + + #[arg(short, long)] + debug: bool, } From bb30d0201324f2628e2be80163edb520305d113b Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:33:16 -0700 Subject: [PATCH 4/9] updated cargo hash --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 9d82ca9..5f6d9dc 100644 --- a/flake.nix +++ b/flake.nix @@ -62,7 +62,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` pname = "status_cloud"; version = "0.1.0"; buildAndTestSubdir = "status_cloud"; - cargoHash = "sha256-Z5JFg9OnCOO9jM/J5T9Tr64x21HOxNf8M4qJKiXiF54="; + cargoHash = "sha256-mMxHI/rU1Gd5UR+hZ+5+FnBrff8uF+SrEvGJT7wh5tI="; meta = meta // { description = "Server status saved to a nextcloud note service."; }; From f40a9264c3985bf609fe66fe13fee942ddb33f36 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:39:50 -0700 Subject: [PATCH 5/9] removed weird replace --- status_cloud/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index a41d70b..9a69dd1 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -80,7 +80,7 @@ fn main() -> Result<(), Box> { }; if !output.contains("sensor") { - drive_temps.push(output.replace('\n', "?")); + drive_temps.push(output.replace('\n', "")); } else { drive_temps.push(output[0..9].to_string() + " No Sensor"); } From 01e195b58cb0896fe058f04213a038505257c33d Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:50:36 -0700 Subject: [PATCH 6/9] added variable time update --- flake.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 5f6d9dc..919e99c 100644 --- a/flake.nix +++ b/flake.nix @@ -88,6 +88,12 @@ rust-project TODO: write shell script for automatically updating `cargoHash` The file path to the toml that contains user information secrets ''; }; + frequency = lib.mkOption { + type = lib.type.int; + description = lib.mdDoc '' + The number of minutes to wait between updates + ''; + }; }; config.systemd.services.status_cloud = let cfg = config.services.status_cloud; @@ -109,7 +115,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` in lib.mkIf cfg.enable { wantedBy = [ "timers.target" ]; partOf = [ "status_cloud.service" ]; - timerConfig.OnCalendar = [ "*:0/15" ]; + timerConfig.OnCalendar = [ "*:0/${builtins.toString cfg.frequncy}" ]; }; }; From a5dffdbbc392becaadaad3f1f0fa5a90416723c3 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:51:40 -0700 Subject: [PATCH 7/9] fixed typo --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 919e99c..9d2a650 100644 --- a/flake.nix +++ b/flake.nix @@ -115,7 +115,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` in lib.mkIf cfg.enable { wantedBy = [ "timers.target" ]; partOf = [ "status_cloud.service" ]; - timerConfig.OnCalendar = [ "*:0/${builtins.toString cfg.frequncy}" ]; + timerConfig.OnCalendar = [ "*:0/${builtins.toString cfg.frequency}" ]; }; }; From 97bb44f71716b5a4172e3a916e93b005d5f262e6 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 08:52:26 -0700 Subject: [PATCH 8/9] fixed typo --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 9d2a650..ae418eb 100644 --- a/flake.nix +++ b/flake.nix @@ -89,7 +89,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` ''; }; frequency = lib.mkOption { - type = lib.type.int; + type = lib.types.int; description = lib.mdDoc '' The number of minutes to wait between updates ''; From 11790783fccef0cc40e5b81dded65776c610aeff Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 26 Aug 2023 09:58:13 -0700 Subject: [PATCH 9/9] changed units to F --- status_cloud/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 9a69dd1..37b92f2 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -68,6 +68,7 @@ fn main() -> Result<(), Box> { for drive in drives { let output = match Command::new(args.hddtemp_executable.clone()) + .arg("--unit=F") .arg(drive.clone()) .output() {