Add new motion command to set PIR sensor ON or OFF on battery powered camera

tested on reolink argus pro
This commit is contained in:
Joris Bremond committed 2022-05-09 22:38:28 +02:00
1 parent a8aa52aec7
commit 9f1bcd6f55
8 files changed
+212

No files matched your search

+4
View File
@@ -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
+26
View File
@@ -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<TalkConfig>,
/// rfAlarmCfg xml is sent or recieved as part of the PIR get/setting
#[yaserde(rename = "rfAlarmCfg")]
pub rf_alarm_cfg: Option<RfAlarmCfg>,
/// Revieced as part of the TalkAbility request
#[yaserde(rename = "TalkAbility")]
pub talk_ability: Option<TalkAbility>,
@@ -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 {
+2
View File
@@ -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};
+118
View File
@@ -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<RfAlarmCfg> {
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,
}
+1
View File
@@ -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),
}
+4
View File
@@ -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)?;
}
+23
View File
@@ -0,0 +1,23 @@
use anyhow::{anyhow, Result};
use structopt::StructOpt;
fn onoff_parse(src: &str) -> Result<bool> {
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,
}
+34
View File
@@ -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(())
}