From 3467951c5ca38835b373fd841e03a62e0fbb7b50 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 7 Jun 2023 17:36:42 +0700 Subject: [PATCH] Handle message that don't reply on success --- .../core/src/bc_protocol/floodlight_status.rs | 25 +++++++++----- crates/core/src/bc_protocol/ledstate.rs | 27 +++++++++------ crates/core/src/bc_protocol/pirstate.rs | 33 +++++++++++-------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/crates/core/src/bc_protocol/floodlight_status.rs b/crates/core/src/bc_protocol/floodlight_status.rs index d682a40..27f87ef 100644 --- a/crates/core/src/bc_protocol/floodlight_status.rs +++ b/crates/core/src/bc_protocol/floodlight_status.rs @@ -77,18 +77,25 @@ impl BcCamera { }; sub_set.send(get).await?; - let msg = sub_set.recv().await?; - if let BcMeta { - response_code: 200, .. - } = msg.meta + if let Ok(reply) = + tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await { - Ok(()) + let msg = reply?; + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "The camera did not accept the Floodlight manual state", + }) + } } else { - Err(Error::UnintelligibleReply { - reply: std::sync::Arc::new(Box::new(msg)), - why: "The camera did not accept the Floodlight manual state", - }) + // Some cameras seem to just not send a reply on success, so after 500ms we return Ok + Ok(()) } } } diff --git a/crates/core/src/bc_protocol/ledstate.rs b/crates/core/src/bc_protocol/ledstate.rs index 472a8ea..7acca2e 100644 --- a/crates/core/src/bc_protocol/ledstate.rs +++ b/crates/core/src/bc_protocol/ledstate.rs @@ -83,18 +83,25 @@ impl BcCamera { }; sub_set.send(get).await?; - let msg = sub_set.recv().await?; - - if let BcMeta { - response_code: 200, .. - } = msg.meta + if let Ok(reply) = + tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await { - Ok(()) + let msg = reply?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "The camera did not except the LEDState xml", + }) + } } else { - Err(Error::UnintelligibleReply { - reply: std::sync::Arc::new(Box::new(msg)), - why: "The camera did not except the LEDState xml", - }) + // Some cameras seem to just not send a reply on success, so after 500ms we return Ok + Ok(()) } } diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index aa340e8..bbd223b 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -94,21 +94,28 @@ impl BcCamera { }; sub_set.send(get).await?; - let msg = sub_set.recv().await?; - if msg.meta.response_code != 200 { - return Err(Error::CameraServiceUnavaliable); - } - - if let BcMeta { - response_code: 200, .. - } = msg.meta + if let Ok(reply) = + tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await { - Ok(()) + let msg = reply?; + if msg.meta.response_code != 200 { + return Err(Error::CameraServiceUnavaliable); + } + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "The camera did not except the RfAlarmCfg xml", + }) + } } else { - Err(Error::UnintelligibleReply { - reply: std::sync::Arc::new(Box::new(msg)), - why: "The camera did not except the RfAlarmCfg xml", - }) + // Some cameras seem to just not send a reply on success, so after 500ms we return Ok + Ok(()) } }