Skip to content

Commit

Permalink
Better error handling and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gordyf committed Nov 19, 2024
1 parent b53bc18 commit ba10069
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ impl RunArgs {
mqttoptions.set_max_packet_size(2000, 1000);

let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
client
.subscribe(self.input_topic, QoS::AtMostOnce)
.await
.unwrap();
if let Err(e) = client.subscribe(self.input_topic, QoS::AtMostOnce).await {
panic!("Failed to subscribe to MQTT topic: {}", e);
}

let (tx, mut rx) = watch::channel(0);

Expand All @@ -55,28 +54,38 @@ impl RunArgs {

if last_output_value != output {
last_output_value = output;
client
if let Err(e) = client
.publish(
output_topic.clone(),
QoS::AtLeastOnce,
false,
output.to_string(),
)
.await
.unwrap();
{
eprintln!("Failed to publish to MQTT topic: {}", e);
}
}
}
});
}

loop {
let notification = eventloop.poll().await.unwrap();
if let rumqttc::Event::Incoming(rumqttc::Packet::Publish(publish)) = notification {
if let Ok(payload) = std::str::from_utf8(&publish.payload) {
if let Ok(value) = payload.parse::<u16>() {
tx.send(value).unwrap();
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>() {
if let Err(e) = tx.send(value) {
eprintln!("Failed to send via channel: {}", e);
}
}
}
}
Ok(_) => {}
Err(e) => {
eprintln!("MQTT error: {:?}", e);
}
}
}
}
Expand Down

0 comments on commit ba10069

Please sign in to comment.