From 45fec7cfc2a9e4c1f56b3afb292ae9a9dcc5de39 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 18 Apr 2023 20:27:53 +0700 Subject: [PATCH 1/6] Update mqtt to use asyncio --- .../src/bc_protocol/connection/discovery.rs | 3 + crates/core/src/bc_protocol/motion.rs | 1 + crates/core/src/bc_protocol/stream.rs | 1 + src/image/gst.rs | 1 + src/mqtt/event_cam.rs | 1 + src/mqtt/mod.rs | 49 ++- src/mqtt/mqttc.rs | 324 ++++++++++-------- src/rtsp/gst/factory.rs | 1 + src/rtsp/mod.rs | 1 + src/rtsp/states/streaming.rs | 1 + 10 files changed, 217 insertions(+), 166 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index efebdf8..e44e022 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -101,6 +101,7 @@ impl Discoverer { let thread_subscriber = subsribers.clone(); set.spawn(async move { loop { + tokio::task::yield_now().await; match reader.next().await { Some(Ok((BcUdp::Discovery(bcudp), addr))) => { let mut tid = bcudp.tid; @@ -852,6 +853,7 @@ impl Discoverer { let mut interval = interval(Duration::from_secs(1)); self.handle.write().await.spawn(async move { loop { + tokio::task::yield_now().await; interval.tick().await; let msg = BcUdp::Discovery(UdpDiscovery { tid, @@ -880,6 +882,7 @@ impl Discoverer { let mut interval = interval(Duration::from_secs(1)); self.handle.write().await.spawn(async move { loop { + tokio::task::yield_now().await; interval.tick().await; let msg = BcUdp::Discovery(UdpDiscovery { tid, diff --git a/crates/core/src/bc_protocol/motion.rs b/crates/core/src/bc_protocol/motion.rs index 8d1bf90..a8fae78 100644 --- a/crates/core/src/bc_protocol/motion.rs +++ b/crates/core/src/bc_protocol/motion.rs @@ -216,6 +216,7 @@ impl BcCamera { let mut sub = connection.subscribe(msg_num).await?; loop { + tokio::task::yield_now().await; let msg = sub.recv().await; let status = match msg { Ok(motion_msg) => { diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 4528c2c..5f5811e 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -105,6 +105,7 @@ impl BcCamera { let channel_id = self.channel_id; let handle = task::spawn(async move { + tokio::task::yield_now().await; let mut sub_video = connection.subscribe(msg_num).await?; // On an E1 and swann cameras: diff --git a/src/image/gst.rs b/src/image/gst.rs index 930a234..1ef6e43 100644 --- a/src/image/gst.rs +++ b/src/image/gst.rs @@ -65,6 +65,7 @@ async fn output(pipeline: Pipeline) -> Result { let mut set = JoinSet::new(); set.spawn(async move { while let Some(control) = reciever.recv().await { + tokio::task::yield_now().await; match control { GstControl::Data(buf) => { let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap(); diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index dbc4779..efe731a 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -130,6 +130,7 @@ impl EventCam { while self.app.running(&self.config.name) { // Ignore errors and just loop + tokio::task::yield_now().await; let _ = Self::cam_run( &loop_config, loop_tx.clone(), diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 37d4faf..38a5ac2 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -93,6 +93,7 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> { info!("{}: Setting up mqtt", camera_config.name); set.spawn(async move { while loop_arc_app.running("app") { + tokio::task::yield_now().await; let _ = listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone()) .await; @@ -115,7 +116,7 @@ async fn listen_on_camera( ) -> Result<()> { // Camera thread let arc_event_cam = Arc::new(EventCam::new(cam_config.clone(), arc_app.clone())); - let arc_mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone()); + let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone()).await; let mut set = tokio::task::JoinSet::new(); // Start listening to camera events let event_cam = arc_event_cam.clone(); @@ -124,35 +125,40 @@ async fn listen_on_camera( event_cam.abort(); // Just to ensure everything aborts }); - // Start listening to mqtt events - let event_cam = arc_event_cam.clone(); - let mqtt = arc_mqtt.clone(); - set.spawn(async move { - let _ = mqtt.start().is_err(); - event_cam.abort(); - }); - // Listen on camera messages and post on mqtt let camera_name = cam_config.name.clone(); let event_cam = arc_event_cam.clone(); - let mqtt = arc_mqtt.clone(); let app = arc_app.clone(); + let mqtt_sender = mqtt.get_sender(); set.spawn(async move { while app.running(&format!("app: {}", camera_name)) { + tokio::task::yield_now().await; if let Ok(msg) = event_cam.poll().await { match msg { Messages::Login => { - if mqtt.send_message("status", "connected", true).is_err() { + if mqtt_sender + .send_message("status", "connected", true) + .await + .is_err() + { error!("Failed to post connect over MQTT for {}", camera_name); } } Messages::MotionStop => { - if mqtt.send_message("status/motion", "off", true).is_err() { + if mqtt_sender + .send_message("status/motion", "off", true) + .await + .is_err() + { error!("Failed to publish motion stop for {}", camera_name); } } Messages::MotionStart => { - if mqtt.send_message("status/motion", "on", true).is_err() { + if mqtt_sender + .send_message("status/motion", "on", true) + .await + .is_err() + { error!("Failed to publish motion start for {}", camera_name); } } @@ -164,12 +170,13 @@ async fn listen_on_camera( // Listen on mqtt messages and post on camera let event_cam = arc_event_cam.clone(); - let mqtt = arc_mqtt.clone(); let app = arc_app.clone(); let camera_name = cam_config.name.clone(); + let mqtt_sender = mqtt.get_sender(); set.spawn(async move { while app.running(&format!("app: {}", camera_name)) { - if let Ok(msg) = mqtt.poll() { + tokio::task::yield_now().await; + if let Ok(msg) = mqtt.poll().await { match msg.as_ref() { MqttReplyRef { topic: "control/led", @@ -279,7 +286,11 @@ async fn listen_on_camera( .. } => match event_cam.send_message_with_reply(Messages::Battery).await { Ok(reply) => { - if mqtt.send_message("status/battery", &reply, false).is_err() { + if mqtt_sender + .send_message("status/battery", &reply, false) + .await + .is_err() + { error!("Failed to send battery status reply"); } } @@ -289,7 +300,11 @@ async fn listen_on_camera( topic: "query/pir", .. } => match event_cam.send_message_with_reply(Messages::PIRQuery).await { Ok(reply) => { - if mqtt.send_message("status/pir", &reply, false).is_err() { + if mqtt_sender + .send_message("status/pir", &reply, false) + .await + .is_err() + { error!("Failed to send pir status reply"); } } diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index ba15f67..1d9686d 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -1,23 +1,21 @@ use super::App; use crate::config::MqttConfig; use anyhow::{Context, Result}; -use crossbeam_channel::{unbounded, Receiver, RecvError, Sender}; use log::*; use rumqttc::{ - Client, ClientError, ConnectReturnCode, Connection, Event, Incoming, Key, LastWill, + AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill, MqttOptions, Publish, QoS, TlsConfiguration, Transport, }; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::Duration; +use tokio::sync::mpsc::{channel, Receiver, Sender}; +use tokio::task::JoinSet; pub(crate) struct Mqtt { - app: Arc, - client: Mutex, - connection: Mutex, + client: Arc, name: String, - incoming: (Sender, Receiver), - msg_channel: (Sender, Receiver), - drop_message: Mutex>, + incoming: Receiver, + drop_message: Option, } pub(crate) struct MqttReply { @@ -39,10 +37,121 @@ pub(crate) struct MqttReplyRef<'a> { pub(crate) message: &'a str, } +struct MqttReciever { + app: Arc, + client: MqttSender, + incoming_tx: Sender, + name: String, +} + +pub(crate) struct MqttSender { + client: Arc, + name: String, +} + +impl MqttSender { + pub async fn send_message( + &self, + sub_topic: &str, + message: &str, + retain: bool, + ) -> Result<(), ClientError> { + self.client + .publish( + format!("neolink/{}/{}", self.name, sub_topic), + QoS::AtLeastOnce, + retain, + message, + ) + .await?; + Ok(()) + } + + async fn subscribe(&self) -> Result<(), ClientError> { + self.client + .subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce) + .await?; + Ok(()) + } + + async fn update_status(&self) -> Result<()> { + self.send_message("status", "disconnected", true).await?; + Ok(()) + } + + pub fn try_send_message(&self, sub_topic: &str, message: &str, retain: bool) -> Result<()> { + self.client + .try_publish( + format!("neolink/{}/{}", self.name, sub_topic), + QoS::AtLeastOnce, + retain, + message, + ) + .map_err(|e| e.into()) + } +} + +impl MqttReciever { + async fn run(&mut self, connection: &mut EventLoop) -> Result<()> { + // This acts as an event loop + let name = self.name.clone(); + info!("Starting MQTT Client for {}", name); + while self.app.running(&format!("app:{}", name)) { + if let Ok(notification) = connection.poll().await { + // if let Ok(notification) = notification { + match notification { + Event::Incoming(Incoming::ConnAck(connected)) => { + if ConnectReturnCode::Success == connected.code { + self.client + .update_status() + .await + .context("Failed to update status")?; + // We succesfully logged in. Now ask for the cameras subscription. + self.client + .subscribe() + .await + .context("Failed to subscribe")?; + } + } + Event::Incoming(Incoming::Publish(published_message)) => { + if self.handle_message(published_message).await.is_err() { + error!("Failed to forward messages in mqtt"); + } + } + _ => {} + } + // } + } + } + Ok(()) + } + + async fn handle_message(&mut self, published_message: Publish) -> Result<()> { + if let Some(sub_topic) = published_message + .topic + .strip_prefix(&format!("neolink/{}/", &self.name)) + { + if self + .incoming_tx + .send(MqttReply { + topic: sub_topic.to_string(), + message: String::from_utf8_lossy(published_message.payload.as_ref()) + .into_owned(), + }) + .await + .is_err() + { + error!("Failed to send messages up the mqtt msg channel"); + } + } + + Ok(()) + } +} + impl Mqtt { - pub(crate) fn new(config: &MqttConfig, name: &str, app: Arc) -> Arc { - let incoming = unbounded::(); - let msg_channel = unbounded::(); + pub(crate) async fn new(config: &MqttConfig, name: &str, app: Arc) -> Self { + let (incoming_tx, incoming) = channel::(100); let mut mqttoptions = MqttOptions::new( format!("Neolink-{}", name), &config.broker_addr, @@ -91,158 +200,75 @@ impl Mqtt { true, )); - let (client, connection) = Client::new(mqttoptions, 10); + let (client, mut connection) = AsyncClient::new(mqttoptions, 10); - let me = Self { - app: app.clone(), - client: Mutex::new(client), - connection: Mutex::new(connection), - name: name.to_string(), - incoming, - msg_channel, - // Send the drop message on clean disconnect - drop_message: Mutex::new(Some(MqttReply { - topic: "status".to_string(), - message: "offline".to_string(), - })), - }; - - let arc_me = Arc::new(me); + let client = Arc::new(client); // Start the mqtt server - let mqtt_running = arc_me.clone(); - let mqtt_app = app.clone(); - let mqtt_name = name.to_string(); - std::thread::spawn(move || { - let _ = (*mqtt_running).start(); - mqtt_app.abort(&mqtt_name) - }); + let mut set = JoinSet::>::new(); - // Start polling messages - info!("{}: Starting listening to mqtt", name); - let mqtt_read_app = app; - let mqtt_reading = arc_me.clone(); - let mqtt_read_name = name.to_string(); - std::thread::spawn(move || { - while mqtt_read_app.running(&format!("app:{}", mqtt_read_name)) { - if (*mqtt_reading).handle_message().is_err() { - error!( - "Failed to get messages from mqtt client {}", - (mqtt_read_name) - ); - } - } - }); + let mut reciever = MqttReciever { + app: app.clone(), + incoming_tx, + name: name.to_string(), + client: MqttSender { + client: client.clone(), + name: name.to_string(), + }, + }; + set.spawn(async move { reciever.run(&mut connection).await }); - arc_me - } - - #[allow(unused)] - pub(crate) fn set_drop_message(&mut self, topic: &str, message: &str) { - self.drop_message.lock().unwrap().replace(MqttReply { - topic: topic.to_string(), - message: message.to_string(), - }); - } - - fn subscribe(&self) -> Result<(), ClientError> { - let mut client = self.client.lock().unwrap(); - client.subscribe(format!("neolink/{}/#", self.name), QoS::AtMostOnce)?; - Ok(()) - } - - fn update_status(&self) -> Result<(), ClientError> { - self.send_message("status", "disconnected", true)?; - Ok(()) - } - - pub fn send_message( - &self, - sub_topic: &str, - message: &str, - retain: bool, - ) -> Result<(), ClientError> { - let mut client = self.client.lock().unwrap(); - client.publish( - format!("neolink/{}/{}", self.name, sub_topic), - QoS::AtLeastOnce, - retain, - message, - )?; - Ok(()) - } - - fn handle_message(&self) -> Result<(), RecvError> { - let (_, receiver) = &self.incoming; - let published_message = receiver.recv()?; - - if let Some(sub_topic) = published_message - .topic - .strip_prefix(&format!("neolink/{}/", &self.name)) - { - if self - .msg_channel - .0 - .send(MqttReply { - topic: sub_topic.to_string(), - message: String::from_utf8_lossy(published_message.payload.as_ref()) - .into_owned(), - }) - .is_err() - { - error!("Failed to send messages up the mqtt msg channel"); - } - } - - Ok(()) - } - - pub fn get_message_listener(&self) -> Receiver { - self.msg_channel.1.clone() - } - - pub(crate) fn poll(&self) -> Result { - self.get_message_listener() - .recv() - .context("Mqtt Polling error") - } - - pub(crate) fn start(&self) -> Result<()> { - // This acts as an event loop - let mut connection = self.connection.lock().unwrap(); - let (sender, _) = &self.incoming; - info!("Starting MQTT Client for {}", self.name); - while self.app.running(&format!("app:{}", self.name)) { - for (_i, notification) in connection.iter().enumerate() { - if let Ok(notification) = notification { - match notification { - Event::Incoming(Incoming::ConnAck(connected)) => { - if ConnectReturnCode::Success == connected.code { - self.update_status().context("Failed to update status")?; - // We succesfully logged in. Now ask for the cameras subscription. - self.subscribe().context("Failed to subscribe")?; - } - } - Event::Incoming(Incoming::Publish(published_message)) => { - if sender.send(published_message).is_err() { - error!("Failed to publish motion message on mqtt"); - } - } - _ => {} + let join_app = app; + let join_name = name.to_string(); + tokio::task::spawn(async move { + while let Some(joined) = set.join_next().await { + tokio::task::yield_now().await; + match joined { + Err(e) => { + error!("MQTT reciever panicked: {:?}", e); + join_app.abort(&join_name); + } + Ok(Err(e)) => { + error!("MQTT stopped with error: {:?}", e); + join_app.abort(&join_name); + } + Ok(Ok(())) => { + info!("Normal shutdown of MQTT"); } } } + }); + + Self { + client, + name: name.to_string(), + incoming, + // Send the drop message on clean disconnect + drop_message: Some(MqttReply { + topic: "status".to_string(), + message: "offline".to_string(), + }), } - Ok(()) + } + + pub fn get_sender(&self) -> MqttSender { + MqttSender { + client: self.client.clone(), + name: self.name.to_string(), + } + } + + pub(crate) async fn poll(&mut self) -> Result { + self.incoming.recv().await.context("Mqtt Polling error") } } impl Drop for Mqtt { fn drop(&mut self) { - let drop_message = self.drop_message.lock().unwrap(); - if let Some(drop_message) = drop_message.as_ref() { + if let Some(drop_message) = self.drop_message.as_ref() { if self - .send_message(&drop_message.topic, &drop_message.message, true) + .get_sender() + .try_send_message(&drop_message.topic, &drop_message.message, true) .is_err() { error!("Failed to send offline message to mqtt"); diff --git a/src/rtsp/gst/factory.rs b/src/rtsp/gst/factory.rs index 81bd512..e01b5c6 100644 --- a/src/rtsp/gst/factory.rs +++ b/src/rtsp/gst/factory.rs @@ -141,6 +141,7 @@ impl Default for NeoMediaFactoryImpl { }; threads.spawn(async move { loop { + tokio::task::yield_now().await; match sender.run().await { Err(e) => { warn!("Media send thead failed... restarting. Cause: {:?}", e); diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 723376c..e07d11d 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -97,6 +97,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { let name = camera.get_name(); let mut backoff = Duration::from_micros(125); loop { + tokio::task::yield_now().await; let failure = camera_main(camera).await; match failure { Err(CameraFailureKind::Fatal(e)) => { diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index 2cfaa97..d1593e6 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -52,6 +52,7 @@ impl Camera { .await?; set.spawn(async move { loop { + tokio::task::yield_now().await; // debug!("Straming: Get"); let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??; // debug!("Straming: Got"); From a46192d47cca6c59eb64770b41f3f071b5148c06 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 19 Apr 2023 11:47:30 +0700 Subject: [PATCH 2/6] Do discovery sequentially --- crates/core/src/bc_protocol.rs | 144 +++++++++++---------------------- 1 file changed, 46 insertions(+), 98 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 11760ac..681a588 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -124,12 +124,6 @@ impl BcCamera { /// Try to connect to the camera via appropaite methods and return /// the location that should be used async fn find_camera(options: &BcCameraOpt) -> Result { - let mut tcp_set = tokio::task::JoinSet::new(); - let mut local_set = tokio::task::JoinSet::new(); - let mut remote_set = tokio::task::JoinSet::new(); - let mut map_set = tokio::task::JoinSet::new(); - let mut relay_set = tokio::task::JoinSet::new(); - if let ConnectionProtocol::Tcp | ConnectionProtocol::TcpUdp = options.protocol { let mut sockets = vec![]; match options.port { @@ -148,12 +142,12 @@ impl BcCamera { for socket in sockets.drain(..) { let channel_id: u8 = options.channel_id; let name: String = options.name.clone(); - tcp_set.spawn(async move { - Discovery::check_tcp(socket, channel_id).await.map(|_| { - info!("{}: TCP Discovery success at {:?}", name, &socket); - socket - }) - }); + if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| { + info!("{}: TCP Discovery success at {:?}", name, &socket); + socket + }) { + return Ok(CameraLocation::Tcp(addr)); + } } } @@ -186,108 +180,62 @@ impl BcCamera { }; if allow_local { - let uid_local = uid.clone(); let name: String = options.name.clone(); - local_set.spawn(async move { - trace!("{}: Starting Local discovery", name); - let result = Discovery::local(&uid_local, Some(sockets)).await; - if let Ok(disc) = &result { - info!( - "{}: Local discovery success {} at {}", - name, - uid_local, - disc.get_addr() - ); - } - result - }); + let uid_local = uid.clone(); + trace!("{}: Starting Local discovery", name); + let result = Discovery::local(&uid_local, Some(sockets)).await; + if let Ok(disc) = result { + info!( + "{}: Local discovery success {} at {}", + name, + uid_local, + disc.get_addr() + ); + return Ok(CameraLocation::Udp(disc)); + } } if allow_remote { let uid_remote = uid.clone(); let name: String = options.name.clone(); - remote_set.spawn(async move { - trace!("Starting Remote discovery"); - let result = Discovery::remote(&uid_remote).await; - if let Ok(disc) = &result { - info!( - "{}: Remote discovery success {} at {}", - name, - uid_remote, - disc.get_addr() - ); - } - result - }); + trace!("Starting Remote discovery"); + let result = Discovery::remote(&uid_remote).await; + if let Ok(disc) = result { + info!( + "{}: Remote discovery success {} at {}", + name, + uid_remote, + disc.get_addr() + ); + return Ok(CameraLocation::Udp(disc)); + } } if allow_map { let uid_map = uid.clone(); let name: String = options.name.clone(); - map_set.spawn(async move { - trace!("Starting Map"); - let result = Discovery::map(&uid_map).await; - if let Ok(disc) = &result { - info!("{}: Map success {} at {}", name, uid_map, disc.get_addr()); - } - result - }); + trace!("Starting Map"); + let result = Discovery::map(&uid_map).await; + if let Ok(disc) = result { + info!("{}: Map success {} at {}", name, uid_map, disc.get_addr()); + return Ok(CameraLocation::Udp(disc)); + } } if allow_relay { let uid_relay = uid.clone(); let name: String = options.name.clone(); - relay_set.spawn(async move { - trace!("Starting Relay"); - let result = Discovery::relay(&uid_relay).await; - if let Ok(disc) = &result { - info!( - "{}: Relay success {} at {}", - name, - uid_relay, - disc.get_addr() - ); - } - result - }); + trace!("Starting Relay"); + let result = Discovery::relay(&uid_relay).await; + if let Ok(disc) = result { + info!( + "{}: Relay success {} at {}", + name, + uid_relay, + disc.get_addr() + ); + return Ok(CameraLocation::Udp(disc)); + } } } - // Wait for all TCP to finish - while let Some(result) = tcp_set.join_next().await { - if let Ok(Ok(addr)) = result { - return Ok(CameraLocation::Tcp(addr)); - } - } - debug!("TCP fail"); - // Tcp failed see if Local faired any better - // Wait for all local to finish - while let Some(result) = local_set.join_next().await { - if let Ok(Ok(discovery)) = result { - return Ok(CameraLocation::Udp(discovery)); - } - } - debug!("Local fail"); - // Local failed see if Remote faired any better - // Wait for all remote to finish - while let Some(result) = remote_set.join_next().await { - if let Ok(Ok(discovery)) = result { - return Ok(CameraLocation::Udp(discovery)); - } - } - debug!("Remote fail"); - // Remote failed see if Map faired any better - // Wait for all Map to finish - while let Some(result) = map_set.join_next().await { - if let Ok(Ok(discovery)) = result { - return Ok(CameraLocation::Udp(discovery)); - } - } - debug!("Map fail"); - // Map failed see if Relay faired any better - // Wait for all Relay to finish - while let Some(result) = relay_set.join_next().await { - if let Ok(Ok(discovery)) = result { - return Ok(CameraLocation::Udp(discovery)); - } - } debug!("Relay (All) fail"); // Nothing works Err(Error::CannotInitCamera) From 19a468ac2ecf6f7c27be670efbb218abf24d234c Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 19 Apr 2023 11:51:50 +0700 Subject: [PATCH 3/6] Squash more blocking in async on mqttt --- src/mqtt/event_cam.rs | 583 +++++++++++++++++++++++------------------- src/mqtt/mod.rs | 152 ++++++----- src/mqtt/mqttc.rs | 6 +- 3 files changed, 409 insertions(+), 332 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index efe731a..89b4fa4 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -4,17 +4,16 @@ use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; use log::*; use neolink_core::bc_protocol::{ - BcCamera, Direction as BcDirection, Error as BcError, LightState, MotionStatus, + BcCamera, Direction as BcDirection, Error as BcError, LightState, MaxEncryption, MotionStatus, }; use std::sync::Arc; -use tokio::sync::{ - mpsc::{channel, error::TryRecvError, Receiver, Sender}, - Mutex, +use tokio::{ + sync::mpsc::{channel, Receiver, Sender}, + task::JoinSet, }; #[derive(Debug, Copy, Clone)] pub(crate) enum Messages { - None, Login, MotionStop, MotionStart, @@ -50,103 +49,119 @@ enum ToCamera { }, } -pub(crate) struct EventCam { - config: Arc, - app: Arc, - channel_out: Arc>>>, - channel_in: Arc>>>, +#[derive(Clone)] +pub(crate) struct EventCamSender { + channel_in: Sender, } -impl EventCam { - pub(crate) fn new(config: Arc, app: Arc) -> Self { - Self { - config, - app, - channel_out: Arc::new(Mutex::new(None)), - channel_in: Arc::new(Mutex::new(None)), - } - } - - pub(crate) async fn poll(&self) -> Result { - if let Ok(ref mut channel_out) = self.channel_out.try_lock() { - if let Some(channel_out) = channel_out.as_mut() { - channel_out.recv().await.context("Camera failed to poll") - } else { - Ok(Messages::None) - } - } else { - Ok(Messages::None) - } - } - +impl EventCamSender { pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> { - let mut channel_in = self.channel_in.lock().await; - if let Some(channel_in) = channel_in.as_mut() { - channel_in - .send(ToCamera::Send(msg)) - .await - .context("Failed to send message from camera") - } else { - Err(anyhow!("Failed to send camera data over channel")) - } + self.channel_in + .send(ToCamera::Send(msg)) + .await + .context("Failed to send message from camera") } pub(crate) async fn send_message_with_reply(&self, msg: Messages) -> Result { - let mut channel_in = self.channel_in.lock().await; - if let Some(channel_in) = channel_in.as_mut() { - let (tx, mut rx) = channel(1); - channel_in - .send(ToCamera::SendAndReply { - message: msg, - reply: tx, - }) - .await - .context("Failed to send message from camera")?; - tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv()) - .await - .context("Failed to recieve reply Timeout")? - .ok_or_else(|| anyhow!("Failed to recieve reply")) - } else { - Err(anyhow!("Failed to send camera data over crossbeam")) - } + let (tx, mut rx) = channel(1); + self.channel_in + .send(ToCamera::SendAndReply { + message: msg, + reply: tx, + }) + .await + .context("Failed to send message from camera")?; + tokio::time::timeout(std::time::Duration::from_secs(3), rx.recv()) + .await + .context("Failed to recieve reply Timeout")? + .ok_or_else(|| anyhow!("Failed to recieve reply")) } +} +pub(crate) struct EventCam { + sender: EventCamSender, + channel_out: Receiver, +} - pub(crate) fn abort(&self) { - self.app.abort(&self.config.name); - } - - pub(crate) async fn start_listening(&self) { - let loop_config = self.config.clone(); - +impl EventCam { + pub(crate) async fn new(config: Arc, app: Arc) -> Self { // Channels from the camera let (tx, rx) = channel(40); - let loop_tx = Arc::new(Mutex::new(tx)); - *self.channel_out.lock().await = Some(rx); // Channels to the camera let (stx, srx) = channel(40); - let loop_rx = Arc::new(Mutex::new(srx)); - *self.channel_in.lock().await = Some(stx); - while self.app.running(&self.config.name) { - // Ignore errors and just loop - tokio::task::yield_now().await; - let _ = Self::cam_run( - &loop_config, - loop_tx.clone(), - loop_rx.clone(), - self.app.clone(), - ) - .await; + let name = config.name.clone(); + let mut set = JoinSet::>::new(); + let thread_app = app.clone(); + let thread_config = config.clone(); + let mut eventcam_thread = EventCamThread { + rx: srx, + tx, + app: app.clone(), + }; + set.spawn(async move { + let mut wait_for = tokio::time::Duration::from_micros(125); + while thread_app.running(&name) { + // Ignore errors and just loop + tokio::task::yield_now().await; + if let Err(e) = eventcam_thread.cam_run(&thread_config).await { + warn!("Camera thread error: {:?}. Restarting...", e); + tokio::time::sleep(wait_for).await; + wait_for *= 2; + } + } + Ok(()) + }); + + // Watch the thread and abort the app if it fails + let join_app = app; + let join_name = config.name.to_string(); + tokio::task::spawn(async move { + while let Some(joined) = set.join_next().await { + tokio::task::yield_now().await; + match joined { + Err(e) => { + error!("EventCam panicked: {:?}", e); + join_app.abort(&join_name); + } + Ok(Err(e)) => { + error!("EventCam stopped with error: {:?}", e); + join_app.abort(&join_name); + } + Ok(Ok(())) => { + info!("Normal shutdown of EventCam"); + } + } + info!("EventCam Loop End"); + } + }); + + Self { + sender: EventCamSender { channel_in: stx }, + channel_out: rx, } } - async fn cam_run( - camera_config: &CameraConfig, - tx: Arc>>, - rx: Arc>>, - app: Arc, - ) -> Result<()> { + pub(crate) async fn poll(&mut self) -> Result { + self.channel_out + .recv() + .await + .context("Camera failed to poll") + } + + pub(crate) fn get_sender(&self) -> EventCamSender { + self.sender.clone() + } +} + +struct EventCamThread { + rx: Receiver, + tx: Sender, + app: Arc, +} + +impl EventCamThread { + async fn cam_run(&mut self, camera_config: &CameraConfig) -> Result<()> { let camera_addr = AddressOrUid::new( &camera_config.camera_addr, &camera_config.camera_uid, @@ -169,48 +184,73 @@ impl EventCam { })?; info!("{}: Logging in", camera_config.name); + let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() { + "none" => MaxEncryption::None, + "bcencrypt" => MaxEncryption::BcEncrypt, + "aes" => MaxEncryption::Aes, + _ => MaxEncryption::Aes, + }; camera - .login() + .login_with_maxenc(max_encryption) .await .context("Failed to login to the camera")?; info!("{}: Connected and logged in", camera_config.name); - (tx.lock().await).send(Messages::Login).await?; + self.tx.send(Messages::Login).await?; // Shararble cameras let arc_cam = Arc::new(camera); let mut motion_thread = MotionThread { - app: app.clone(), - tx, + app: self.app.clone(), + tx: self.tx.clone(), camera: arc_cam.clone(), name: camera_config.name.to_string(), }; + let thread_app = self.app.clone(); let mut message_handler = MessageHandler { - app: app.clone(), - rx, + app: self.app.clone(), + rx: &mut self.rx, camera: arc_cam, name: camera_config.name.to_string(), }; - let camera_name = camera_config.name.clone(); - info!("{}: Listening to Camera Motion", camera_config.name); - let handle_motion = tokio::task::spawn(async move { - if let Err(e) = motion_thread.run().await { - error!("Motion thread aborted: {:?}", e); - } - app.abort(&camera_name); - }); - info!("{}: Setting up camera actions", camera_config.name); - let handle_camera = tokio::task::spawn(async move { - message_handler.listen().await; - }); - tokio::select! { - val = async {handle_motion.await} => {val}, - val = async {handle_camera.await} => {val} + val = async { + info!("{}: Listening to Camera Motion", camera_config.name); + motion_thread.run().await + } => { + if let Err(e) = val { + error!("Motion thread aborted: {:?}", e); + Err(e) + } else { + debug!("Normal finish on motion thread"); + Ok(()) + } + }, + val = async { + info!("{}: Setting up camera actions", camera_config.name); + message_handler.listen().await + } => { + if let Err(e) = val { + error!("Camera message handler aborted: {:?}", e); + Err(e) + } else { + debug!("Normal finish on camera message thread"); + Ok(()) + } + }, + val = async { + while thread_app.running(&format!("app:{}", camera_config.name)) { + tokio::time::sleep(tokio::time::Duration::from_micros(500)).await + } + } => { + info!("Normal finish on cam run"); + Result::Ok(()) + }, }?; + self.app.abort(&camera_config.name); Ok(()) } @@ -218,7 +258,7 @@ impl EventCam { struct MotionThread { app: Arc, - tx: Arc>>, + tx: Sender, camera: Arc, name: String, } @@ -226,17 +266,19 @@ struct MotionThread { impl MotionThread { async fn run(&mut self) -> Result<()> { let mut motion_data = self.camera.listen_on_motion().await?; + let mut queued_motion = motion_data.consume_motion_events()?; while self.app.running(&format!("app:{}", self.name)) { - for motion_status in motion_data.consume_motion_events()?.drain(..) { + tokio::task::yield_now().await; + for motion_status in queued_motion.drain(..) { match motion_status { MotionStatus::Start(_) => { - (self.tx.lock().await) + self.tx .send(Messages::MotionStart) .await .map_err(|_| BcError::Other("Failed to send Message"))?; } MotionStatus::Stop(_) => { - (self.tx.lock().await) + self.tx .send(Messages::MotionStop) .await .map_err(|_| BcError::Other("Failed to send Message"))?; @@ -244,182 +286,185 @@ impl MotionThread { _ => {} } } + queued_motion.push(motion_data.next_motion().await?); } Ok(()) } } -struct MessageHandler { +struct MessageHandler<'a> { app: Arc, - rx: Arc>>, + rx: &'a mut Receiver, camera: Arc, name: String, } -impl MessageHandler { - async fn listen(&mut self) { +impl<'a> MessageHandler<'a> { + async fn listen(&mut self) -> Result<()> { while self.app.running(&format!("app:{}", self.name)) { - // Try and lock don't worry if not - if let Ok(ref mut channel_out) = self.rx.try_lock() { - match channel_out.try_recv() { - Ok(to_camera) => { - let (replier, message) = match to_camera { - ToCamera::Send(message) => (None, message), - ToCamera::SendAndReply { message, reply } => (Some(reply), message), - }; - let reply = match message { - Messages::Reboot => { - if self.camera.reboot().await.is_err() { - error!("Failed to reboot the camera"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } + tokio::task::yield_now().await; + match self.rx.recv().await { + Some(to_camera) => { + let (replier, message) = match to_camera { + ToCamera::Send(message) => (None, message), + ToCamera::SendAndReply { message, reply } => (Some(reply), message), + }; + let reply = match message { + Messages::Reboot => { + if self.camera.reboot().await.is_err() { + error!("Failed to reboot the camera"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() } - Messages::StatusLedOn => { - if self.camera.led_light_set(true).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::StatusLedOff => { - if self.camera.led_light_set(false).await.is_err() { - error!("Failed to turn off the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::IRLedOn => { - if self.camera.irled_light_set(LightState::On).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::IRLedOff => { - if self.camera.irled_light_set(LightState::Off).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::IRLedAuto => { - if self.camera.irled_light_set(LightState::Auto).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::Battery => match self.camera.battery_info().await { - Err(_) => { - error!("Failed to get battery status"); - "FAIL".to_string() - } - Ok(battery_info) => { - let bytes_res = yaserde::ser::serialize_with_writer( - &battery_info, - vec![], - &Default::default(), - ); - match bytes_res { - Ok(bytes) => match String::from_utf8(bytes) { - Ok(str) => str, - Err(_) => { - error!("Failed to encode battery status"); - "FAIL".to_string() - } - }, - Err(_) => { - error!("Failed to serialise battery status"); - "FAIL".to_string() - } - } - } - }, - Messages::PIRQuery => match self.camera.get_pirstate().await { - Err(_) => { - error!("Failed to get pir status"); - "FAIL".to_string() - } - Ok(pir_info) => { - let bytes_res = yaserde::ser::serialize_with_writer( - &pir_info, - vec![], - &Default::default(), - ); - match bytes_res { - Ok(bytes) => match String::from_utf8(bytes) { - Ok(str) => str, - Err(_) => { - error!("Failed to encode pir status"); - "FAIL".to_string() - } - }, - Err(_) => { - error!("Failed to serialise pir status"); - "FAIL".to_string() - } - } - } - }, - Messages::PIROn => { - if self.camera.pir_set(true).await.is_err() { - error!("Failed to turn on the pir"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::PIROff => { - if self.camera.pir_set(false).await.is_err() { - error!("Failed to turn on the pir"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::Ptz(direction) => { - let (bc_direction, amount) = match direction { - Direction::Up(amount) => (BcDirection::Up, amount), - Direction::Down(amount) => (BcDirection::Down, amount), - Direction::Left(amount) => (BcDirection::Left, amount), - Direction::Right(amount) => (BcDirection::Right, amount), - Direction::In(amount) => (BcDirection::In, amount), - Direction::Out(amount) => (BcDirection::Out, amount), - }; - if self.camera.send_ptz(bc_direction, amount).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - _ => "UNKNOWN COMMAND".to_string(), - }; - if let Some(replier) = replier { - let _ = replier.send(reply).await; } + Messages::StatusLedOn => { + if self.camera.led_light_set(true).await.is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::StatusLedOff => { + if self.camera.led_light_set(false).await.is_err() { + error!("Failed to turn off the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::IRLedOn => { + if self.camera.irled_light_set(LightState::On).await.is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::IRLedOff => { + if self.camera.irled_light_set(LightState::Off).await.is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::IRLedAuto => { + if self.camera.irled_light_set(LightState::Auto).await.is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::Battery => match self.camera.battery_info().await { + Err(_) => { + error!("Failed to get battery status"); + "FAIL".to_string() + } + Ok(battery_info) => { + let bytes_res = yaserde::ser::serialize_with_writer( + &battery_info, + vec![], + &Default::default(), + ); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => str, + Err(_) => { + error!("Failed to encode battery status"); + "FAIL".to_string() + } + }, + Err(_) => { + error!("Failed to serialise battery status"); + "FAIL".to_string() + } + } + } + }, + Messages::PIRQuery => match self.camera.get_pirstate().await { + Err(_) => { + error!("Failed to get pir status"); + "FAIL".to_string() + } + Ok(pir_info) => { + let bytes_res = yaserde::ser::serialize_with_writer( + &pir_info, + vec![], + &Default::default(), + ); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => str, + Err(_) => { + error!("Failed to encode pir status"); + "FAIL".to_string() + } + }, + Err(_) => { + error!("Failed to serialise pir status"); + "FAIL".to_string() + } + } + } + }, + Messages::PIROn => { + if self.camera.pir_set(true).await.is_err() { + error!("Failed to turn on the pir"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::PIROff => { + if self.camera.pir_set(false).await.is_err() { + error!("Failed to turn on the pir"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + Messages::Ptz(direction) => { + let (bc_direction, amount) = match direction { + Direction::Up(amount) => (BcDirection::Up, amount), + Direction::Down(amount) => (BcDirection::Down, amount), + Direction::Left(amount) => (BcDirection::Left, amount), + Direction::Right(amount) => (BcDirection::Right, amount), + Direction::In(amount) => (BcDirection::In, amount), + Direction::Out(amount) => (BcDirection::Out, amount), + }; + if self.camera.send_ptz(bc_direction, amount).await.is_err() { + error!("Failed to turn on the status light"); + self.abort(); + "FAIL".to_string() + } else { + "OK".to_string() + } + } + _ => "UNKNOWN COMMAND".to_string(), + }; + if let Some(replier) = replier { + let _ = replier.send(reply).await; } - Err(TryRecvError::Empty) => (), - Err(TryRecvError::Disconnected) => self.abort(), + } + None => { + self.abort(); + return Err(anyhow!("Message handller channel dropped")); } } } + + Ok(()) } fn abort(&self) { diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 38a5ac2..8d16086 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -115,61 +115,13 @@ async fn listen_on_camera( arc_app: Arc, ) -> Result<()> { // Camera thread - let arc_event_cam = Arc::new(EventCam::new(cam_config.clone(), arc_app.clone())); + let mut event_cam = EventCam::new(cam_config.clone(), arc_app.clone()).await; let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone()).await; let mut set = tokio::task::JoinSet::new(); - // Start listening to camera events - let event_cam = arc_event_cam.clone(); - set.spawn(async move { - event_cam.start_listening().await; // Loop forever - event_cam.abort(); // Just to ensure everything aborts - }); - - // Listen on camera messages and post on mqtt - let camera_name = cam_config.name.clone(); - let event_cam = arc_event_cam.clone(); - let app = arc_app.clone(); - let mqtt_sender = mqtt.get_sender(); - set.spawn(async move { - while app.running(&format!("app: {}", camera_name)) { - tokio::task::yield_now().await; - if let Ok(msg) = event_cam.poll().await { - match msg { - Messages::Login => { - if mqtt_sender - .send_message("status", "connected", true) - .await - .is_err() - { - error!("Failed to post connect over MQTT for {}", camera_name); - } - } - Messages::MotionStop => { - if mqtt_sender - .send_message("status/motion", "off", true) - .await - .is_err() - { - error!("Failed to publish motion stop for {}", camera_name); - } - } - Messages::MotionStart => { - if mqtt_sender - .send_message("status/motion", "on", true) - .await - .is_err() - { - error!("Failed to publish motion start for {}", camera_name); - } - } - _ => {} - } - } - } - }); + let mqtt_sender_cam = mqtt.get_sender(); // Listen on mqtt messages and post on camera - let event_cam = arc_event_cam.clone(); + let event_cam_sender = event_cam.get_sender(); let app = arc_app.clone(); let camera_name = cam_config.name.clone(); let mqtt_sender = mqtt.get_sender(); @@ -182,7 +134,11 @@ async fn listen_on_camera( topic: "control/led", message: "on", } => { - if event_cam.send_message(Messages::StatusLedOn).await.is_err() { + if event_cam_sender + .send_message(Messages::StatusLedOn) + .await + .is_err() + { error!("Failed to set camera status light on"); } } @@ -190,7 +146,7 @@ async fn listen_on_camera( topic: "control/led", message: "off", } => { - if event_cam + if event_cam_sender .send_message(Messages::StatusLedOff) .await .is_err() @@ -202,7 +158,11 @@ async fn listen_on_camera( topic: "control/ir", message: "on", } => { - if event_cam.send_message(Messages::IRLedOn).await.is_err() { + if event_cam_sender + .send_message(Messages::IRLedOn) + .await + .is_err() + { error!("Failed to set camera status light on"); } } @@ -210,7 +170,11 @@ async fn listen_on_camera( topic: "control/ir", message: "off", } => { - if event_cam.send_message(Messages::IRLedOff).await.is_err() { + if event_cam_sender + .send_message(Messages::IRLedOff) + .await + .is_err() + { error!("Failed to set camera status light off"); } } @@ -218,7 +182,11 @@ async fn listen_on_camera( topic: "control/ir", message: "auto", } => { - if event_cam.send_message(Messages::IRLedAuto).await.is_err() { + if event_cam_sender + .send_message(Messages::IRLedAuto) + .await + .is_err() + { error!("Failed to set camera status light off"); } } @@ -226,7 +194,11 @@ async fn listen_on_camera( topic: "control/reboot", .. } => { - if event_cam.send_message(Messages::Reboot).await.is_err() { + if event_cam_sender + .send_message(Messages::Reboot) + .await + .is_err() + { error!("Failed to set camera status light off"); } } @@ -251,7 +223,7 @@ async fn listen_on_camera( continue; } }; - if event_cam + if event_cam_sender .send_message(Messages::Ptz(direction)) .await .is_err() @@ -269,7 +241,11 @@ async fn listen_on_camera( topic: "control/pir", message: "on", } => { - if event_cam.send_message(Messages::PIROn).await.is_err() { + if event_cam_sender + .send_message(Messages::PIROn) + .await + .is_err() + { error!("Failed to set pir on"); } } @@ -277,14 +253,21 @@ async fn listen_on_camera( topic: "control/pir", message: "off", } => { - if event_cam.send_message(Messages::PIROff).await.is_err() { + if event_cam_sender + .send_message(Messages::PIROff) + .await + .is_err() + { error!("Failed to set pir off"); } } MqttReplyRef { topic: "query/battery", .. - } => match event_cam.send_message_with_reply(Messages::Battery).await { + } => match event_cam_sender + .send_message_with_reply(Messages::Battery) + .await + { Ok(reply) => { if mqtt_sender .send_message("status/battery", &reply, false) @@ -298,7 +281,10 @@ async fn listen_on_camera( }, MqttReplyRef { topic: "query/pir", .. - } => match event_cam.send_message_with_reply(Messages::PIRQuery).await { + } => match event_cam_sender + .send_message_with_reply(Messages::PIRQuery) + .await + { Ok(reply) => { if mqtt_sender .send_message("status/pir", &reply, false) @@ -316,6 +302,48 @@ async fn listen_on_camera( } }); + // Listen on camera messages and post on mqtt + let camera_name = cam_config.name.clone(); + let app = arc_app.clone(); + + set.spawn(async move { + while app.running(&format!("app: {}", camera_name)) { + tokio::task::yield_now().await; + if let Ok(msg) = event_cam.poll().await { + match msg { + Messages::Login => { + if mqtt_sender_cam + .send_message("status", "connected", true) + .await + .is_err() + { + error!("Failed to post connect over MQTT for {}", camera_name); + } + } + Messages::MotionStop => { + if mqtt_sender_cam + .send_message("status/motion", "off", true) + .await + .is_err() + { + error!("Failed to publish motion stop for {}", camera_name); + } + } + Messages::MotionStart => { + if mqtt_sender_cam + .send_message("status/motion", "on", true) + .await + .is_err() + { + error!("Failed to publish motion start for {}", camera_name); + } + } + _ => {} + } + } + } + }); + while set.join_next().await.is_some() {} Ok(()) diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index 1d9686d..b0188a5 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -1,6 +1,6 @@ use super::App; use crate::config::MqttConfig; -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use log::*; use rumqttc::{ AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill, @@ -121,6 +121,9 @@ impl MqttReciever { _ => {} } // } + } else { + error!("Mqtt connection failure"); + return Err(anyhow!("Mqtt connection failure")); } } Ok(()) @@ -237,6 +240,7 @@ impl Mqtt { } } } + info!("MQTT Loop End"); }); Self { From 7468d771788b2ac53c8743f80a25aa0f0f2b0866 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 20 Apr 2023 07:13:54 +0700 Subject: [PATCH 4/6] Pass error up from mqtt, add more detailed error messages --- src/mqtt/event_cam.rs | 165 +++++++---------- src/mqtt/mod.rs | 403 +++++++++++++++++++----------------------- src/mqtt/mqttc.rs | 102 +++++------ 3 files changed, 291 insertions(+), 379 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 89b4fa4..8d0228d 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -1,4 +1,3 @@ -use super::App; use crate::config::CameraConfig; use crate::utils::AddressOrUid; use anyhow::{anyhow, Context, Result}; @@ -80,28 +79,23 @@ impl EventCamSender { pub(crate) struct EventCam { sender: EventCamSender, channel_out: Receiver, + set: JoinSet>, } impl EventCam { - pub(crate) async fn new(config: Arc, app: Arc) -> Self { + pub(crate) async fn new(config: Arc) -> Self { // Channels from the camera let (tx, rx) = channel(40); // Channels to the camera let (stx, srx) = channel(40); - let name = config.name.clone(); let mut set = JoinSet::>::new(); - let thread_app = app.clone(); - let thread_config = config.clone(); - let mut eventcam_thread = EventCamThread { - rx: srx, - tx, - app: app.clone(), - }; + let thread_config = config; + let mut eventcam_thread = EventCamThread { rx: srx, tx }; set.spawn(async move { let mut wait_for = tokio::time::Duration::from_micros(125); - while thread_app.running(&name) { + loop { // Ignore errors and just loop tokio::task::yield_now().await; if let Err(e) = eventcam_thread.cam_run(&thread_config).await { @@ -110,43 +104,37 @@ impl EventCam { wait_for *= 2; } } - Ok(()) - }); - - // Watch the thread and abort the app if it fails - let join_app = app; - let join_name = config.name.to_string(); - tokio::task::spawn(async move { - while let Some(joined) = set.join_next().await { - tokio::task::yield_now().await; - match joined { - Err(e) => { - error!("EventCam panicked: {:?}", e); - join_app.abort(&join_name); - } - Ok(Err(e)) => { - error!("EventCam stopped with error: {:?}", e); - join_app.abort(&join_name); - } - Ok(Ok(())) => { - info!("Normal shutdown of EventCam"); - } - } - info!("EventCam Loop End"); - } }); Self { sender: EventCamSender { channel_in: stx }, channel_out: rx, + set, } } + /// This will also error is the join set errors pub(crate) async fn poll(&mut self) -> Result { - self.channel_out - .recv() - .await - .context("Camera failed to poll") + let (incoming, set) = (&mut self.channel_out, &mut self.set); + tokio::select! { + v = incoming.recv() => v.with_context(|| "Camera Polling error"), + v = async { + while let Some(res) = set.join_next().await { + match res { + Err(e) => { + set.abort_all(); + return Err(e.into()); + } + Ok(Err(e)) => { + set.abort_all(); + return Err(e); + } + Ok(Ok(())) => {} + } + } + Err(anyhow!("Camera background thread dropped without error")) + } => v.with_context(|| "Camera Threads aborted"), + } } pub(crate) fn get_sender(&self) -> EventCamSender { @@ -154,10 +142,15 @@ impl EventCam { } } +impl Drop for EventCam { + fn drop(&mut self) { + self.set.abort_all(); + } +} + struct EventCamThread { rx: Receiver, tx: Sender, - app: Arc, } impl EventCamThread { @@ -202,18 +195,13 @@ impl EventCamThread { let arc_cam = Arc::new(camera); let mut motion_thread = MotionThread { - app: self.app.clone(), tx: self.tx.clone(), camera: arc_cam.clone(), - name: camera_config.name.to_string(), }; - let thread_app = self.app.clone(); let mut message_handler = MessageHandler { - app: self.app.clone(), rx: &mut self.rx, camera: arc_cam, - name: camera_config.name.to_string(), }; tokio::select! { @@ -241,33 +229,22 @@ impl EventCamThread { Ok(()) } }, - val = async { - while thread_app.running(&format!("app:{}", camera_config.name)) { - tokio::time::sleep(tokio::time::Duration::from_micros(500)).await - } - } => { - info!("Normal finish on cam run"); - Result::Ok(()) - }, }?; - self.app.abort(&camera_config.name); Ok(()) } } struct MotionThread { - app: Arc, tx: Sender, camera: Arc, - name: String, } impl MotionThread { async fn run(&mut self) -> Result<()> { let mut motion_data = self.camera.listen_on_motion().await?; let mut queued_motion = motion_data.consume_motion_events()?; - while self.app.running(&format!("app:{}", self.name)) { + loop { tokio::task::yield_now().await; for motion_status in queued_motion.drain(..) { match motion_status { @@ -288,20 +265,17 @@ impl MotionThread { } queued_motion.push(motion_data.next_motion().await?); } - Ok(()) } } struct MessageHandler<'a> { - app: Arc, rx: &'a mut Receiver, camera: Arc, - name: String, } impl<'a> MessageHandler<'a> { async fn listen(&mut self) -> Result<()> { - while self.app.running(&format!("app:{}", self.name)) { + loop { tokio::task::yield_now().await; match self.rx.recv().await { Some(to_camera) => { @@ -309,64 +283,64 @@ impl<'a> MessageHandler<'a> { ToCamera::Send(message) => (None, message), ToCamera::SendAndReply { message, reply } => (Some(reply), message), }; + let mut error = None; let reply = match message { Messages::Reboot => { - if self.camera.reboot().await.is_err() { - error!("Failed to reboot the camera"); - self.abort(); + if let Err(e) = self.camera.reboot().await { + error = Some(format!("Failed to reboot the camera: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::StatusLedOn => { - if self.camera.led_light_set(true).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); + if let Err(e) = self.camera.led_light_set(true).await { + error = + Some(format!("Failed to turn on the status light: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::StatusLedOff => { - if self.camera.led_light_set(false).await.is_err() { - error!("Failed to turn off the status light"); - self.abort(); + if let Err(e) = self.camera.led_light_set(false).await { + error = + Some(format!("Failed to turn off the status light: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::IRLedOn => { - if self.camera.irled_light_set(LightState::On).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); + if let Err(e) = self.camera.irled_light_set(LightState::On).await { + error = + Some(format!("Failed to turn on the status light: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::IRLedOff => { - if self.camera.irled_light_set(LightState::Off).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); + if let Err(e) = self.camera.irled_light_set(LightState::Off).await { + error = + Some(format!("Failed to turn on the status light: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::IRLedAuto => { - if self.camera.irled_light_set(LightState::Auto).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); + if let Err(e) = self.camera.irled_light_set(LightState::Auto).await { + error = + Some(format!("Failed to turn on the status light: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::Battery => match self.camera.battery_info().await { - Err(_) => { - error!("Failed to get battery status"); + Err(e) => { + error!("Failed to get battery status: {:?}", e); "FAIL".to_string() } Ok(battery_info) => { @@ -391,8 +365,8 @@ impl<'a> MessageHandler<'a> { } }, Messages::PIRQuery => match self.camera.get_pirstate().await { - Err(_) => { - error!("Failed to get pir status"); + Err(e) => { + error!("Failed to get pir status: {:?}", e); "FAIL".to_string() } Ok(pir_info) => { @@ -417,18 +391,16 @@ impl<'a> MessageHandler<'a> { } }, Messages::PIROn => { - if self.camera.pir_set(true).await.is_err() { - error!("Failed to turn on the pir"); - self.abort(); + if let Err(e) = self.camera.pir_set(true).await { + error = Some(format!("Failed to turn on the pir: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() } } Messages::PIROff => { - if self.camera.pir_set(false).await.is_err() { - error!("Failed to turn on the pir"); - self.abort(); + if let Err(e) = self.camera.pir_set(false).await { + error = Some(format!("Failed to turn on the pir: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() @@ -443,9 +415,8 @@ impl<'a> MessageHandler<'a> { Direction::In(amount) => (BcDirection::In, amount), Direction::Out(amount) => (BcDirection::Out, amount), }; - if self.camera.send_ptz(bc_direction, amount).await.is_err() { - error!("Failed to turn on the status light"); - self.abort(); + if let Err(e) = self.camera.send_ptz(bc_direction, amount).await { + error = Some(format!("Failed to send PTZ: {:?}", e)); "FAIL".to_string() } else { "OK".to_string() @@ -456,18 +427,14 @@ impl<'a> MessageHandler<'a> { if let Some(replier) = replier { let _ = replier.send(reply).await; } + if let Some(error) = error { + return Err(anyhow!("{}", error)); + } } None => { - self.abort(); return Err(anyhow!("Message handller channel dropped")); } } } - - Ok(()) - } - - fn abort(&self) { - self.app.abort(&self.name); } } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 8d16086..554a234 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1,4 +1,3 @@ -use log::*; /// /// # Neolink MQTT /// @@ -54,27 +53,24 @@ use log::*; // `credentials` are the username and password required to identify with the mqtt server // use std::sync::Arc; +use tokio::time::{sleep, Duration}; -mod app; mod cmdline; mod event_cam; mod mqttc; use crate::config::{CameraConfig, Config, MqttConfig}; -use anyhow::{anyhow, Result}; -pub(crate) use app::App; +use anyhow::{anyhow, Context, Error, Result}; pub(crate) use cmdline::Opt; use event_cam::EventCam; pub(crate) use event_cam::{Direction, Messages}; +use log::*; use mqttc::{Mqtt, MqttReplyRef}; /// Entry point for the mqtt subcommand /// /// Opt is the command line options pub(crate) async fn main(_: Opt, config: Config) -> Result<()> { - let app = App::new(); - let arc_app = Arc::new(app); - if config.cameras.iter().all(|config| config.mqtt.is_none()) { return Err(anyhow!( "MQTT command run, but no cameras configured with MQTT settings. Exiting." @@ -89,262 +85,217 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> { .collect::>() { if let Some(mqtt_config) = camera_config.mqtt.as_ref().map(|a| Arc::new(a.clone())) { - let loop_arc_app = arc_app.clone(); info!("{}: Setting up mqtt", camera_config.name); set.spawn(async move { - while loop_arc_app.running("app") { + let mut wait_for = Duration::from_micros(125); + loop { tokio::task::yield_now().await; - let _ = - listen_on_camera(camera_config.clone(), &mqtt_config, loop_arc_app.clone()) - .await; + if let Err(e) = listen_on_camera(camera_config.clone(), &mqtt_config).await { + warn!("Error: {:?}. Retrying", e); + } + sleep(wait_for).await; + wait_for *= 2; } }); } } - if let Some(result) = set.join_next().await { + while let Some(result) = set.join_next().await { result?; } Ok(()) } -async fn listen_on_camera( - cam_config: Arc, - mqtt_config: &MqttConfig, - arc_app: Arc, -) -> Result<()> { +async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfig) -> Result<()> { // Camera thread - let mut event_cam = EventCam::new(cam_config.clone(), arc_app.clone()).await; - let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name, arc_app.clone()).await; - let mut set = tokio::task::JoinSet::new(); + let mut event_cam = EventCam::new(cam_config.clone()).await; + let mut mqtt = Mqtt::new(mqtt_config, &cam_config.name).await; + let mqtt_sender_cam = mqtt.get_sender(); + let mqtt_sender_mqtt = mqtt.get_sender(); + let event_cam_sender = event_cam.get_sender(); // Listen on mqtt messages and post on camera - let event_cam_sender = event_cam.get_sender(); - let app = arc_app.clone(); let camera_name = cam_config.name.clone(); - let mqtt_sender = mqtt.get_sender(); - set.spawn(async move { - while app.running(&format!("app: {}", camera_name)) { + + let mqtt_to_cam = async { + while let Ok(msg) = mqtt.poll().await { tokio::task::yield_now().await; - if let Ok(msg) = mqtt.poll().await { - match msg.as_ref() { - MqttReplyRef { - topic: "control/led", - message: "on", - } => { - if event_cam_sender - .send_message(Messages::StatusLedOn) - .await - .is_err() - { - error!("Failed to set camera status light on"); - } - } - MqttReplyRef { - topic: "control/led", - message: "off", - } => { - if event_cam_sender - .send_message(Messages::StatusLedOff) - .await - .is_err() - { - error!("Failed to set camera status light off"); - } - } - MqttReplyRef { - topic: "control/ir", - message: "on", - } => { - if event_cam_sender - .send_message(Messages::IRLedOn) - .await - .is_err() - { - error!("Failed to set camera status light on"); - } - } - MqttReplyRef { - topic: "control/ir", - message: "off", - } => { - if event_cam_sender - .send_message(Messages::IRLedOff) - .await - .is_err() - { - error!("Failed to set camera status light off"); - } - } - MqttReplyRef { - topic: "control/ir", - message: "auto", - } => { - if event_cam_sender - .send_message(Messages::IRLedAuto) - .await - .is_err() - { - error!("Failed to set camera status light off"); - } - } - MqttReplyRef { - topic: "control/reboot", - .. - } => { - if event_cam_sender - .send_message(Messages::Reboot) - .await - .is_err() - { - error!("Failed to set camera status light off"); - } - } - MqttReplyRef { - topic: "control/ptz", - message, - } => { - let lowercase_message = message.to_lowercase(); - let mut words = lowercase_message.split_whitespace(); - if let Some(direction_txt) = words.next() { - let amount = words.next().unwrap_or("32.0"); - if let Ok(amount) = amount.parse::() { - let direction = match direction_txt { - "up" => Direction::Up(amount), - "down" => Direction::Down(amount), - "left" => Direction::Left(amount), - "right" => Direction::Right(amount), - "in" => Direction::In(amount), - "out" => Direction::Out(amount), - _ => { - error!("Unrecongnized PTZ direction"); - continue; - } - }; - if event_cam_sender - .send_message(Messages::Ptz(direction)) - .await - .is_err() - { - error!("Failed to send PTZ"); + match msg.as_ref() { + MqttReplyRef { + topic: "control/led", + message: "on", + } => { + event_cam_sender + .send_message(Messages::StatusLedOn) + .await + .with_context(|| "Failed to set camera status light on")?; + } + MqttReplyRef { + topic: "control/led", + message: "off", + } => { + event_cam_sender + .send_message(Messages::StatusLedOff) + .await + .with_context(|| "Failed to set camera status light off")?; + } + MqttReplyRef { + topic: "control/ir", + message: "on", + } => { + event_cam_sender + .send_message(Messages::IRLedOn) + .await + .with_context(|| "Failed to set camera status light on")?; + } + MqttReplyRef { + topic: "control/ir", + message: "off", + } => { + event_cam_sender + .send_message(Messages::IRLedOff) + .await + .with_context(|| "Failed to set camera status light off")?; + } + MqttReplyRef { + topic: "control/ir", + message: "auto", + } => { + event_cam_sender + .send_message(Messages::IRLedAuto) + .await + .with_context(|| "Failed to set camera status light off")?; + } + MqttReplyRef { + topic: "control/reboot", + .. + } => { + event_cam_sender + .send_message(Messages::Reboot) + .await + .with_context(|| "Failed to set camera status light off")?; + } + MqttReplyRef { + topic: "control/ptz", + message, + } => { + let lowercase_message = message.to_lowercase(); + let mut words = lowercase_message.split_whitespace(); + if let Some(direction_txt) = words.next() { + let amount = words.next().unwrap_or("32.0"); + if let Ok(amount) = amount.parse::() { + let direction = match direction_txt { + "up" => Direction::Up(amount), + "down" => Direction::Down(amount), + "left" => Direction::Left(amount), + "right" => Direction::Right(amount), + "in" => Direction::In(amount), + "out" => Direction::Out(amount), + _ => { + error!("Unrecongnized PTZ direction"); + continue; } - } else { - error!("No PTZ direction speed was not a valid number"); - } + }; + event_cam_sender + .send_message(Messages::Ptz(direction)) + .await + .with_context(|| "Failed to send PTZ")?; } else { - error!("No PTZ Direction given. Please add up/down/left/right/in/out"); + error!("No PTZ direction speed was not a valid number"); } + } else { + error!("No PTZ Direction given. Please add up/down/left/right/in/out"); } - MqttReplyRef { - topic: "control/pir", - message: "on", - } => { - if event_cam_sender - .send_message(Messages::PIROn) - .await - .is_err() - { - error!("Failed to set pir on"); - } - } - MqttReplyRef { - topic: "control/pir", - message: "off", - } => { - if event_cam_sender - .send_message(Messages::PIROff) - .await - .is_err() - { - error!("Failed to set pir off"); - } - } - MqttReplyRef { - topic: "query/battery", - .. - } => match event_cam_sender + } + MqttReplyRef { + topic: "control/pir", + message: "on", + } => { + event_cam_sender + .send_message(Messages::PIROn) + .await + .with_context(|| "Failed to set pir on")?; + } + MqttReplyRef { + topic: "control/pir", + message: "off", + } => { + event_cam_sender + .send_message(Messages::PIROff) + .await + .with_context(|| "Failed to set pir off")?; + } + MqttReplyRef { + topic: "query/battery", + .. + } => { + let reply = event_cam_sender .send_message_with_reply(Messages::Battery) .await - { - Ok(reply) => { - if mqtt_sender - .send_message("status/battery", &reply, false) - .await - .is_err() - { - error!("Failed to send battery status reply"); - } - } - Err(_) => error!("Failed to get battery status"), - }, - MqttReplyRef { - topic: "query/pir", .. - } => match event_cam_sender + .with_context(|| "Failed to get battery status")?; + mqtt_sender_mqtt + .send_message("status/battery", &reply, false) + .await + .with_context(|| "Failed to send battery status reply")?; + } + MqttReplyRef { + topic: "query/pir", .. + } => { + let reply = event_cam_sender .send_message_with_reply(Messages::PIRQuery) .await - { - Ok(reply) => { - if mqtt_sender - .send_message("status/pir", &reply, false) - .await - .is_err() - { - error!("Failed to send pir status reply"); - } - } - Err(_) => error!("Failed to get pir status"), - }, - _ => {} + .with_context(|| "Failed to get pir status")?; + + mqtt_sender_mqtt + .send_message("status/pir", &reply, false) + .await + .with_context(|| "Failed to send pir status reply")?; } + _ => {} } } - }); + Result::<(), Error>::Ok(()) + }; - // Listen on camera messages and post on mqtt - let camera_name = cam_config.name.clone(); - let app = arc_app.clone(); - - set.spawn(async move { - while app.running(&format!("app: {}", camera_name)) { + let cam_to_mqtt = async { + loop { tokio::task::yield_now().await; - if let Ok(msg) = event_cam.poll().await { - match msg { - Messages::Login => { - if mqtt_sender_cam - .send_message("status", "connected", true) - .await - .is_err() - { - error!("Failed to post connect over MQTT for {}", camera_name); - } - } - Messages::MotionStop => { - if mqtt_sender_cam - .send_message("status/motion", "off", true) - .await - .is_err() - { - error!("Failed to publish motion stop for {}", camera_name); - } - } - Messages::MotionStart => { - if mqtt_sender_cam - .send_message("status/motion", "on", true) - .await - .is_err() - { - error!("Failed to publish motion start for {}", camera_name); - } - } - _ => {} + match event_cam.poll().await? { + Messages::Login => { + mqtt_sender_cam + .send_message("status", "connected", true) + .await + .with_context(|| { + format!("Failed to post connect over MQTT for {}", camera_name) + })?; } + Messages::MotionStop => { + mqtt_sender_cam + .send_message("status/motion", "off", true) + .await + .with_context(|| { + format!("Failed to publish motion stop for {}", camera_name) + })?; + } + Messages::MotionStart => { + mqtt_sender_cam + .send_message("status/motion", "on", true) + .await + .with_context(|| { + format!("Failed to publish motion start for {}", camera_name) + })?; + } + _ => {} } } - }); + }; - while set.join_next().await.is_some() {} + tokio::select! { + v = mqtt_to_cam => {v}, + v = cam_to_mqtt => {v}, + }?; Ok(()) } diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index b0188a5..87e85b1 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -1,4 +1,3 @@ -use super::App; use crate::config::MqttConfig; use anyhow::{anyhow, Context, Result}; use log::*; @@ -15,6 +14,7 @@ pub(crate) struct Mqtt { client: Arc, name: String, incoming: Receiver, + set: JoinSet>, drop_message: Option, } @@ -38,7 +38,6 @@ pub(crate) struct MqttReplyRef<'a> { } struct MqttReciever { - app: Arc, client: MqttSender, incoming_tx: Sender, name: String, @@ -96,37 +95,33 @@ impl MqttReciever { // This acts as an event loop let name = self.name.clone(); info!("Starting MQTT Client for {}", name); - while self.app.running(&format!("app:{}", name)) { - if let Ok(notification) = connection.poll().await { - // if let Ok(notification) = notification { - match notification { - Event::Incoming(Incoming::ConnAck(connected)) => { - if ConnectReturnCode::Success == connected.code { - self.client - .update_status() - .await - .context("Failed to update status")?; - // We succesfully logged in. Now ask for the cameras subscription. - self.client - .subscribe() - .await - .context("Failed to subscribe")?; - } + loop { + let notification = connection + .poll() + .await + .with_context(|| "MQTT connection dropped")?; + match notification { + Event::Incoming(Incoming::ConnAck(connected)) => { + if ConnectReturnCode::Success == connected.code { + self.client + .update_status() + .await + .context("Failed to update status")?; + // We succesfully logged in. Now ask for the cameras subscription. + self.client + .subscribe() + .await + .context("Failed to subscribe")?; } - Event::Incoming(Incoming::Publish(published_message)) => { - if self.handle_message(published_message).await.is_err() { - error!("Failed to forward messages in mqtt"); - } - } - _ => {} } - // } - } else { - error!("Mqtt connection failure"); - return Err(anyhow!("Mqtt connection failure")); + Event::Incoming(Incoming::Publish(published_message)) => { + if self.handle_message(published_message).await.is_err() { + error!("Failed to forward messages in mqtt"); + } + } + _ => {} } } - Ok(()) } async fn handle_message(&mut self, published_message: Publish) -> Result<()> { @@ -153,7 +148,7 @@ impl MqttReciever { } impl Mqtt { - pub(crate) async fn new(config: &MqttConfig, name: &str, app: Arc) -> Self { + pub(crate) async fn new(config: &MqttConfig, name: &str) -> Self { let (incoming_tx, incoming) = channel::(100); let mut mqttoptions = MqttOptions::new( format!("Neolink-{}", name), @@ -211,7 +206,6 @@ impl Mqtt { let mut set = JoinSet::>::new(); let mut reciever = MqttReciever { - app: app.clone(), incoming_tx, name: name.to_string(), client: MqttSender { @@ -221,32 +215,11 @@ impl Mqtt { }; set.spawn(async move { reciever.run(&mut connection).await }); - let join_app = app; - let join_name = name.to_string(); - tokio::task::spawn(async move { - while let Some(joined) = set.join_next().await { - tokio::task::yield_now().await; - match joined { - Err(e) => { - error!("MQTT reciever panicked: {:?}", e); - join_app.abort(&join_name); - } - Ok(Err(e)) => { - error!("MQTT stopped with error: {:?}", e); - join_app.abort(&join_name); - } - Ok(Ok(())) => { - info!("Normal shutdown of MQTT"); - } - } - } - info!("MQTT Loop End"); - }); - Self { client, name: name.to_string(), incoming, + set, // Send the drop message on clean disconnect drop_message: Some(MqttReply { topic: "status".to_string(), @@ -262,8 +235,28 @@ impl Mqtt { } } + /// This will also error is the join set errors pub(crate) async fn poll(&mut self) -> Result { - self.incoming.recv().await.context("Mqtt Polling error") + let (incoming, set) = (&mut self.incoming, &mut self.set); + tokio::select! { + v = incoming.recv() => v.with_context(|| "Mqtt Polling error"), + v = async { + while let Some(res) = set.join_next().await { + match res { + Err(e) => { + set.abort_all(); + return Err(e.into()); + } + Ok(Err(e)) => { + set.abort_all(); + return Err(e); + } + Ok(Ok(())) => {} + } + } + Err(anyhow!("MQTT background thread dropped without error")) + } => v.with_context(|| "MQTT Threads aborted"), + } } } @@ -278,5 +271,6 @@ impl Drop for Mqtt { error!("Failed to send offline message to mqtt"); } } + self.set.abort_all(); } } From 4f50a962e6c07aba9b66d126e6eb52e091b23e6c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 20 Apr 2023 07:32:30 +0700 Subject: [PATCH 5/6] Pass fail/OK messages back to mqtt --- src/mqtt/event_cam.rs | 1 + src/mqtt/mod.rs | 136 ++++++++++++++++++++++++++---------------- 2 files changed, 84 insertions(+), 53 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 8d0228d..fe0ae26 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -54,6 +54,7 @@ pub(crate) struct EventCamSender { } impl EventCamSender { + #[allow(dead_code)] pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> { self.channel_in .send(ToCamera::Send(msg)) diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 554a234..dcdb2ab 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -122,60 +122,74 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi let mqtt_to_cam = async { while let Ok(msg) = mqtt.poll().await { tokio::task::yield_now().await; + let mut reply = None; + let mut reply_topic = None; match msg.as_ref() { MqttReplyRef { topic: "control/led", message: "on", } => { - event_cam_sender - .send_message(Messages::StatusLedOn) - .await - .with_context(|| "Failed to set camera status light on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::StatusLedOn) + .await + .with_context(|| "Failed to set camera status light on")?, + ); } MqttReplyRef { topic: "control/led", message: "off", } => { - event_cam_sender - .send_message(Messages::StatusLedOff) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::StatusLedOff) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ir", message: "on", } => { - event_cam_sender - .send_message(Messages::IRLedOn) - .await - .with_context(|| "Failed to set camera status light on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedOn) + .await + .with_context(|| "Failed to set camera status light on")?, + ); } MqttReplyRef { topic: "control/ir", message: "off", } => { - event_cam_sender - .send_message(Messages::IRLedOff) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedOff) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ir", message: "auto", } => { - event_cam_sender - .send_message(Messages::IRLedAuto) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::IRLedAuto) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/reboot", .. } => { - event_cam_sender - .send_message(Messages::Reboot) - .await - .with_context(|| "Failed to set camera status light off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Reboot) + .await + .with_context(|| "Failed to set camera status light off")?, + ); } MqttReplyRef { topic: "control/ptz", @@ -198,10 +212,12 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi continue; } }; - event_cam_sender - .send_message(Messages::Ptz(direction)) - .await - .with_context(|| "Failed to send PTZ")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Ptz(direction)) + .await + .with_context(|| "Failed to send PTZ")?, + ); } else { error!("No PTZ direction speed was not a valid number"); } @@ -213,48 +229,62 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi topic: "control/pir", message: "on", } => { - event_cam_sender - .send_message(Messages::PIROn) - .await - .with_context(|| "Failed to set pir on")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIROn) + .await + .with_context(|| "Failed to set pir on")?, + ); } MqttReplyRef { topic: "control/pir", message: "off", } => { - event_cam_sender - .send_message(Messages::PIROff) - .await - .with_context(|| "Failed to set pir off")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIROff) + .await + .with_context(|| "Failed to set pir off")?, + ); } MqttReplyRef { topic: "query/battery", .. } => { - let reply = event_cam_sender - .send_message_with_reply(Messages::Battery) - .await - .with_context(|| "Failed to get battery status")?; - mqtt_sender_mqtt - .send_message("status/battery", &reply, false) - .await - .with_context(|| "Failed to send battery status reply")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Battery) + .await + .with_context(|| "Failed to get battery status")?, + ); + reply_topic = Some("status/battery"); } MqttReplyRef { topic: "query/pir", .. } => { - let reply = event_cam_sender - .send_message_with_reply(Messages::PIRQuery) - .await - .with_context(|| "Failed to get pir status")?; - - mqtt_sender_mqtt - .send_message("status/pir", &reply, false) - .await - .with_context(|| "Failed to send pir status reply")?; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::PIRQuery) + .await + .with_context(|| "Failed to get pir status")?, + ); + reply_topic = Some("status/pir"); } _ => {} } + if let Some(reply) = reply { + if let Some(topic) = reply_topic { + mqtt_sender_mqtt + .send_message(topic, &reply, false) + .await + .with_context(|| "Failed to send Camera reply to Mqtt")?; + } else { + mqtt_sender_mqtt + .send_message(&msg.topic, &reply, false) + .await + .with_context(|| "Failed to send Camera reply to Mqtt")?; + } + } } Result::<(), Error>::Ok(()) }; From 7d771fc94b726e21f22a77fc47e837bc6f3f3189 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 20 Apr 2023 15:24:31 +0700 Subject: [PATCH 6/6] Log current discovery method --- crates/core/src/bc_protocol.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 681a588..d76487f 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -139,11 +139,11 @@ impl BcCamera { } } } + info!("{}: Trying TCP discovery", options.name); for socket in sockets.drain(..) { let channel_id: u8 = options.channel_id; - let name: String = options.name.clone(); if let Ok(addr) = Discovery::check_tcp(socket, channel_id).await.map(|_| { - info!("{}: TCP Discovery success at {:?}", name, &socket); + info!("{}: TCP Discovery success at {:?}", options.name, &socket); socket }) { return Ok(CameraLocation::Tcp(addr)); @@ -180,14 +180,13 @@ impl BcCamera { }; if allow_local { - let name: String = options.name.clone(); let uid_local = uid.clone(); - trace!("{}: Starting Local discovery", name); + info!("{}: Trying local discovery", options.name); let result = Discovery::local(&uid_local, Some(sockets)).await; if let Ok(disc) = result { info!( "{}: Local discovery success {} at {}", - name, + options.name, uid_local, disc.get_addr() ); @@ -196,13 +195,12 @@ impl BcCamera { } if allow_remote { let uid_remote = uid.clone(); - let name: String = options.name.clone(); - trace!("Starting Remote discovery"); + info!("{}: Trying remote discovery", options.name); let result = Discovery::remote(&uid_remote).await; if let Ok(disc) = result { info!( "{}: Remote discovery success {} at {}", - name, + options.name, uid_remote, disc.get_addr() ); @@ -211,23 +209,26 @@ impl BcCamera { } if allow_map { let uid_map = uid.clone(); - let name: String = options.name.clone(); - trace!("Starting Map"); + info!("{}: Trying map discovery", options.name); let result = Discovery::map(&uid_map).await; if let Ok(disc) = result { - info!("{}: Map success {} at {}", name, uid_map, disc.get_addr()); + info!( + "{}: Map success {} at {}", + options.name, + uid_map, + disc.get_addr() + ); return Ok(CameraLocation::Udp(disc)); } } if allow_relay { let uid_relay = uid.clone(); - let name: String = options.name.clone(); - trace!("Starting Relay"); + info!("{}: Trying relay discovery", options.name); let result = Discovery::relay(&uid_relay).await; if let Ok(disc) = result { info!( "{}: Relay success {} at {}", - name, + options.name, uid_relay, disc.get_addr() ); @@ -236,7 +237,7 @@ impl BcCamera { } } - debug!("Relay (All) fail"); + info!("{}: Discovery failed", options.name); // Nothing works Err(Error::CannotInitCamera) }