Add LED light controls to core

This commit is contained in:
QuantumEntangledAndy committed 2021-08-01 13:38:34 +07:00
1 parent d93534a1c2
commit e12ac6c7b4
4 files changed
+186 -10

No files matched your search

+4
View File
@@ -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 =
+43 -10
View File
@@ -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<Encryption>,
/// LoginUser xml is used during modern login: `MSG_ID_LOGIN`
/// LoginUser xml is used during modern login
#[yaserde(rename = "LoginUser")]
pub login_user: Option<LoginUser>,
/// LoginNet xml is used during modern login: `MSG_ID_LOGIN`
/// LoginNet xml is used during modern login
#[yaserde(rename = "LoginNet")]
pub login_net: Option<LoginNet>,
/// 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<DeviceInfo>,
/// 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<VersionInfo>,
/// Preview xml is used as part of the stream request to set the stream quality and channel
#[yaserde(rename = "Preview")]
pub preview: Option<Preview>,
#[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<SystemGeneral>,
/// 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<Norm>,
/// Received as part of the LEDState info request
#[yaserde(rename = "LedState")]
pub led_state: Option<LedState>,
}
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<u8>,
}
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<u32>,
/// 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()
}
+2
View File
@@ -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<T> = std::result::Result<T, Error>;
+137
View File
@@ -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<LedState> {
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,
}