Add camera preview into mqtt via snap shot command
This commit is contained in:
8 files changed
+91
-8
No files matched your search
+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