From e12ac6c7b453dbe92459d27f0dd0bf9ba9074e66 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 1 Aug 2021 13:38:34 +0700 Subject: [PATCH] Add LED light controls to core --- crates/core/src/bc/model.rs | 4 + crates/core/src/bc/xml.rs | 53 +++++++-- crates/core/src/bc_protocol.rs | 2 + crates/core/src/bc_protocol/ledstate.rs | 137 ++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 crates/core/src/bc_protocol/ledstate.rs diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 977fc92..e0c9e12 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -18,6 +18,10 @@ pub const MSG_ID_PING: u32 = 93; pub const MSG_ID_GET_GENERAL: u32 = 104; /// Setting general system info (clock mostly) messages have this ID pub const MSG_ID_SET_GENERAL: u32 = 105; +/// Getting the LED status is done with this ID +pub const MSG_ID_GET_LED_STATUS: u32 = 208; +/// Setting the LED status is done with this ID +pub const MSG_ID_SET_LED_STATUS: u32 = 209; /// An empty password in legacy format pub const EMPTY_LEGACY_PASSWORD: &str = diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 22506e6..5d41268 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -33,31 +33,33 @@ impl Default for BcPayloads { #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] #[yaserde(rename = "body")] pub struct BcXml { - /// Encryption xml are negociated during login and contain the NONCE: `MSG_ID_LOGIN` + /// Encryption xml are negociated during login and contain the NONCE #[yaserde(rename = "Encryption")] pub encryption: Option, - /// LoginUser xml is used during modern login: `MSG_ID_LOGIN` + /// LoginUser xml is used during modern login #[yaserde(rename = "LoginUser")] pub login_user: Option, - /// LoginNet xml is used during modern login: `MSG_ID_LOGIN` + /// LoginNet xml is used during modern login #[yaserde(rename = "LoginNet")] pub login_net: Option, - /// The final part of a login sequence will return DeviceInfo xml: `MSG_ID_LOGIN` + /// The final part of a login sequence will return DeviceInfo xml #[yaserde(rename = "DeviceInfo")] pub device_info: Option, - /// The VersionInfo xml is recieved in reply to a version request: `MSG_ID_VERSION` + /// The VersionInfo xml is recieved in reply to a version request #[yaserde(rename = "VersionInfo")] pub version_info: Option, /// Preview xml is used as part of the stream request to set the stream quality and channel #[yaserde(rename = "Preview")] pub preview: Option, #[yaserde(rename = "SystemGeneral")] - /// SystemGeneral xml is sent or recieved as part of the clock get/setting: - /// `MSG_ID_GET_GENERAL` and `MSG_ID_SET_GENERAL` + /// SystemGeneral xml is sent or recieved as part of the clock get/setting pub system_general: Option, - /// Received as part of the Genral system info request: `MSG_ID_GET_GENERAL` + /// Received as part of the Genral system info request #[yaserde(rename = "Norm")] pub norm: Option, + /// Received as part of the LEDState info request + #[yaserde(rename = "LedState")] + pub led_state: Option, } impl BcXml { @@ -194,7 +196,7 @@ pub struct Preview { /// Extension xml /// /// This is used to describe the subsequent payload passed the `payload_offset` -#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[derive(PartialEq, Eq, Debug, YaDeserialize, YaSerialize)] pub struct Extension { /// XML Version always "1.1" #[yaserde(attribute)] @@ -216,6 +218,18 @@ pub struct Extension { pub channel_id: Option, } +impl Default for Extension { + fn default() -> Extension { + Extension { + version: xml_ver(), + binary_data: None, + user_name: None, + token: None, + channel_id: None, + } + } +} + /// SystemGeneral xml #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] pub struct SystemGeneral { @@ -263,7 +277,26 @@ pub struct Norm { norm: String, } -/// Convience function to return the xml function used throughout the library +/// LedState xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct LedState { + /// XML Version always "1.1" + #[yaserde(attribute)] + pub version: String, + /// Channel ID of camera to change its LED + #[yaserde(rename = "channelId")] + pub channel_id: u32, + /// LED Version, observed value is "2" + #[yaserde(rename = "ledVersion")] + pub led_version: Option, + /// State of the IR LEDs values are "auto", "open", "close" + pub state: String, + /// State of the LED status light (blue on light), vallues are "open", "close" + #[yaserde(rename = "lightState")] + pub light_state: 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 1982b29..c9d4893 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -10,6 +10,7 @@ use Md5Trunc::*; mod connection; mod errors; +mod ledstate; mod login; mod logout; mod media_packet; @@ -19,6 +20,7 @@ mod time; mod version; pub use errors::Error; +pub use ledstate::LightState; pub use stream::{StreamFormat, StreamOutput, StreamOutputError}; type Result = std::result::Result; diff --git a/crates/core/src/bc_protocol/ledstate.rs b/crates/core/src/bc_protocol/ledstate.rs new file mode 100644 index 0000000..91259de --- /dev/null +++ b/crates/core/src/bc_protocol/ledstate.rs @@ -0,0 +1,137 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::{model::*, xml::*}; + +impl BcCamera { + /// Get the LEDState xml which contains the LED status of the camera + pub fn get_ledstate(&mut self) -> Result { + let connection = self + .connection + .as_ref() + .expect("Must be connected to get time"); + let sub_get = connection.subscribe(MSG_ID_GET_LED_STATUS)?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_GET_LED_STATUS, + 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 { + led_state: Some(ledstate), + .. + })), + .. + }) = msg.body + { + Ok(ledstate) + } else { + Err(Error::UnintelligibleReply { + reply: msg, + why: "Expected LEDState xml but it was not recieved", + }) + } + } + + /// Set the led lights using the LEDState xml + pub fn set_ledstate(&mut self, mut led_state: LedState) -> Result<()> { + let connection = self + .connection + .as_ref() + .expect("Must be connected to get time"); + let sub_set = connection.subscribe(MSG_ID_SET_LED_STATUS)?; + + // led_version is a field recieved from the camera but not sent + // we set to None to ensure we don't send it to the camera + led_state.led_version = None; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_SET_LED_STATUS, + 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 { + led_state: Some(led_state), + ..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 LEDState xml", + }) + } + } + + /// This is a convience function to control the IR LED lights + /// + /// This is for the RED IR lights that can come on automaitcally + /// during low light. + pub fn irled_light_set(&mut self, state: LightState) -> Result<()> { + let mut led_state = self.get_ledstate()?; + led_state.state = match state { + LightState::On => "open".to_string(), + LightState::Off => "close".to_string(), + LightState::Auto => "auto".to_string(), + }; + self.set_ledstate(led_state)?; + Ok(()) + } + + /// This is a convience function to control the LED light + /// True is on and false is off + /// + /// This is for the little blue on light of some camera + pub fn led_light_set(&mut self, state: bool) -> Result<()> { + let mut led_state = self.get_ledstate()?; + led_state.light_state = match state { + true => "open".to_string(), + false => "close".to_string(), + }; + self.set_ledstate(led_state)?; + Ok(()) + } +} + +/// This is pased to `irled_light_set` to turn it on, off or set it to light based auto +pub enum LightState { + /// Turn the light on + On, + /// Turn the light off + Off, + /// Set the light to light based auto + Auto, +}