Add camera preview into mqtt via snap shot command
This commit is contained in:
8 files changed
+91
-8
No files matched your search
Generated
+4
-3
@@ -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]]
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -97,6 +97,9 @@ pub struct BcXml {
|
||||
/// Recieved on request for a link type
|
||||
#[yaserde(rename = "LinkType")]
|
||||
pub link_type: Option<LinkType>,
|
||||
/// Recieved AND send for the snap message
|
||||
#[yaserde(rename = "Snap")]
|
||||
pub snap: Option<Snap>,
|
||||
}
|
||||
|
||||
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<u8>,
|
||||
/// 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<u32>,
|
||||
/// Stream name, observed values: `main`, `sub`
|
||||
/// value is only set on request
|
||||
#[yaserde(rename = "streamType")]
|
||||
pub stream_type: Option<String>,
|
||||
/// File name, usually of the form `01_20230518140240.jpg`
|
||||
/// value is only set on recieve
|
||||
#[yaserde(rename = "fileName")]
|
||||
pub file_name: Option<String>,
|
||||
/// Size in bytes of the picture
|
||||
/// value is only set on recieve
|
||||
#[yaserde(rename = "pictureSize")]
|
||||
pub picture_size: Option<u32>,
|
||||
}
|
||||
|
||||
/// Convience function to return the xml version used throughout the library
|
||||
|
||||
@@ -27,6 +27,7 @@ mod pirstate;
|
||||
mod ptz;
|
||||
mod reboot;
|
||||
mod resolution;
|
||||
mod snap;
|
||||
mod stream;
|
||||
mod talk;
|
||||
mod time;
|
||||
|
||||
+35
-1
@@ -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<u8>),
|
||||
}
|
||||
|
||||
#[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<Messages>,
|
||||
camera: Arc<BcCamera>,
|
||||
}
|
||||
|
||||
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<BcCamera>,
|
||||
}
|
||||
|
||||
+12
-3
@@ -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<CameraConfig>, 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<CameraConfig>, 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<CameraConfig>, 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)
|
||||
})?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user