From 9f1bcd6f555f9924d4cd43d4432618f3470ca932 Mon Sep 17 00:00:00 2001 From: Joris Bremond Date: Mon, 9 May 2022 22:38:28 +0200 Subject: [PATCH 1/3] Add new motion command to set PIR sensor ON or OFF on battery powered camera tested on reolink argus pro --- crates/core/src/bc/model.rs | 4 + crates/core/src/bc/xml.rs | 26 +++++ crates/core/src/bc_protocol.rs | 2 + crates/core/src/bc_protocol/motionstate.rs | 118 +++++++++++++++++++++ src/cmdline.rs | 1 + src/main.rs | 4 + src/motion/cmdline.rs | 23 ++++ src/motion/mod.rs | 34 ++++++ 8 files changed, 212 insertions(+) create mode 100644 crates/core/src/bc_protocol/motionstate.rs create mode 100644 src/motion/cmdline.rs create mode 100644 src/motion/mod.rs diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index b71bd3b..92c43cd 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -18,6 +18,10 @@ pub const MSG_ID_TALKRESET: u32 = 11; pub const MSG_ID_REBOOT: u32 = 23; /// Version messages have this ID pub const MSG_ID_VERSION: u32 = 80; +/// Getting PIR status messages have this ID +pub const MSG_ID_GET_MOTION_ALARM: u32 = 212; +/// Setting PIR status messages have this ID +pub const MSG_ID_START_MOTION_ALARM: u32 = 213; /// Ping messages have this ID pub const MSG_ID_PING: u32 = 93; /// General system info messages have this ID diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 2ce5a5c..3945131 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -63,6 +63,9 @@ pub struct BcXml { /// Sent as part of the TalkConfig to prepare the camera for audio talk-back #[yaserde(rename = "TalkConfig")] pub talk_config: Option, + /// rfAlarmCfg xml is sent or recieved as part of the PIR get/setting + #[yaserde(rename = "rfAlarmCfg")] + pub rf_alarm_cfg: Option, /// Revieced as part of the TalkAbility request #[yaserde(rename = "TalkAbility")] pub talk_ability: Option, @@ -302,6 +305,29 @@ pub struct LedState { pub light_state: String, } +/// rfAlarmCfg xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct RfAlarmCfg { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Rfid + #[yaserde(rename = "rfID")] + pub rf_id: u8, + /// PIR status + pub enable: u8, + /// PIR sensitivity + pub sensitivity: u8, + /// PIR sensivalue + pub sensiValue: u8, + /// reduce False alarm boolean + pub reduceFalseAlarm: u8, + /// XML time block for all week days + pub timeBlockList: String, +} + + + /// TalkConfig xml #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] pub struct TalkConfig { diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 24c8fca..78627aa 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -13,6 +13,7 @@ mod login; mod logout; mod ping; mod reboot; +mod motionstate; mod resolution; mod stream; mod talk; @@ -24,6 +25,7 @@ use bc::model::*; pub(crate) use connection::*; pub use errors::Error; pub use ledstate::LightState; +pub use motionstate::MotionState; pub use resolution::*; pub use stream::{Stream, StreamOutput, StreamOutputError}; diff --git a/crates/core/src/bc_protocol/motionstate.rs b/crates/core/src/bc_protocol/motionstate.rs new file mode 100644 index 0000000..01dca49 --- /dev/null +++ b/crates/core/src/bc_protocol/motionstate.rs @@ -0,0 +1,118 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::{model::*, xml::*}; + +impl BcCamera { + /// Get the [RfAlarmCfg] xml which contains the motion status of the camera + pub fn get_motionstate(&mut self) -> Result { + let connection = self + .connection + .as_ref() + .expect("Must be connected to get time"); + let sub_get = connection.subscribe(MSG_ID_GET_MOTION_ALARM)?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_GET_MOTION_ALARM, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: None, + }), + }; + + sub_get.send(get)?; + let msg = sub_get.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + rf_alarm_cfg: Some(motionstate), + .. + })), + .. + }) = msg.body + { + Ok(motionstate) + } else { + Err(Error::UnintelligibleReply { + reply: msg, + why: "Expected MotionSate xml but it was not recieved", + }) + } + } + + /// Set the PIR sensor using the [RfAlarmCfg] xml + pub fn set_motionstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { + let connection = self + .connection + .as_ref() + .expect("Must be connected to get time"); + let sub_set = connection.subscribe(MSG_ID_START_MOTION_ALARM)?; + + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_START_MOTION_ALARM, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: Some(BcPayloads::BcXml(BcXml { + rf_alarm_cfg: Some(rf_alarm_cfg), + ..Default::default() + })), + }), + }; + + sub_set.send(get)?; + let msg = sub_set.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: msg, + why: "The camera did not except the RfAlarmCfg xml", + }) + } + } + + + + /// This is a convience function to control the PIR status + /// True is on and false is off + pub fn motion_set(&mut self, state: bool) -> Result<()> { + let mut motion_state = self.get_motionstate()?; + // println!("{:?}", motion_state); + motion_state.enable = match state { + true => 1, + false => 0, + }; + self.set_motionstate(motion_state)?; + Ok(()) + } +} + +/// Turn PIR ON or OFF +pub enum MotionState { + /// Turn the PIR on + On, + /// Turn the PIR off + Off, +} diff --git a/src/cmdline.rs b/src/cmdline.rs index 1fb1f18..089ded0 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -20,5 +20,6 @@ pub enum Command { Rtsp(super::rtsp::Opt), StatusLight(super::statusled::Opt), Reboot(super::reboot::Opt), + Motion(super::motion::Opt), Talk(super::talk::Opt), } diff --git a/src/main.rs b/src/main.rs index ff6a0fb..fb3e01b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use validator::Validate; mod cmdline; mod config; mod reboot; +mod motion; mod rtsp; mod statusled; mod talk; @@ -64,6 +65,9 @@ fn main() -> Result<()> { Some(Command::Reboot(opts)) => { reboot::main(opts, config)?; } + Some(Command::Motion(opts)) => { + motion::main(opts, config)?; + } Some(Command::Talk(opts)) => { talk::main(opts, config)?; } diff --git a/src/motion/cmdline.rs b/src/motion/cmdline.rs new file mode 100644 index 0000000..17406d7 --- /dev/null +++ b/src/motion/cmdline.rs @@ -0,0 +1,23 @@ +use anyhow::{anyhow, Result}; +use structopt::StructOpt; + +fn onoff_parse(src: &str) -> Result { + match src { + "true" | "on" | "yes" => Ok(true), + "false" | "off" | "no" => Ok(false), + _ => Err(anyhow!( + "Could not understand {}, check your input, should be true/false, on/off or yes/no", + src + )), + } +} + +/// The motion command will control the motion status off the camera +#[derive(StructOpt, Debug)] +pub struct Opt { + /// The name of the camera. Must be a name in the config + pub camera: String, + /// Whether to turn the PIR ON or OFF + #[structopt(parse(try_from_str = onoff_parse), name = "on|off")] + pub on: bool, +} diff --git a/src/motion/mod.rs b/src/motion/mod.rs new file mode 100644 index 0000000..2117b19 --- /dev/null +++ b/src/motion/mod.rs @@ -0,0 +1,34 @@ +/// +/// # Neolink Motion +/// +/// This module handles the controls of the motion alarm +/// +/// +/// # Usage +/// +/// ```bash +/// # To turn the motion on +/// neolink motion --config=config.toml CameraName on +/// # Or off +/// neolink motion --config=config.toml CameraName off +/// ``` +/// +use anyhow::{Context, Result}; + +mod cmdline; + +use super::config::Config; +use crate::utils::find_and_connect; +pub(crate) use cmdline::Opt; + +/// Entry point for the motion subcommand +/// +/// Opt is the command line options +pub(crate) fn main(opt: Opt, config: Config) -> Result<()> { + let mut camera = find_and_connect(&config, &opt.camera)?; + + camera + .motion_set(opt.on) + .context("Unable to set camera motion state")?; + Ok(()) +} From 2b5670517203ca63abd9a9800873a12aff2371ce Mon Sep 17 00:00:00 2001 From: Joris Bremond Date: Sat, 14 May 2022 14:23:01 +0200 Subject: [PATCH 2/3] Rename command from motion to pir --- crates/core/src/bc/model.rs | 4 +-- crates/core/src/bc_protocol.rs | 4 +-- .../{motionstate.rs => pirstate.rs} | 32 +++++++++---------- src/cmdline.rs | 2 +- src/main.rs | 6 ++-- src/{motion => pir}/cmdline.rs | 2 +- src/{motion => pir}/mod.rs | 16 +++++----- 7 files changed, 33 insertions(+), 33 deletions(-) rename crates/core/src/bc_protocol/{motionstate.rs => pirstate.rs} (75%) rename src/{motion => pir}/cmdline.rs (90%) rename src/{motion => pir}/mod.rs (53%) diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 92c43cd..6f1b8be 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -19,9 +19,9 @@ pub const MSG_ID_REBOOT: u32 = 23; /// Version messages have this ID pub const MSG_ID_VERSION: u32 = 80; /// Getting PIR status messages have this ID -pub const MSG_ID_GET_MOTION_ALARM: u32 = 212; +pub const MSG_ID_GET_PIR_ALARM: u32 = 212; /// Setting PIR status messages have this ID -pub const MSG_ID_START_MOTION_ALARM: u32 = 213; +pub const MSG_ID_START_PIR_ALARM: u32 = 213; /// Ping messages have this ID pub const MSG_ID_PING: u32 = 93; /// General system info messages have this ID diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 78627aa..3421d5a 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -13,7 +13,7 @@ mod login; mod logout; mod ping; mod reboot; -mod motionstate; +mod pirstate; mod resolution; mod stream; mod talk; @@ -25,7 +25,7 @@ use bc::model::*; pub(crate) use connection::*; pub use errors::Error; pub use ledstate::LightState; -pub use motionstate::MotionState; +pub use pirstate::PirState; pub use resolution::*; pub use stream::{Stream, StreamOutput, StreamOutputError}; diff --git a/crates/core/src/bc_protocol/motionstate.rs b/crates/core/src/bc_protocol/pirstate.rs similarity index 75% rename from crates/core/src/bc_protocol/motionstate.rs rename to crates/core/src/bc_protocol/pirstate.rs index 01dca49..6f61dfd 100644 --- a/crates/core/src/bc_protocol/motionstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -2,16 +2,16 @@ use super::{BcCamera, Error, Result, RX_TIMEOUT}; use crate::bc::{model::*, xml::*}; impl BcCamera { - /// Get the [RfAlarmCfg] xml which contains the motion status of the camera - pub fn get_motionstate(&mut self) -> Result { + /// Get the [RfAlarmCfg] xml which contains the PIR status of the camera + pub fn get_pirstate(&mut self) -> Result { let connection = self .connection .as_ref() .expect("Must be connected to get time"); - let sub_get = connection.subscribe(MSG_ID_GET_MOTION_ALARM)?; + let sub_get = connection.subscribe(MSG_ID_GET_PIR_ALARM)?; let get = Bc { meta: BcMeta { - msg_id: MSG_ID_GET_MOTION_ALARM, + msg_id: MSG_ID_GET_PIR_ALARM, channel_id: self.channel_id, msg_num: self.new_message_num(), response_code: 0, @@ -33,32 +33,32 @@ impl BcCamera { if let BcBody::ModernMsg(ModernMsg { payload: Some(BcPayloads::BcXml(BcXml { - rf_alarm_cfg: Some(motionstate), + rf_alarm_cfg: Some(pirstate), .. })), .. }) = msg.body { - Ok(motionstate) + Ok(pirstate) } else { Err(Error::UnintelligibleReply { reply: msg, - why: "Expected MotionSate xml but it was not recieved", + why: "Expected PirSate xml but it was not recieved", }) } } /// Set the PIR sensor using the [RfAlarmCfg] xml - pub fn set_motionstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { + pub fn set_pirstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { let connection = self .connection .as_ref() .expect("Must be connected to get time"); - let sub_set = connection.subscribe(MSG_ID_START_MOTION_ALARM)?; + let sub_set = connection.subscribe(MSG_ID_START_PIR_ALARM)?; let get = Bc { meta: BcMeta { - msg_id: MSG_ID_START_MOTION_ALARM, + msg_id: MSG_ID_START_PIR_ALARM, channel_id: self.channel_id, msg_num: self.new_message_num(), response_code: 0, @@ -97,20 +97,20 @@ impl BcCamera { /// This is a convience function to control the PIR status /// True is on and false is off - pub fn motion_set(&mut self, state: bool) -> Result<()> { - let mut motion_state = self.get_motionstate()?; - // println!("{:?}", motion_state); - motion_state.enable = match state { + pub fn pir_set(&mut self, state: bool) -> Result<()> { + let mut pir_state = self.get_pirstate()?; + // println!("{:?}", pir_state); + pir_state.enable = match state { true => 1, false => 0, }; - self.set_motionstate(motion_state)?; + self.set_pirstate(pir_state)?; Ok(()) } } /// Turn PIR ON or OFF -pub enum MotionState { +pub enum PirState { /// Turn the PIR on On, /// Turn the PIR off diff --git a/src/cmdline.rs b/src/cmdline.rs index 089ded0..a550b9d 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -20,6 +20,6 @@ pub enum Command { Rtsp(super::rtsp::Opt), StatusLight(super::statusled::Opt), Reboot(super::reboot::Opt), - Motion(super::motion::Opt), + Pir(super::pir::Opt), Talk(super::talk::Opt), } diff --git a/src/main.rs b/src/main.rs index fb3e01b..0501727 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use validator::Validate; mod cmdline; mod config; mod reboot; -mod motion; +mod pir; mod rtsp; mod statusled; mod talk; @@ -65,8 +65,8 @@ fn main() -> Result<()> { Some(Command::Reboot(opts)) => { reboot::main(opts, config)?; } - Some(Command::Motion(opts)) => { - motion::main(opts, config)?; + Some(Command::Pir(opts)) => { + pir::main(opts, config)?; } Some(Command::Talk(opts)) => { talk::main(opts, config)?; diff --git a/src/motion/cmdline.rs b/src/pir/cmdline.rs similarity index 90% rename from src/motion/cmdline.rs rename to src/pir/cmdline.rs index 17406d7..36d2ed4 100644 --- a/src/motion/cmdline.rs +++ b/src/pir/cmdline.rs @@ -12,7 +12,7 @@ fn onoff_parse(src: &str) -> Result { } } -/// The motion command will control the motion status off the camera +/// The pir command will control the PIR status of the camera #[derive(StructOpt, Debug)] pub struct Opt { /// The name of the camera. Must be a name in the config diff --git a/src/motion/mod.rs b/src/pir/mod.rs similarity index 53% rename from src/motion/mod.rs rename to src/pir/mod.rs index 2117b19..ac56a40 100644 --- a/src/motion/mod.rs +++ b/src/pir/mod.rs @@ -1,16 +1,16 @@ /// -/// # Neolink Motion +/// # Neolink PIR /// -/// This module handles the controls of the motion alarm +/// This module handles the controls of the pir sensor alarm /// /// /// # Usage /// /// ```bash -/// # To turn the motion on -/// neolink motion --config=config.toml CameraName on +/// # To turn the pir sensor on +/// neolink pir --config=config.toml CameraName on /// # Or off -/// neolink motion --config=config.toml CameraName off +/// neolink pir --config=config.toml CameraName off /// ``` /// use anyhow::{Context, Result}; @@ -21,14 +21,14 @@ use super::config::Config; use crate::utils::find_and_connect; pub(crate) use cmdline::Opt; -/// Entry point for the motion subcommand +/// Entry point for the pir subcommand /// /// Opt is the command line options pub(crate) fn main(opt: Opt, config: Config) -> Result<()> { let mut camera = find_and_connect(&config, &opt.camera)?; camera - .motion_set(opt.on) - .context("Unable to set camera motion state")?; + .pir_set(opt.on) + .context("Unable to set camera PIR state")?; Ok(()) } From a49a9d248f41feae12b83a201ae72486f14e2666 Mon Sep 17 00:00:00 2001 From: Joris Bremond Date: Sun, 12 Jun 2022 14:24:37 +0200 Subject: [PATCH 3/3] Format code --- crates/core/src/bc/xml.rs | 2 -- crates/core/src/bc_protocol.rs | 2 +- crates/core/src/bc_protocol/pirstate.rs | 4 +--- src/main.rs | 2 +- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 3945131..908bb41 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -326,8 +326,6 @@ pub struct RfAlarmCfg { pub timeBlockList: String, } - - /// TalkConfig xml #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] pub struct TalkConfig { diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 3421d5a..40e099b 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -12,8 +12,8 @@ mod ledstate; mod login; mod logout; mod ping; -mod reboot; mod pirstate; +mod reboot; mod resolution; mod stream; mod talk; diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index 6f61dfd..98e5281 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -93,8 +93,6 @@ impl BcCamera { } } - - /// This is a convience function to control the PIR status /// True is on and false is off pub fn pir_set(&mut self, state: bool) -> Result<()> { @@ -114,5 +112,5 @@ pub enum PirState { /// Turn the PIR on On, /// Turn the PIR off - Off, + Off, } diff --git a/src/main.rs b/src/main.rs index 0501727..5068eee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,8 @@ use validator::Validate; mod cmdline; mod config; -mod reboot; mod pir; +mod reboot; mod rtsp; mod statusled; mod talk;