diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 46ee38a..141c84f 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -47,6 +47,10 @@ pub const MSG_ID_GET_LED_STATUS: u32 = 208; pub const MSG_ID_SET_LED_STATUS: u32 = 209; /// UDP Keep alive pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234; +/// Battery message initiaed by the camera +pub const MSG_ID_BATTERY_INFO_LIST: u32 = 252; +/// Battery message initiaed by the client +pub const MSG_ID_BATTERY_INFO: u32 = 253; /// 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 cd12fa2..1ed7ea0 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -76,6 +76,12 @@ pub struct BcXml { /// Sent to move the camera #[yaserde(rename = "PtzControl")] pub ptz_control: Option, + /// Recieved on login/low battery events + #[yaserde(rename = "BatteryList")] + pub battery_list: Option, + /// Recieved on request for battery info + #[yaserde(rename = "BatteryInfo")] + pub battery_info: Option, } impl BcXml { @@ -463,6 +469,47 @@ pub struct PtzControl { pub command: String, } +/// A list of battery infos. This message is sent from the camera as +/// an event +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct BatteryList { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Battery info items + #[yaserde(rename = "BatteryInfo")] + pub battery_info: Vec, +} + +/// The individual battery info +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct BatteryInfo { + /// The channel the for the camera usually 0 + #[yaserde(rename = "channelId")] + pub channel_id: u8, + /// Charge status known values, "chargeComplete", "charging", + #[yaserde(rename = "chargeStatus")] + pub charge_status: String, + /// Status of charging port known values: "solarPanel" + #[yaserde(rename = "adapterStatus")] + pub adapter_status: String, + /// Voltage + pub voltage: u32, + /// Current + pub current: u32, + /// Temperture + pub temperature: u32, + /// % charge from 0-100 + #[yaserde(rename = "batteryPercent")] + pub battery_percent: u32, + /// Low power flag. Known values 0, 1 (0=false) + #[yaserde(rename = "lowPower")] + pub low_power: u32, + /// Battery version info: Known values 2 + #[yaserde(rename = "batteryVersion")] + pub battery_version: u32, +} + /// 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 5cdc3a6..669c0e0 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -6,6 +6,7 @@ use std::sync::atomic::{AtomicBool, AtomicU16, Ordering}; use Md5Trunc::*; +mod battery; mod connection; mod credentials; mod errors; @@ -306,6 +307,7 @@ impl BcCamera { credentials: Credentials::new(username, passwd), }; me.keepalive().await?; + me.monitor_battery().await?; Ok(me) } diff --git a/crates/core/src/bc_protocol/battery.rs b/crates/core/src/bc_protocol/battery.rs new file mode 100644 index 0000000..ae2dec0 --- /dev/null +++ b/crates/core/src/bc_protocol/battery.rs @@ -0,0 +1,114 @@ +//! Handles battery related messages +//! +//! There are primarily two messages: +//! - BatteryInfoList which the camera sends as part of its login info +//! - BatteryInfo which the client can request on demand +//! + +use super::{BcCamera, Result}; +use crate::{ + bc::{model::*, xml::BatteryInfo}, + Error, +}; +use log::*; + +impl BcCamera { + /// Create a handller to respond to battery messages + /// These messages are sent by the camera on login and maybe + /// also on low battery events + pub async fn monitor_battery(&self) -> Result<()> { + let connection = self.get_connection(); + connection + .handle_msg(MSG_ID_BATTERY_INFO_LIST, |bc| { + if let Bc { + body: + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + battery_list: Some(battery_list), + .. + })), + .. + }), + .. + } = bc + { + for battery in battery_list.battery_info.iter() { + info!( + "==Battery==\n\ + Charge: {}%,\n\ + Temperature: {}°C,\n\ + LowPower: {},\n\ + Adapter: {},\n\ + ChargeStatus: {},\n\ + ", + battery.battery_percent, + battery.temperature, + if battery.low_power == 1 { + "true" + } else { + "false" + }, + battery.adapter_status, + battery.charge_status, + ); + } + } + None + }) + .await?; + Ok(()) + } + + /// Requests the current battery status of the camera + pub async fn battery_info(&self) -> Result { + let connection = self.get_connection(); + + let msg_num = self.new_message_num(); + let mut sub = connection.subscribe(msg_num).await?; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_BATTERY_INFO, + channel_id: self.channel_id, + msg_num, + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: None, + }), + }; + + sub.send(msg).await?; + let msg = sub.recv().await?; + + if let Bc { + meta: BcMeta { + response_code: 200, .. + }, + body: + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + battery_info: Some(battery_info), + .. + })), + .. + }), + } = msg + { + Ok(battery_info) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "The camera did not accept the battery info (maybe no battery) command.", + }) + } + } +}