diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index e19ef6b..d7de246 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -16,6 +16,8 @@ pub const MSG_ID_VIDEO_STOP: u32 = 4; pub const MSG_ID_TALKABILITY: u32 = 10; /// TalkReset messages have this ID pub const MSG_ID_TALKRESET: u32 = 11; +/// PtzControl messages have this ID +pub const MSG_ID_PTZ_CONTROL: u32 = 18; /// Reboot messages have this ID pub const MSG_ID_REBOOT: u32 = 23; /// Request motion detection messages @@ -50,7 +52,7 @@ pub const EMPTY_LEGACY_PASSWORD: &str = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; /// Top level bc message -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] pub struct Bc { /// header part of the message pub meta: BcMeta, @@ -61,7 +63,7 @@ pub struct Bc { /// /// The body of a bc message is either legacy or modern /// -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] #[allow(clippy::large_enum_variant)] pub enum BcBody { /// Legacy is unsupported except for login where it is used @@ -86,7 +88,7 @@ pub enum BcBody { /// `extension` or `payload` these are header only messages. This usually occurs to acknoledge receipt /// of a command. In such cases the header `response_code` should be checked. /// -#[derive(Debug, Default, PartialEq, Eq)] +#[derive(Debug, Default, PartialEq)] pub struct ModernMsg { /// Extension describes the following payload such as which channel id it is for pub extension: Option, diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index e5cc699..cd12fa2 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -10,7 +10,7 @@ use yaserde_derive::{YaDeserialize, YaSerialize}; use indoc::indoc; /// There are two types of payloads xml and binary -#[derive(PartialEq, Eq, Debug, YaDeserialize)] +#[derive(PartialEq, Debug, YaDeserialize)] #[yaserde(flatten)] #[allow(clippy::large_enum_variant)] pub enum BcPayloads { @@ -31,7 +31,7 @@ impl Default for BcPayloads { } /// The top level BC Xml -#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename = "body")] pub struct BcXml { /// Encryption xml is received during login and contain the NONCE @@ -73,6 +73,9 @@ pub struct BcXml { /// Received when motion is detected #[yaserde(rename = "AlarmEventList")] pub alarm_event_list: Option, + /// Sent to move the camera + #[yaserde(rename = "PtzControl")] + pub ptz_control: Option, } impl BcXml { @@ -445,6 +448,21 @@ pub struct AlarmEvent { pub timeStamp: i32, } +/// The Ptz messages used to move the camera +#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct PtzControl { + /// 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, + /// The amount of movement to perform + pub speed: f32, + /// The direction to transverse. Known directions: "right" + pub command: String, +} + /// Convience function to return the xml version used throughout the library pub fn xml_ver() -> String { "1.1".to_string() diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 8b3e3c6..d3e86ba 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -17,6 +17,7 @@ mod logout; mod motion; mod ping; mod pirstate; +mod ptz; mod reboot; mod resolution; mod stream; @@ -31,6 +32,7 @@ pub use errors::Error; pub use ledstate::LightState; pub use motion::{MotionData, MotionStatus}; pub use pirstate::PirState; +pub use ptz::Direction; pub use resolution::*; use std::sync::Arc; pub use stream::{Stream, StreamData}; diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index b02f798..aae4c91 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -3,7 +3,7 @@ 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 { + pub fn get_pirstate(&self) -> Result { let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_get = connection.subscribe(msg_num)?; @@ -50,7 +50,7 @@ impl BcCamera { } /// Set the PIR sensor using the [RfAlarmCfg] xml - pub fn set_pirstate(&mut self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { + pub fn set_pirstate(&self, rf_alarm_cfg: RfAlarmCfg) -> Result<()> { let connection = self.get_connection(); let msg_num = self.new_message_num(); let sub_set = connection.subscribe(msg_num)?; @@ -97,7 +97,7 @@ 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<()> { + pub fn pir_set(&self, state: bool) -> Result<()> { let mut pir_state = self.get_pirstate()?; // println!("{:?}", pir_state); pir_state.enable = match state { diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs new file mode 100644 index 0000000..252cbb2 --- /dev/null +++ b/crates/core/src/bc_protocol/ptz.rs @@ -0,0 +1,81 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::{model::*, xml::*}; + +/// Directions used for Ptz +pub enum Direction { + /// To move the camera Up + Up, + /// To move the camera Down + Down, + /// To move the camera Left + Left, + /// To move the camera Right + Right, + /// To zoom the camera In (may be done with cropping depending on camera model) + In, + /// To zoom the camera Out (may be done with cropping depending on camera model) + Out, +} + +impl BcCamera { + /// Send a PTZ message to the camera + pub fn send_ptz(&self, direction: Direction, amount: f32) -> Result<()> { + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let sub_set = connection.subscribe(msg_num)?; + + let direction_str = match direction { + Direction::Up => "up", + Direction::Down => "down", + Direction::Left => "left", + Direction::Right => "right", + Direction::In => { + unimplemented!() + } + Direction::Out => { + unimplemented!() + } + } + .to_string(); + let send = Bc { + meta: BcMeta { + msg_id: MSG_ID_PTZ_CONTROL, + channel_id: self.channel_id, + msg_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 { + ptz_control: Some(PtzControl { + version: xml_ver(), + channel_id: self.channel_id, + speed: amount, + command: direction_str, + }), + ..Default::default() + })), + }), + }; + + sub_set.send(send)?; + let msg = sub_set.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: Box::new(msg), + why: "The camera did not accept the PtzControl xml", + }) + } + } +} diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 70873c0..05be67c 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -4,7 +4,9 @@ use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; use crossbeam_channel::{bounded, unbounded, Receiver, Sender, TryRecvError}; use log::*; -use neolink_core::bc_protocol::{BcCamera, Error as BcError, LightState, MotionStatus}; +use neolink_core::bc_protocol::{ + BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus, +}; use std::sync::{Arc, Mutex}; pub(crate) enum Messages { @@ -309,8 +311,22 @@ impl MessageHandler { Messages::Battery => { unimplemented!() } - Messages::Ptz(_direction) => { - unimplemented!() + Messages::Ptz(direction) => { + let (bc_direction, amount) = match direction { + Direction::Up(amount) => (BcDirection::Up, amount), + Direction::Down(amount) => (BcDirection::Down, amount), + Direction::Left(amount) => (BcDirection::Left, amount), + Direction::Right(amount) => (BcDirection::Right, amount), + Direction::In(amount) => (BcDirection::In, amount), + Direction::Out(amount) => (BcDirection::Out, amount), + }; + if self.camera.send_ptz(bc_direction, amount).is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } } _ => "UNKNOWN COMMAND".to_string(), };