Add motion to neolink_core
This commit is contained in:
4 files changed
+170
No files matched your search
@@ -16,6 +16,10 @@ pub const MSG_ID_TALKABILITY: u32 = 10;
|
||||
pub const MSG_ID_TALKRESET: u32 = 11;
|
||||
/// Reboot messages have this ID
|
||||
pub const MSG_ID_REBOOT: u32 = 23;
|
||||
/// Request motion detection messages
|
||||
pub const MSG_ID_MOTION_REQUEST: u32 = 31;
|
||||
/// Motion detection messages
|
||||
pub const MSG_ID_MOTION: u32 = 33;
|
||||
/// Version messages have this ID
|
||||
pub const MSG_ID_VERSION: u32 = 80;
|
||||
/// Ping messages have this ID
|
||||
|
||||
@@ -66,6 +66,9 @@ pub struct BcXml {
|
||||
/// Revieced as part of the TalkAbility request
|
||||
#[yaserde(rename = "TalkAbility")]
|
||||
pub talk_ability: Option<TalkAbility>,
|
||||
/// Received when motion is detected
|
||||
#[yaserde(rename = "AlarmEventList")]
|
||||
pub alarm_event_list: Option<AlarmEventList>,
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
@@ -388,6 +391,35 @@ pub struct AudioConfigList {
|
||||
pub audio_config: AudioConfig,
|
||||
}
|
||||
|
||||
/// An XML that desctibes a list of events such as motion detection
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct AlarmEventList {
|
||||
/// XML Version
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
/// List of events
|
||||
#[yaserde(rename = "AlarmEvent")]
|
||||
pub alarm_events: Vec<AlarmEvent>,
|
||||
}
|
||||
|
||||
/// An alarm event. Camera can send multiple per message as an array in AlarmEventList.
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct AlarmEvent {
|
||||
/// XML Version
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
/// The channel the event occured on. Usually zero unless from an NVR
|
||||
#[yaserde(rename = "channelId")]
|
||||
pub channel_id: u8,
|
||||
/// Motion status. Known values are `"MD"` or `"none"`
|
||||
pub status: String,
|
||||
/// The recording status. Known values `0` or `1`
|
||||
pub recording: i32,
|
||||
/// The timestamp associated with the recording. `0` if not recording
|
||||
#[yaserde(rename = "timeStamp")]
|
||||
pub timeStamp: i32,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
|
||||
@@ -15,6 +15,7 @@ pub(crate) mod filesub;
|
||||
mod ledstate;
|
||||
mod login;
|
||||
mod logout;
|
||||
mod motion;
|
||||
mod ping;
|
||||
mod reboot;
|
||||
mod stream;
|
||||
@@ -25,6 +26,7 @@ mod version;
|
||||
pub use binarysub::BinarySubscriber;
|
||||
pub use errors::Error;
|
||||
pub use ledstate::LightState;
|
||||
pub use motion::{MotionOutput, MotionOutputError, MotionStatus};
|
||||
pub use stream::{StreamOutput, StreamOutputError};
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
use super::{BcCamera, Error, Result, RX_TIMEOUT};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
|
||||
/// Motion Status that the callback can send
|
||||
pub enum MotionStatus {
|
||||
/// Sent when motion is first detected
|
||||
Start,
|
||||
/// Sent when motion stops
|
||||
Stop,
|
||||
/// Sent when an Alarm about something other than motion was received
|
||||
NoChange,
|
||||
}
|
||||
|
||||
/// This is a conveince type for the error of the MotionOutput callback
|
||||
pub type MotionOutputError = Result<bool>;
|
||||
|
||||
/// Trait used as part of [`listen_on_motion`] to send motion messages
|
||||
pub trait MotionOutput {
|
||||
/// This is the callback used when motion is received
|
||||
///
|
||||
/// If result is `Ok(true)` more messages will be sent
|
||||
///
|
||||
/// If result if `Ok(false)` then message will be stopped
|
||||
///
|
||||
/// If result is `Err(E)` then motion messages be stopped
|
||||
/// and an error will be thrown
|
||||
fn motion_recv(&mut self, motion_status: MotionStatus) -> MotionOutputError;
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
/// This message tells the camera to send the motion events to us
|
||||
/// Which are the recieved on msgid 33
|
||||
fn start_motion_query(&self) -> Result<()> {
|
||||
let connection = self
|
||||
.connection
|
||||
.as_ref()
|
||||
.expect("Must be connected to listen to messages");
|
||||
|
||||
let sub = connection.subscribe(MSG_ID_MOTION_REQUEST)?;
|
||||
let msg = Bc {
|
||||
meta: BcMeta {
|
||||
msg_id: MSG_ID_MOTION_REQUEST,
|
||||
channel_id: self.channel_id,
|
||||
msg_num: self.new_message_num(),
|
||||
stream_type: 0,
|
||||
response_code: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
body: BcBody::ModernMsg(ModernMsg {
|
||||
..Default::default()
|
||||
}),
|
||||
};
|
||||
|
||||
sub.send(msg)?;
|
||||
|
||||
let msg = sub.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 accept the request to start motion",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// This requests that motion messages be listen to and sent to the
|
||||
/// output struct.
|
||||
///
|
||||
/// The output structure must implement the [`MotionCallback`] trait
|
||||
pub fn listen_on_motion<T: MotionOutput>(&self, data_out: &mut T) -> Result<()> {
|
||||
self.start_motion_query()?;
|
||||
|
||||
let connection = self
|
||||
.connection
|
||||
.as_ref()
|
||||
.expect("Must be connected to listen to messages");
|
||||
|
||||
// After start_motion_query (MSG_ID 31) the camera sends motion messages
|
||||
// when whenever motion is detected.
|
||||
let sub = connection.subscribe(MSG_ID_MOTION)?;
|
||||
|
||||
loop {
|
||||
// Mostly ignore when timout is reached because these messages are only
|
||||
// sent when motion is detected which might means hours between messages
|
||||
// being received
|
||||
let msg = sub.rx.recv_timeout(RX_TIMEOUT);
|
||||
let status = match msg {
|
||||
Ok(motion_msg) => {
|
||||
if let BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
alarm_event_list: Some(alarm_event_list),
|
||||
..
|
||||
})),
|
||||
..
|
||||
}) = motion_msg.body
|
||||
{
|
||||
let mut result = MotionStatus::NoChange;
|
||||
for alarm_event in &alarm_event_list.alarm_events {
|
||||
if alarm_event.channel_id == self.channel_id {
|
||||
if alarm_event.status == "MD" {
|
||||
result = MotionStatus::Start;
|
||||
break;
|
||||
} else if alarm_event.status == "none" {
|
||||
result = MotionStatus::Stop;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
} else {
|
||||
Ok(MotionStatus::NoChange)
|
||||
}
|
||||
}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Ok(MotionStatus::NoChange),
|
||||
// On connection drop we stop
|
||||
Err(e @ std::sync::mpsc::RecvTimeoutError::Disconnected) => Err(e),
|
||||
}?;
|
||||
|
||||
match data_out.motion_recv(status) {
|
||||
Ok(true) => {}
|
||||
Ok(false) => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user