Skip to content

Commit

Permalink
fix: mqtt: remove topic check
Browse files Browse the repository at this point in the history
This check will never be true, as the topic is always either screens or
screens/${id}.

Record the topic in the current span, so it's also included as fields in
log statements, and log the raw payload in case it couldn't be parsed as
a Command.
  • Loading branch information
flokli committed Jul 16, 2024
1 parent 4d22089 commit 4b9e85a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

45 changes: 43 additions & 2 deletions Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,43 @@ rec {
];

};
"bstr" = rec {
crateName = "bstr";
version = "1.9.1";
edition = "2021";
sha256 = "01ipr5rncw3kf4dyc1p2g00njn1df2b0xpviwhb8830iv77wbvq5";
authors = [
"Andrew Gallant <jamslam@gmail.com>"
];
dependencies = [
{
name = "memchr";
packageId = "memchr";
usesDefaultFeatures = false;
}
{
name = "regex-automata";
packageId = "regex-automata 0.4.7";
optional = true;
usesDefaultFeatures = false;
features = [ "dfa-search" ];
}
{
name = "serde";
packageId = "serde";
optional = true;
usesDefaultFeatures = false;
}
];
features = {
"alloc" = [ "memchr/alloc" "serde?/alloc" ];
"default" = [ "std" "unicode" ];
"serde" = [ "dep:serde" ];
"std" = [ "alloc" "memchr/std" "serde?/std" ];
"unicode" = [ "dep:regex-automata" ];
};
resolvedDefaultFeatures = [ "alloc" "default" "std" "unicode" ];
};
"byteorder" = rec {
crateName = "byteorder";
version = "1.5.0";
Expand Down Expand Up @@ -1691,6 +1728,10 @@ rec {
];
src = lib.cleanSourceWith { filter = sourceFilter; src = ./.; };
dependencies = [
{
name = "bstr";
packageId = "bstr";
}
{
name = "clap";
packageId = "clap";
Expand Down Expand Up @@ -5022,7 +5063,7 @@ rec {
"unicode-script" = [ "regex-syntax?/unicode-script" ];
"unicode-segment" = [ "regex-syntax?/unicode-segment" ];
};
resolvedDefaultFeatures = [ "alloc" "meta" "nfa-pikevm" "nfa-thompson" "std" "syntax" "unicode-case" "unicode-perl" "unicode-word-boundary" ];
resolvedDefaultFeatures = [ "alloc" "dfa-search" "meta" "nfa-pikevm" "nfa-thompson" "std" "syntax" "unicode-case" "unicode-perl" "unicode-word-boundary" ];
};
"regex-syntax 0.6.29" = rec {
crateName = "regex-syntax";
Expand Down Expand Up @@ -5633,7 +5674,7 @@ rec {
"derive" = [ "serde_derive" ];
"serde_derive" = [ "dep:serde_derive" ];
};
resolvedDefaultFeatures = [ "default" "derive" "serde_derive" "std" ];
resolvedDefaultFeatures = [ "alloc" "default" "derive" "serde_derive" "std" ];
};
"serde_derive" = rec {
crateName = "serde_derive";
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bstr = "1.9.1"
clap = { version = "4.5.9", features = ["derive"] }
color-eyre = "0.6.3"
eyre = "0.6.12"
Expand Down
18 changes: 10 additions & 8 deletions src/mqtt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{sync::mpsc::Sender, thread, time::Duration};

use bstr::BStr;
use fossbeamer::Command;
use rumqttc::{Client, ClientError, MqttOptions, Packet, Publish};
use tracing::{debug, warn};
use std::{sync::mpsc::Sender, thread, time::Duration};
use tracing::{debug, info, warn, Span};

pub(crate) struct Listener {
pub id: String,
Expand All @@ -28,14 +28,16 @@ impl Listener {
payload,
..
})) => {
if topic == "commands" {
if let Ok(command) = serde_json::from_slice::<Command>(&payload) {
debug!(?command, "received command");
Span::current().record("topic", &topic);
match serde_json::from_slice::<Command>(&payload) {
Ok(command) => {
info!(?command, "received command");

self.sender.send(command).unwrap();
}
} else {
debug!(?topic, "received other topic");
Err(e) => {
warn!(err=%e, payload=%BStr::new(&payload), "received payload that couldn't be parsed");
}
}
}
rumqttc::Event::Incoming(incoming) => {
Expand Down

0 comments on commit 4b9e85a

Please sign in to comment.