From 10956e8c8e02e38e7e263346edaa374c48da3161 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 7 Jun 2023 13:03:58 +0700 Subject: [PATCH] Add camera preview into mqtt via snap shot command --- Cargo.lock | 7 ++++--- Cargo.toml | 1 + crates/core/src/bc/model.rs | 2 ++ crates/core/src/bc/xml.rs | 35 ++++++++++++++++++++++++++++++++- crates/core/src/bc_protocol.rs | 1 + src/mqtt/event_cam.rs | 36 +++++++++++++++++++++++++++++++++- src/mqtt/mod.rs | 15 +++++++++++--- src/mqtt/mqttc.rs | 2 ++ 8 files changed, 91 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91dd860..a34b50a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -198,9 +198,9 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" [[package]] name = "base64" -version = "0.21.0" +version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" [[package]] name = "bitflags" @@ -1411,6 +1411,7 @@ name = "neolink" version = "0.5.12" dependencies = [ "anyhow", + "base64 0.21.2", "byte-slice-cast", "clap", "console-subscriber", @@ -1919,7 +1920,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b" dependencies = [ - "base64 0.21.0", + "base64 0.21.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f789f01..498eb13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ [dependencies] anyhow = "1.0.70" +base64 = "0.21.2" byte-slice-cast = "1.2.2" clap = { version = "4.2.2", features = ["derive", "cargo"] } console-subscriber = "0.1.8" diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 8a52f59..f7d6996 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -33,6 +33,8 @@ pub const MSG_ID_VERSION: u32 = 80; pub const MSG_ID_PING: u32 = 93; /// General system info messages have this ID pub const MSG_ID_GET_GENERAL: u32 = 104; +/// Snapshot to get a jpeg image +pub const MSG_ID_SNAP: u32 = 109; /// Used to get the abilities of a user pub const MSG_ID_ABILITY_INFO: u32 = 151; /// Setting general system info (clock mostly) messages have this ID diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 3cda05c..adc790e 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -97,6 +97,9 @@ pub struct BcXml { /// Recieved on request for a link type #[yaserde(rename = "LinkType")] pub link_type: Option, + /// Recieved AND send for the snap message + #[yaserde(rename = "Snap")] + pub snap: Option, } impl BcXml { @@ -700,7 +703,37 @@ pub struct AbilityInfoSubModule { pub struct LinkType { #[yaserde(rename = "type")] /// Type of connection known values `"LAN"` - link_type: String, + pub link_type: String, +} + +/// The Link Type contains the type of connection present +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct Snap { + #[yaserde(rename = "channelId")] + /// The channel id to get the snapshot from + pub channel_id: u8, + /// Unknown, observed values: 0 + /// value is only set on request + #[yaserde(rename = "logicChannel")] + pub logic_channel: Option, + /// Time of snapshot, zero when requesting + pub time: u32, + /// Request a full frame, observed values: 0 + /// value is only set on request + #[yaserde(rename = "fullFrame")] + pub full_frame: Option, + /// Stream name, observed values: `main`, `sub` + /// value is only set on request + #[yaserde(rename = "streamType")] + pub stream_type: Option, + /// File name, usually of the form `01_20230518140240.jpg` + /// value is only set on recieve + #[yaserde(rename = "fileName")] + pub file_name: Option, + /// Size in bytes of the picture + /// value is only set on recieve + #[yaserde(rename = "pictureSize")] + pub picture_size: Option, } /// Convience function to return the xml version used throughout the library diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 4b3b889..f838bfc 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -27,6 +27,7 @@ mod pirstate; mod ptz; mod reboot; mod resolution; +mod snap; mod stream; mod talk; mod time; diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 6fc652b..70e5736 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -13,7 +13,7 @@ use tokio::{ time::{interval, sleep, Duration}, }; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] pub(crate) enum Messages { Login, MotionStop, @@ -32,6 +32,7 @@ pub(crate) enum Messages { PIRQuery, Ptz(Direction), Preset(i8), + Snap(Vec), } #[derive(Debug, Copy, Clone)] @@ -210,6 +211,11 @@ impl EventCamThread { camera: arc_cam.clone(), }; + let mut snap_thread = SnapThread { + tx: self.tx.clone(), + camera: arc_cam.clone(), + }; + let mut keepalive_thread = KeepaliveThread { camera: arc_cam.clone(), }; @@ -256,6 +262,18 @@ impl EventCamThread { Ok(()) } }, + val = async { + info!("{}: Updating Preview", camera_config.name); + snap_thread.run().await + } => { + if let Err(e) = val { + error!("Snap thread aborted: {:?}", e); + Err(e) + } else { + debug!("Normal finish on Snap thread"); + Ok(()) + } + }, val = async { info!("{}: Setting up camera actions", camera_config.name); message_handler.listen().await @@ -329,6 +347,22 @@ impl FloodlightThread { } } +struct SnapThread { + tx: Sender, + camera: Arc, +} + +impl SnapThread { + async fn run(&mut self) -> Result<()> { + let mut inter = interval(Duration::from_millis(500)); + loop { + inter.tick().await; + let snapshot = self.camera.get_snapshot().await?; + self.tx.send(Messages::Snap(snapshot)).await?; + } + } +} + struct KeepaliveThread { camera: Arc, } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 1ebd924..4f8db09 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1,3 +1,4 @@ +use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; /// /// # Neolink MQTT /// @@ -71,9 +72,9 @@ use log::*; use mqttc::{Mqtt, MqttReplyRef}; use self::{ + discovery::enable_discovery, event_cam::EventCamSender, mqttc::{MqttReply, MqttSender}, - discovery::enable_discovery, }; /// Entry point for the mqtt subcommand @@ -135,7 +136,8 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi tokio::select! { v = async { // Normal poll operations - while let Ok(msg) = mqtt.poll().await { + loop { + let msg = mqtt.poll().await?; tokio::task::yield_now().await; // Put the reply on it's own async thread so we can safely sleep // and wait for it to reply in it's own time @@ -152,7 +154,6 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi } }); } - Ok(()) } => v, // Wait on any error from any of the error channels and if we get it we abort v = error_recv.recv() => v.map(Err).unwrap_or_else(|| Err(anyhow!("Listen on camera error channel closed"))), @@ -213,6 +214,14 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi format!("Failed to publish motion start for {}", camera_name) })?; } + Messages::Snap(data) => { + mqtt_sender_cam + .send_message("status/preview", BASE64.encode(data).as_str(), true) + .await + .with_context(|| { + format!("Failed to publish preview over MQTT for {}", camera_name) + })?; + } _ => {} } } diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index 6dea40d..6f49954 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -168,6 +168,8 @@ impl Mqtt { &config.broker_addr, config.port, ); + let max_size = 100 * (1024 * 1024); + mqttoptions.set_max_packet_size(max_size, max_size); // Use TLS if ca path is set if let Some(ca_path) = &config.ca {