Skip to content

Commit

Permalink
Logging!
Browse files Browse the repository at this point in the history
  • Loading branch information
gordyf committed Nov 19, 2024
1 parent ba10069 commit e499813
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 12 additions & 4 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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::<u16>() {
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);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f64>::new(args.setpoint, 100.0);
pid.p(args.kp, 100.0);
Expand Down

0 comments on commit e499813

Please sign in to comment.