From 0b351333b53c500543b9a8f0ef3df6af551ce33f Mon Sep 17 00:00:00 2001 From: Gordy French Date: Fri, 15 Nov 2024 21:28:29 -0800 Subject: [PATCH] some refactoring --- Cargo.lock | 155 ++++++--------------------------------- Cargo.toml | 3 +- src/commands/run.rs | 4 +- src/commands/simulate.rs | 48 ++++-------- src/main.rs | 13 ++-- 5 files changed, 46 insertions(+), 177 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a5714b9..931399e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,21 +17,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.6.18" @@ -108,12 +93,6 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - [[package]] name = "byteorder" version = "1.5.0" @@ -141,20 +120,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets", -] - [[package]] name = "clap" version = "4.5.21" @@ -309,29 +274,6 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -345,12 +287,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] -name = "js-sys" -version = "0.3.72" +name = "jiff" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d9d414fc817d3e3d62b2598616733f76c4cc74fbac96069674739b881295c8" +dependencies = [ + "jiff-tzdb-platform", + "windows-sys 0.59.0", +] + +[[package]] +name = "jiff-tzdb" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91335e575850c5c4c673b9bd467b0e025f164ca59d0564f69d0c2ee0ffad4653" + +[[package]] +name = "jiff-tzdb-platform" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "9835f0060a626fe59f160437bc725491a6af23133ea906500027d1bd2f8f4329" dependencies = [ - "wasm-bindgen", + "jiff-tzdb", ] [[package]] @@ -406,9 +364,10 @@ dependencies = [ name = "mqtt_pid" version = "0.1.0" dependencies = [ - "chrono", "clap", "csv", + "jiff", + "num-traits", "pid", "rand", "rumqttc", @@ -433,12 +392,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - [[package]] name = "openssl-probe" version = "0.1.5" @@ -818,70 +771,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" - -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 1cef1b0..61afd7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,10 @@ lto = true codegen-units = 1 [dependencies] -chrono = "0.4.38" clap = { version = "4.5.20", features = ["derive", "env"] } csv = "1.3.1" +jiff = "0.1.14" +num-traits = "0.2.19" pid = "4.0.0" rand = "0.8.5" rumqttc = "0.24.0" diff --git a/src/commands/run.rs b/src/commands/run.rs index a2d26d4..bd451b7 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -22,7 +22,7 @@ pub struct RunArgs { } impl RunArgs { - pub async fn run(self: RunArgs, mut pid: Pid) -> Result<(), Box> { + pub async fn run(self: RunArgs, mut pid: Pid) -> Result<(), Box> { let mut mqttoptions = MqttOptions::new("mqtt_pid", self.mqtt_host, self.mqtt_port); mqttoptions.set_keep_alive(Duration::from_secs(5)); mqttoptions.set_max_packet_size(2000, 1000); @@ -48,7 +48,7 @@ impl RunArgs { let last_input_value = *rx.borrow_and_update(); let output = pid - .next_control_output(last_input_value as f32) + .next_control_output(last_input_value as f64) .output .clamp(0.0, 100.0) .round() as u8; diff --git a/src/commands/simulate.rs b/src/commands/simulate.rs index c2e43ce..da9ec99 100644 --- a/src/commands/simulate.rs +++ b/src/commands/simulate.rs @@ -1,7 +1,7 @@ use std::io::{BufRead, BufReader}; -use chrono::{DateTime, Duration, TimeZone, Timelike, Utc}; use clap::Parser; +use jiff::{Timestamp, ToSpan}; use pid::Pid; #[derive(Parser, Clone)] @@ -11,14 +11,14 @@ pub struct SimulateArgs { } struct Event { - value: f32, - timestamp: DateTime, + value: f64, + timestamp: Timestamp, } impl SimulateArgs { pub async fn run( self: SimulateArgs, - mut pid: Pid, + mut pid: Pid, ) -> Result<(), Box> { // Read the CSV file let mut events: Vec = Vec::new(); @@ -31,10 +31,12 @@ impl SimulateArgs { if parts.len() != 2 { continue; // Skip invalid lines } - let value: f32 = parts[0].parse()?; + let value = parts[0].parse()?; let timestamp_str = parts[1]; - let timestamp = timestamp_str.parse::>()?; - events.push(Event { value, timestamp }); + events.push(Event { + value, + timestamp: timestamp_str.parse()?, + }); } // Sort events by timestamp @@ -48,28 +50,8 @@ impl SimulateArgs { let first_event = events.first().unwrap(); let last_event = events.last().unwrap(); - let start_time = first_event - .timestamp - .date_naive() - .and_hms_opt( - first_event.timestamp.hour(), - first_event.timestamp.minute(), - first_event.timestamp.second(), - ) - .unwrap(); - - let mut end_time = last_event - .timestamp - .date_naive() - .and_hms_opt( - last_event.timestamp.hour(), - last_event.timestamp.minute(), - last_event.timestamp.second(), - ) - .unwrap(); - - // Include the last second - end_time += Duration::seconds(1); + let start_time = first_event.timestamp; + let end_time = last_event.timestamp + 1.seconds(); let mut current_value = 0.0; let mut event_index = 0; @@ -78,19 +60,17 @@ impl SimulateArgs { while t_s < end_time { // Update current_value based on events - while event_index < events.len() - && events[event_index].timestamp <= Utc.from_utc_datetime(&t_s) - { + while event_index < events.len() && events[event_index].timestamp <= t_s { current_value = events[event_index].value; event_index += 1; } let output = pid.next_control_output(current_value).output; let output = output.clamp(0.0, 100.0); - println!("{},{},{}", t_s, current_value, output); + println!("{},{},{}", t_s.as_second(), current_value, output); // Increment time by one second - t_s += Duration::seconds(1); + t_s += 1.seconds(); } Ok(()) diff --git a/src/main.rs b/src/main.rs index 456f02e..ded1ee6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ struct Args { kd: f32, #[command(subcommand)] - command: Option, + command: Commands, } #[derive(Subcommand, Clone)] @@ -33,14 +33,13 @@ enum Commands { #[tokio::main] async fn main() { let args = Args::parse(); - let mut pid = Pid::new(args.setpoint, 100.0); + let mut pid = Pid::::new(args.setpoint, 100.0); pid.p(args.kp, 100.0); pid.i(args.ki, 100.0); pid.d(args.kd, 100.0); - match args.command.clone().unwrap() { - Commands::Run(command) => command.run(pid).await, - Commands::Simulate(command) => command.run(pid).await, - } - .unwrap(); + match args.command { + Commands::Run(command) => command.run(pid).await.unwrap(), + Commands::Simulate(command) => command.run(pid).await.unwrap(), + }; }