diff --git a/README.md b/README.md index 872510b..5592771 100644 --- a/README.md +++ b/README.md @@ -130,10 +130,12 @@ Control messages: Status Messages: `/status offline` Sent when the neolink goes offline this is a LastWill message `/status disconnected` Sent when the camera goes offline -`/status/battery` Sent in reply to a `/query/battery` send an XML encoded version of the battery status +`/status/battery` Sent in reply to a `/query/battery` an XML encoded version of the battery status +`/status/pir` Sent in reply to a `/query/pir` an XML encoded version of the pir status Query Messages: `/query/battery` Request that the camera reports its battery level +`/query/pir` Request that the camera reports its pir status ### Pause diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 3ab3733..794631d 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -254,7 +254,7 @@ impl UdpPayloadSource { fn handle_ack(&mut self, ack: UdpAck) { let start = ack.packet_id; // -1 is the special case of no messages recieved - if start != 0xFFFFFFFF { + if start != 0xFFFFFFFF { self.sent.retain(|&k, _| k > start); for (idx, &value) in ack.payload.iter().enumerate() { diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 22d07e6..c2fa812 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -25,6 +25,9 @@ pub(crate) enum Messages { IRLedOff, IRLedAuto, Battery, + PIROn, + PIROff, + PIRQuery, Ptz(Direction), } @@ -349,6 +352,50 @@ impl MessageHandler { } } }, + Messages::PIRQuery => match self.camera.get_pirstate().await { + Err(_) => { + error!("Failed to get pir status"); + "FAIL".to_string() + } + Ok(pir_info) => { + let bytes_res = yaserde::ser::serialize_with_writer( + &pir_info, + vec![], + &Default::default(), + ); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => str, + Err(_) => { + error!("Failed to encode pir status"); + "FAIL".to_string() + } + }, + Err(_) => { + error!("Failed to serialise pir status"); + "FAIL".to_string() + } + } + } + }, + Messages::PIROn => { + if self.camera.pir_set(true).await.is_err() { + error!("Failed to turn on the pir"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::PIROff => { + if self.camera.pir_set(false).await.is_err() { + error!("Failed to turn on the pir"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } Messages::Ptz(direction) => { let (bc_direction, amount) = match direction { Direction::Up(amount) => (BcDirection::Up, amount), diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 32b3004..37d4faf 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -11,6 +11,7 @@ use log::*; /// Control messages: /// /// - `/control/led [on|off]` Turns status LED on/off +/// - `/control/pir [on|off]` Turns PIR on/off /// - `/control/ir [on|off|auto]` Turn IR lights on/off or automatically via light detection /// - `/control/reboot` Reboot the camera /// - `/control/ptz` [up|down|left|right|in|out] (amount) Control the PTZ movements, amount defaults to 32.0 @@ -20,10 +21,12 @@ use log::*; /// `/status offline` Sent when the neolink goes offline this is a LastWill message /// `/status disconnected` Sent when the camera goes offline /// `/status/battery` Sent in reply to a `/query/battery` +/// `/status/pir` Sent in reply to a `/query/pir` /// /// Query Messages: /// -/// `/query/battery` Request that the camera reports its battery level (Not Yet Implemented) +/// `/query/battery` Request that the camera reports its battery level +/// `/query/pir` Request that the camera reports its pir status /// /// /// # Usage @@ -193,7 +196,7 @@ async fn listen_on_camera( message: "on", } => { if event_cam.send_message(Messages::IRLedOn).await.is_err() { - error!("Failed to set camera status light off"); + error!("Failed to set camera status light on"); } } MqttReplyRef { @@ -255,6 +258,22 @@ async fn listen_on_camera( error!("No PTZ Direction given. Please add up/down/left/right/in/out"); } } + MqttReplyRef { + topic: "control/pir", + message: "on", + } => { + if event_cam.send_message(Messages::PIROn).await.is_err() { + error!("Failed to set pir on"); + } + } + MqttReplyRef { + topic: "control/pir", + message: "off", + } => { + if event_cam.send_message(Messages::PIROff).await.is_err() { + error!("Failed to set pir off"); + } + } MqttReplyRef { topic: "query/battery", .. @@ -264,7 +283,17 @@ async fn listen_on_camera( error!("Failed to send battery status reply"); } } - Err(_) => error!("Failed to set camera status light off"), + Err(_) => error!("Failed to get battery status"), + }, + MqttReplyRef { + topic: "query/pir", .. + } => match event_cam.send_message_with_reply(Messages::PIRQuery).await { + Ok(reply) => { + if mqtt.send_message("status/pir", &reply, false).is_err() { + error!("Failed to send pir status reply"); + } + } + Err(_) => error!("Failed to get pir status"), }, _ => {} }