diff --git a/.gitignore b/.gitignore index 78aa81c..4780ea9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ # Committing IntelliJ project files is a violation of intergalactic law .idea/ + +# We prefer to keep .vscode files outside the repo +.vscode/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..945cf95 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}", + "args": ["mqtt", "--config", "target/config.toml"], + "cwd": "${workspaceRoot}", + "sourceLanguages": [ + "rust" + ], + "env": { + "RUST_LOG": "debug" + } + } + ]} \ No newline at end of file diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index d7de246..584cfc5 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -46,6 +46,11 @@ 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; +/// Manual Floodlight Control +pub const MSG_ID_FLOODLIGHT_MANUAL: u32 = 288; +/// Floodlight status report from the camera +pub const MSG_ID_FLOODLIGHT_STATUS_LIST: u32 = 291; + /// 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..fe2e74f 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, + /// Sent to manually control the floodlight + #[yaserde(rename = "FloodlightManual")] + pub floodlight_manual: Option, + /// Received when the floodlight status is updated + #[yaserde(rename = "FloodlightStatusList")] + pub floodlight_status_list: Option, } impl BcXml { @@ -312,6 +318,49 @@ pub struct LedState { pub light_state: String, } + +/// FloodlightStatus xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct FloodlightStatus { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Channel ID of floodlight + #[yaserde(rename = "channel")] + pub channel_id: u8, + /// On or off + #[yaserde(rename = "status")] + pub status: u8, +} + +/// FloodlightStatusList xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct FloodlightStatusList { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// List of events + #[yaserde(rename = "FloodlightStatusList")] + pub floodlight_status_list: Vec, +} + +/// FloodlightManual xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct FloodlightManual { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Channel ID of floodlight + #[yaserde(rename = "channelId")] + pub channel_id: u8, + /// On or off + #[yaserde(rename = "status")] + pub status: u8, + /// How long the manual control should apply for + pub duration: u16, +} + + /// rfAlarmCfg xml #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] pub struct RfAlarmCfg { diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index d3e86ba..22f7e1e 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -11,6 +11,7 @@ use Md5Trunc::*; mod connection; mod errors; +mod floodlight_status; mod ledstate; mod login; mod logout; diff --git a/crates/core/src/bc_protocol/floodlight_status.rs b/crates/core/src/bc_protocol/floodlight_status.rs new file mode 100644 index 0000000..508ce4c --- /dev/null +++ b/crates/core/src/bc_protocol/floodlight_status.rs @@ -0,0 +1,110 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::{model::*, xml::*}; + +impl BcCamera { + /// Get the [FloodlightStatusList] xml which contains the Floodlist status of the camera + pub fn get_floodlight_status(&self) -> Result { + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let sub_get = connection.subscribe(msg_num)?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_FLOODLIGHT_STATUS_LIST, + 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: None, + }), + }; + + sub_get.send(get)?; + let msg = sub_get.rx.recv_timeout(RX_TIMEOUT)?; + if msg.meta.response_code != 200 { + return Err(Error::CameraServiceUnavaliable); + } + + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + floodlight_status_list: Some(floodlight_status_list), + .. + })), + .. + }) = msg.body + { + Ok(floodlight_status_list) + } else { + Err(Error::UnintelligibleReply { + reply: Box::new(msg), + why: "Expected FloodlightStatusList xml but it was not recieved", + }) + } + } + + /// Set the floodlight status using the [FloodlightManual] xml + pub fn set_floodlight_manual(&self, floodlight_manual: FloodlightManual) -> Result<()> { + let connection = self.get_connection(); + + let msg_num = self.new_message_num(); + let sub_set = connection.subscribe(msg_num)?; + + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_FLOODLIGHT_MANUAL, + 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 { + floodlight_manual: Some(floodlight_manual), + ..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: Box::new(msg), + why: "The camera did not accept the Floodlight manual state", + }) + } + } + + /// Set the floodlight status for a duration + pub fn floodlight_light_set(&self, state: bool, duration: u16) -> Result<()> { + let mut floodlight_state = self.get_floodlight_status()?; + for floodlight in floodlight_state.floodlight_status_list.iter_mut() { + if floodlight.channel_id == self.channel_id { + self.set_floodlight_manual(FloodlightManual { version: "1".to_string(), channel_id: floodlight.channel_id, status: match state { + true => 1, + false => 0, + } , duration: duration })?; + } + } + + Ok(()) + } +} diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 05be67c..657095c 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -15,6 +15,8 @@ pub(crate) enum Messages { MotionStop, MotionStart, Reboot, + FloodlightOn, + FloodlightOff, StatusLedOn, StatusLedOff, IRLedOn, @@ -263,6 +265,24 @@ impl MessageHandler { "OK".to_string() } } + Messages::FloodlightOn => { + if self.camera.floodlight_light_set(true, 180).is_err() { + error!("Failed to turn on the floodlight light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::FloodlightOff => { + if self.camera.floodlight_light_set(false, 180).is_err() { + error!("Failed to turn off the floodlight light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } Messages::StatusLedOn => { if self.camera.led_light_set(true).is_err() { error!("Failed to turn on the status light"); diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index dd3968f..c2d331f 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -148,6 +148,22 @@ fn listen_on_camera( while app.running(&format!("app: {}", cam_config.name)) { if let Ok(msg) = mqtt.poll() { match msg.as_ref() { + MqttReplyRef { + topic: "control/floodlight", + message: "on", + } => { + if event_cam.send_message(Messages::FloodlightOn).is_err() { + error!("Failed to set floodlight on"); + } + } + MqttReplyRef { + topic: "control/floodlight", + message: "off", + } => { + if event_cam.send_message(Messages::FloodlightOff).is_err() { + error!("Failed to set floodlight off"); + } + } MqttReplyRef { topic: "control/led", message: "on",