Merge remote-tracking branch 'upstream/master' into feature-ptz-preset

This commit is contained in:
Andrew committed 2023-05-25 17:44:56 +01:00
commit b8dafbb92e
12 files changed
+427 -69

No files matched your search

+102 -3
View File
@@ -1,6 +1,7 @@
use crate::config::CameraConfig;
use crate::utils::AddressOrUid;
use anyhow::{anyhow, Context, Result};
use futures::stream::StreamExt;
use log::*;
use neolink_core::bc_protocol::{
BcCamera, Direction as BcDirection, Error as BcError, LightState, MaxEncryption, MotionStatus,
@@ -9,17 +10,17 @@ use std::sync::Arc;
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
task::JoinSet,
time::sleep,
time::{interval, sleep, Duration},
};
use core::time::Duration;
#[derive(Debug, Copy, Clone)]
pub(crate) enum Messages {
Login,
MotionStop,
MotionStart,
Reboot,
FloodlightOn,
FloodlightOff,
StatusLedOn,
StatusLedOff,
IRLedOn,
@@ -204,6 +205,15 @@ impl EventCamThread {
camera: arc_cam.clone(),
};
let mut flight_thread = FloodlightThread {
tx: self.tx.clone(),
camera: arc_cam.clone(),
};
let mut keepalive_thread = KeepaliveThread {
camera: arc_cam.clone(),
};
let mut message_handler = MessageHandler {
rx: &mut self.rx,
camera: arc_cam,
@@ -222,6 +232,30 @@ impl EventCamThread {
Ok(())
}
},
val = async {
debug!("{}: Starting Pings", camera_config.name);
keepalive_thread.run().await
} => {
if let Err(e) = val {
debug!("Ping thread aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on Ping thread");
Ok(())
}
},
val = async {
info!("{}: Listening to FloodLight Status", camera_config.name);
flight_thread.run().await
} => {
if let Err(e) = val {
error!("FloodLight thread aborted: {:?}", e);
Err(e)
} else {
debug!("Normal finish on FloodLight thread");
Ok(())
}
},
val = async {
info!("{}: Setting up camera actions", camera_config.name);
message_handler.listen().await
@@ -273,6 +307,47 @@ impl MotionThread {
}
}
struct FloodlightThread {
tx: Sender<Messages>,
camera: Arc<BcCamera>,
}
impl FloodlightThread {
async fn run(&mut self) -> Result<()> {
let mut reciever =
tokio_stream::wrappers::ReceiverStream::new(self.camera.listen_on_flightlight().await?);
while let Some(flights) = reciever.next().await {
for flight in flights.floodlight_status_list.iter() {
if flight.status == 0 {
self.tx.send(Messages::FloodlightOff).await?;
} else {
self.tx.send(Messages::FloodlightOn).await?;
}
}
}
Ok(())
}
}
struct KeepaliveThread {
camera: Arc<BcCamera>,
}
impl KeepaliveThread {
async fn run(&mut self) -> Result<()> {
let mut interval =
tokio_stream::wrappers::IntervalStream::new(interval(Duration::from_secs(5)));
while let Some(_update) = interval.next().await {
if self.camera.ping().await.is_err() {
break;
}
}
futures::pending!(); // Never actually finish, has to be aborted
Ok(())
}
}
struct MessageHandler<'a> {
rx: &'a mut Receiver<ToCamera>,
camera: Arc<BcCamera>,
@@ -298,6 +373,30 @@ impl<'a> MessageHandler<'a> {
"OK".to_string()
}
}
Messages::FloodlightOn => {
let res = self.camera.set_floodlight_manual(true, 180).await;
if res.is_err() {
error = Some(format!(
"Failed to turn on the floodlight light: {:?}",
res.err()
));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::FloodlightOff => {
let res = self.camera.set_floodlight_manual(false, 180).await;
if res.is_err() {
error = Some(format!(
"Failed to turn off the floodlight light: {:?}",
res.err()
));
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::StatusLedOn => {
if let Err(e) = self.camera.led_light_set(true).await {
error =
+44
View File
@@ -168,6 +168,28 @@ async fn listen_on_camera(cam_config: Arc<CameraConfig>, mqtt_config: &MqttConfi
format!("Failed to post connect over MQTT for {}", camera_name)
})?;
}
Messages::FloodlightOn => {
mqtt_sender_cam
.send_message("status/floodlight", "on", true)
.await
.with_context(|| {
format!(
"Failed to publish gloodlight on over MQTT for {}",
camera_name
)
})?;
}
Messages::FloodlightOff => {
mqtt_sender_cam
.send_message("status/floodlight", "off", true)
.await
.with_context(|| {
format!(
"Failed to publish gloodlight off over MQTT for {}",
camera_name
)
})?;
}
Messages::MotionStop => {
mqtt_sender_cam
.send_message("status/motion", "off", true)
@@ -215,6 +237,28 @@ async fn handle_mqtt_message(
} => {
// Do nothing for the success/fail replies
}
MqttReplyRef {
topic: "control/floodlight",
message: "on",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::FloodlightOn)
.await
.with_context(|| "Failed to set camera status light on")?,
);
}
MqttReplyRef {
topic: "control/floodlight",
message: "off",
} => {
reply = Some(
event_cam_sender
.send_message_with_reply(Messages::FloodlightOff)
.await
.with_context(|| "Failed to set camera status light off")?,
);
}
MqttReplyRef {
topic: "control/led",
message: "on",
+10 -6
View File
@@ -264,12 +264,16 @@ impl Mqtt {
impl Drop for Mqtt {
fn drop(&mut self) {
if let Some(drop_message) = self.drop_message.as_ref() {
if self
.get_sender()
.try_send_message(&drop_message.topic, &drop_message.message, true)
.is_err()
{
error!("Failed to send offline message to mqtt");
let res = self.get_sender().try_send_message(
&drop_message.topic,
&drop_message.message,
true,
);
if res.is_err() {
error!(
"Failed to send offline message to mqtt: {:?}. Is the MQTT topic name valid?",
res.err().unwrap()
);
}
}
self.set.abort_all();