Skip to content

Commit 88fdbac

Browse files
committed
handle Shelly data parsing error
1 parent b25a19d commit 88fdbac

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/data/shelly/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ fn handle_message<'a, T: Deserialize<'a> + Clone + Debug + Timestamped + Typenam
131131
) {
132132
let location = msg.topic().split("/").nth(1).unwrap();
133133
let channel = msg.topic().split(":").last().unwrap();
134-
let result: Option<T> = shelly::parse(msg).unwrap();
134+
let parse_result = shelly::parse(msg);
135+
if parse_result.is_err() {
136+
warn!("Shelly parse error: {:?} on '{}'", parse_result.err(), msg.payload_str());
137+
return;
138+
}
139+
let result: Option<T> = parse_result.unwrap();
135140
if let Some(data) = result {
136141
debug!("Shelly {}:{}: {:?}", location, channel, data);
137142

@@ -246,6 +251,25 @@ mod tests {
246251
Ok(())
247252
}
248253

254+
#[test]
255+
fn test_handle_message_with_parse_error() -> Result<()> {
256+
let (tx, rx) = sync_channel(100);
257+
let txs = vec![tx];
258+
259+
let mut logger = ShellyLogger::new(txs);
260+
261+
let message = Message::new(
262+
"shellies/bedroom-curtain/status/cover:0",
263+
"{\"id\":0, \"source\":\"limit_switch\", \"state\":\"open\",\
264+
\"apower\":0.0}",
265+
QOS_1,
266+
);
267+
logger.check_message(&message);
268+
269+
assert!(next(&rx).is_err());
270+
271+
Ok(())
272+
}
249273
#[test]
250274
fn test_parse_switch_status() -> Result<()> {
251275
let message = Message::new("shellies/loo-fan/status/switch:0", "{\"id\":0, \"source\":\"timer\", \"output\":false, \"apower\":0.0, \"voltage\":226.5, \"current\":3.1, \"aenergy\":{\"total\":1094.865,\"by_minute\":[0.000,0.000,0.000],\"minute_ts\":1703415907},\"temperature\":{\"tC\":36.4, \"tF\":97.5}}", QOS_1);

0 commit comments

Comments
 (0)