diff --git a/Cargo.lock b/Cargo.lock index bebc94c..bef1804 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.18" @@ -203,6 +212,29 @@ dependencies = [ "memchr", ] +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "flume" version = "0.11.1" @@ -274,6 +306,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -366,7 +404,9 @@ version = "0.1.0" dependencies = [ "clap", "csv", + "env_logger", "jiff", + "log", "pid", "rand", "rumqttc", @@ -475,6 +515,35 @@ dependencies = [ "getrandom", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "ring" version = "0.17.8" diff --git a/Cargo.toml b/Cargo.toml index 2c44bfa..9b6b3d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,9 @@ codegen-units = 1 [dependencies] clap = { version = "4.5.20", features = ["derive"] } csv = "1.3.1" +env_logger = "0.11.5" jiff = "0.1.14" +log = "0.4.22" 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 492c46b..d8ef83d 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,4 +1,7 @@ use clap::Parser; +use log::debug; +use log::error; +use log::info; use pid::Pid; use rumqttc::{AsyncClient, MqttOptions, QoS}; use std::time::Duration; @@ -29,7 +32,8 @@ impl RunArgs { let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); if let Err(e) = client.subscribe(self.input_topic, QoS::AtMostOnce).await { - panic!("Failed to subscribe to MQTT topic: {}", e); + error!("Failed to subscribe to MQTT topic: {}", e); + return Err(Box::new(e)); } let (tx, mut rx) = watch::channel(0); @@ -53,6 +57,7 @@ impl RunArgs { .round() as u8; if last_output_value != output { + debug!("Emitting new output value: {}", output); last_output_value = output; if let Err(e) = client .publish( @@ -63,28 +68,31 @@ impl RunArgs { ) .await { - eprintln!("Failed to publish to MQTT topic: {}", e); + error!("Failed to publish to MQTT topic: {}", e); } } } }); } + info!("Topic subscribed; waiting for events."); + loop { let notification = eventloop.poll().await; match notification { Ok(rumqttc::Event::Incoming(rumqttc::Packet::Publish(publish))) => { if let Ok(payload) = std::str::from_utf8(&publish.payload) { if let Ok(value) = payload.parse::() { + debug!("Received new input value: {}", value); if let Err(e) = tx.send(value) { - eprintln!("Failed to send via channel: {}", e); + error!("Failed to send via channel: {}", e); } } } } Ok(_) => {} Err(e) => { - eprintln!("MQTT error: {:?}", e); + error!("MQTT error: {:?}", e); } } } diff --git a/src/main.rs b/src/main.rs index b3bf454..de354d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,8 @@ enum Commands { #[tokio::main(flavor = "multi_thread", worker_threads = 2)] async fn main() { + env_logger::init(); + let args = Args::parse(); let mut pid = Pid::::new(args.setpoint, 100.0); pid.p(args.kp, 100.0);