Merge pull request #271 from jbremond/master

This commit is contained in:
Andrew King authored and GitHub committed 2022-06-13 11:52:28 +07:00
commit cab43d0b09
8 files changed
+208

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_PIR_ALARM: u32 = 212;
/// Setting PIR status messages have this ID
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
+24
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,27 @@ 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
@@ -12,6 +12,7 @@ mod ledstate;
mod login;
mod logout;
mod ping;
mod pirstate;
mod reboot;
mod resolution;
mod stream;
@@ -24,6 +25,7 @@ use bc::model::*;
pub(crate) use connection::*;
pub use errors::Error;
pub use ledstate::LightState;
pub use pirstate::PirState;
pub use resolution::*;
pub use stream::{Stream, StreamOutput, StreamOutputError};
+116
View File
@@ -0,0 +1,116 @@
use super::{BcCamera, Error, Result, RX_TIMEOUT};
use crate::bc::{model::*, xml::*};
impl BcCamera {
/// Get the [RfAlarmCfg] xml which contains the PIR status of the camera
pub fn get_pirstate(&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_PIR_ALARM)?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_GET_PIR_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(pirstate),
..
})),
..
}) = msg.body
{
Ok(pirstate)
} else {
Err(Error::UnintelligibleReply {
reply: msg,
why: "Expected PirSate xml but it was not recieved",
})
}
}
/// Set the PIR sensor using the [RfAlarmCfg] xml
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_PIR_ALARM)?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_START_PIR_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 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_pirstate(pir_state)?;
Ok(())
}
}
/// Turn PIR ON or OFF
pub enum PirState {
/// 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),
Pir(super::pir::Opt),
Talk(super::talk::Opt),
}
+4
View File
@@ -16,6 +16,7 @@ use validator::Validate;
mod cmdline;
mod config;
mod pir;
mod reboot;
mod rtsp;
mod statusled;
@@ -64,6 +65,9 @@ fn main() -> Result<()> {
Some(Command::Reboot(opts)) => {
reboot::main(opts, config)?;
}
Some(Command::Pir(opts)) => {
pir::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 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
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 PIR
///
/// This module handles the controls of the pir sensor alarm
///
///
/// # Usage
///
/// ```bash
/// # To turn the pir sensor on
/// neolink pir --config=config.toml CameraName on
/// # Or off
/// neolink pir --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 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
.pir_set(opt.on)
.context("Unable to set camera PIR state")?;
Ok(())
}