Skip to content

Commit

Permalink
fix type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Nov 28, 2024
1 parent ec04624 commit b25a19d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mqtt-gateway"
version = "0.4.2"
version = "0.4.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
31 changes: 28 additions & 3 deletions src/data/openmqttgateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl OpenMqttGatewayParser {
}
}

if fields.contains_key("rssi") && fields.len() > 1 && tags.len() > base_tag_count {
if fields.contains_key("rssi") && fields.len() == 1 && tags.len() == base_tag_count {
tags.insert(String::from("type"),String::from("NONE"));
} else if !tags.contains_key("type") {
tags.insert(String::from("type"),String::from("UNKN"));
Expand Down Expand Up @@ -141,7 +141,7 @@ mod tests {
}

#[test]
fn test_parse_ignored() -> Result<()> {
fn test_parse_none_type() -> Result<()> {
let mut parser = OpenMqttGatewayParser::new();
let message = Message::new(
"blegateway/D12331654712/BTtoMQTT/283146C17616",
Expand All @@ -150,7 +150,32 @@ mod tests {
);
let result = parser.parse(&message)?;

assert!(result.is_none());
assert!(result.is_some());
let data = result.unwrap();
let tags = data.tags;
assert_eq!(tags.get("device").unwrap(), "283146C17616");
assert_eq!(tags.get("gateway").unwrap(), "D12331654712");
assert_eq!(tags.get("type").unwrap(), "NONE");

Ok(())
}

#[test]
fn test_parse_unknown_type() -> Result<()> {
let mut parser = OpenMqttGatewayParser::new();
let message = Message::new(
"blegateway/D12331654712/BTtoMQTT/283146C17616",
"{\"id\":\"28:31:46:C1:76:16\",\"rssi\":-92,\"name\":\"foo\"}",
QOS_1,
);
let result = parser.parse(&message)?;

assert!(result.is_some());
let data = result.unwrap();
let tags = data.tags;
assert_eq!(tags.get("device").unwrap(), "283146C17616");
assert_eq!(tags.get("gateway").unwrap(), "D12331654712");
assert_eq!(tags.get("type").unwrap(), "UNKN");

Ok(())
}
Expand Down

0 comments on commit b25a19d

Please sign in to comment.