From 9ae537fb3852ac01dc9fcee5dc58dd53741ce2f9 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 23 Oct 2023 09:57:33 +0700 Subject: [PATCH] Add control/floodlight_tasks --- crates/core/src/bc/model.rs | 4 + crates/core/src/bc_protocol.rs | 2 +- .../core/src/bc_protocol/floodlight_status.rs | 103 ------------------ src/mqtt/mod.rs | 30 +++++ 4 files changed, 35 insertions(+), 104 deletions(-) delete mode 100644 crates/core/src/bc_protocol/floodlight_status.rs diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index a7d0d57..168921e 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -71,8 +71,12 @@ pub const MSG_ID_BATTERY_INFO_LIST: u32 = 252; pub const MSG_ID_BATTERY_INFO: u32 = 253; /// Manual Floodlight Control pub const MSG_ID_FLOODLIGHT_MANUAL: u32 = 288; +/// Set Floodlight tasks xml +pub const MSG_ID_FLOODLIGHT_TASKS_WRITE: u32 = 290; /// Floodlight status report from the camera pub const MSG_ID_FLOODLIGHT_STATUS_LIST: u32 = 291; +/// Get the floodlight task xml +pub const MSG_ID_FLOODLIGHT_TASKS_READ: u32 = 438; /// An empty password in legacy format pub const EMPTY_LEGACY_PASSWORD: &str = diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 512b6f2..2d1646e 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -17,7 +17,7 @@ mod battery; mod connection; mod credentials; mod errors; -mod floodlight_status; +mod floodlight; mod keepalive; mod ledstate; mod link; diff --git a/crates/core/src/bc_protocol/floodlight_status.rs b/crates/core/src/bc_protocol/floodlight_status.rs deleted file mode 100644 index 4705df0..0000000 --- a/crates/core/src/bc_protocol/floodlight_status.rs +++ /dev/null @@ -1,103 +0,0 @@ -use tokio::sync::mpsc::{channel, Receiver}; - -use super::{BcCamera, Error, Result}; -use crate::bc::{model::*, xml::*}; - -impl BcCamera { - /// Listen on the flood light update messages and return their XMLs - pub async fn listen_on_flightlight(&self) -> Result> { - let (tx, rx) = channel(3); - let connection = self.get_connection(); - connection - .handle_msg(MSG_ID_FLOODLIGHT_STATUS_LIST, move |bc| { - let tx = tx.clone(); - Box::pin(async move { - if let Bc { - meta: - BcMeta { - msg_id: MSG_ID_FLOODLIGHT_STATUS_LIST, - .. - }, - body: - BcBody::ModernMsg(ModernMsg { - payload: - Some(BcPayloads::BcXml(BcXml { - floodlight_status_list: Some(list), - .. - })), - .. - }), - } = bc - { - let send_this: FloodlightStatusList = list.clone(); - let _ = tx.send(send_this).await; - } - None - }) - }) - .await?; - - Ok(rx) - } - - /// Set the floodlight status using the [FloodlightManual] xml - pub async fn set_floodlight_manual(&self, state: bool, duration: u16) -> Result<()> { - let connection = self.get_connection(); - - let msg_num = self.new_message_num(); - let mut sub_set = connection - .subscribe(MSG_ID_FLOODLIGHT_MANUAL, msg_num) - .await?; - - 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(FloodlightManual { - version: "1".to_string(), - channel_id: self.channel_id, - status: match state { - true => 1, - false => 0, - }, - duration, - }), - ..Default::default() - })), - }), - }; - - sub_set.send(get).await?; - - if let Ok(reply) = - tokio::time::timeout(tokio::time::Duration::from_micros(500), sub_set.recv()).await - { - let msg = reply?; - if let BcMeta { - response_code: 200, .. - } = msg.meta - { - Ok(()) - } else { - Err(Error::UnintelligibleReply { - reply: std::sync::Arc::new(Box::new(msg)), - why: "The camera did not accept the Floodlight manual state", - }) - } - } else { - // Some cameras seem to just not send a reply on success, so after 500ms we return Ok - Ok(()) - } - } -} diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 5408446..d00cbd4 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1001,6 +1001,36 @@ async fn handle_mqtt_message( .await .with_context(|| "Failed to publish wakeup")?; } + MqttReplyRef { + topic: "control/floodlight_tasks", + message, + } => { + let reply = match message.parse::() { + Ok(state) => { + if let Err(e) = camera + .run_task(|cam| { + Box::pin(async move { + cam.flightlight_tasks_enable(state).await?; + AnyResult::Ok(()) + }) + }) + .await + { + format!("FAIL: {e:?}") + } else { + "OK".to_string() + } + } + Err(e) => { + error!("Failed to parse message as bool: {:?}", e); + format!("FAIL: '{message}' => {e:?}") + } + }; + + mqtt.send_message("control/floodlight_tasks", &reply, false) + .await + .with_context(|| "Failed to publish floodlight_tasks")?; + } MqttReplyRef { topic: "query/battery", ..