Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/mqtt: restructure a bit, add timeouts #14

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/mqtt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{sync::mpsc::Sender, thread};
use std::{sync::mpsc::Sender, thread, time::Duration};

use fossbeamer::Command;
use rumqttc::{Client, ClientError, MqttOptions, Packet, Publish};
use tracing::{debug, warn};

pub(crate) struct Listener {
pub id: String,
Expand All @@ -20,20 +21,34 @@ impl Listener {

thread::spawn(move || {
for event in connection.iter() {
println!("{:?}", event);

if let Ok(rumqttc::Event::Incoming(Packet::Publish(Publish {
topic,
payload,
..
}))) = event
{
if topic == "commands" {
if let Ok(command) = serde_json::from_slice::<Command>(&payload) {
println!("{:?}", command);

self.sender.send(command).unwrap();
match event {
Ok(event) => match event {
rumqttc::Event::Incoming(Packet::Publish(Publish {
topic,
payload,
..
})) => {
if topic == "commands" {
if let Ok(command) = serde_json::from_slice::<Command>(&payload) {
debug!(?command, "received command");

self.sender.send(command).unwrap();
}
} else {
debug!(?topic, "received other topic");
}
}
rumqttc::Event::Incoming(incoming) => {
debug!(?incoming, "other incoming event");
}
rumqttc::Event::Outgoing(out) => {
debug!(?out, "outgoing event");
}
},
Err(e) => {
warn!(err=%e, "connection error");
// sleep a bit
std::thread::sleep(Duration::from_secs(5));
}
}
}
Expand Down
Loading