From 101f2ffaa9abd20d892d5ddf6514aed1ac20547d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 12 Sep 2023 16:41:45 +0700 Subject: [PATCH] WIP Mqtt using reactor --- crates/core/src/bc_protocol.rs | 2 +- crates/core/src/bc_protocol/ptz.rs | 1 + crates/core/src/bc_protocol/resolution.rs | 2 +- src/common/instance.rs | 4 + src/config.rs | 46 +- src/main.rs | 2 +- src/mqtt/discovery.rs | 283 +++--- src/mqtt/event_cam.rs | 711 ------------- src/mqtt/mod.rs | 1117 ++++++++++++++------- src/mqtt/mqttc.rs | 612 ++++++----- src/rtsp/gst.rs | 1 - src/rtsp/gst/sender.rs | 1 - 12 files changed, 1284 insertions(+), 1498 deletions(-) delete mode 100644 src/mqtt/event_cam.rs delete mode 100644 src/rtsp/gst/sender.rs diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 56929ba..589ef89 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -102,7 +102,7 @@ pub struct BcCameraOpt { /// /// Currently this is just the format of battery levels but if we ever got more status /// messages then they will also use this information -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] pub enum PrintFormat { /// None, don't print None, diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs index ccbcf93..7e98e5c 100644 --- a/crates/core/src/bc_protocol/ptz.rs +++ b/crates/core/src/bc_protocol/ptz.rs @@ -2,6 +2,7 @@ use super::{BcCamera, Error, Result}; use crate::bc::{model::*, xml::*}; /// Directions used for Ptz +#[derive(Clone, Copy, Eq, PartialEq)] pub enum Direction { /// To move the camera Up Up, diff --git a/crates/core/src/bc_protocol/resolution.rs b/crates/core/src/bc_protocol/resolution.rs index 32643c1..e4e493b 100644 --- a/crates/core/src/bc_protocol/resolution.rs +++ b/crates/core/src/bc_protocol/resolution.rs @@ -11,7 +11,7 @@ use std::{ /// /// This is used for UID lookup, it is unused with /// TPC/known ip address cameras -#[derive(Debug, Copy, Clone, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)] pub enum DiscoveryMethods { /// Forbid all discovery methods. Only TCP connections with known addresses will work #[serde(alias = "none")] diff --git a/src/common/instance.rs b/src/common/instance.rs index 3beec25..a68957d 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -172,4 +172,8 @@ impl NeoInstance { .await?; Ok(instance_rx.await?) } + + pub(crate) fn camera(&self) -> WatchReceiver> { + self.camera_watch.clone() + } } diff --git a/src/config.rs b/src/config.rs index e41b437..411ad32 100644 --- a/src/config.rs +++ b/src/config.rs @@ -33,6 +33,9 @@ pub(crate) struct Config { #[serde(default = "default_certificate")] pub(crate) certificate: Option, + #[serde(default = "Default::default")] + pub(crate) mqtt: Option, + #[validate(regex( path = "RE_TLS_CLIENT_AUTH", message = "Incorrect tls auth", @@ -46,6 +49,24 @@ pub(crate) struct Config { pub(crate) users: Vec, } +#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)] +#[validate(schema(function = "validate_mqtt_server", skip_on_field_errors = true))] +pub(crate) struct MqttServerConfig { + #[serde(alias = "server")] + pub(crate) broker_addr: String, + + pub(crate) port: u16, + + #[serde(default)] + pub(crate) credentials: Option<(String, String)>, + + #[serde(default)] + pub(crate) ca: Option, + + #[serde(default)] + pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>, +} + #[derive(Debug, Deserialize, Clone, Copy, Eq, PartialEq)] pub(crate) enum StreamConfig { #[serde(alias = "none")] @@ -102,7 +123,7 @@ impl StreamConfig { } } -#[derive(Debug, Deserialize, Validate, Clone)] +#[derive(Debug, Deserialize, Validate, Clone, PartialEq)] #[validate(schema(function = "validate_camera_config"))] pub(crate) struct CameraConfig { pub(crate) name: String, @@ -177,7 +198,7 @@ pub(crate) struct CameraConfig { pub(crate) max_discovery_retries: usize, } -#[derive(Debug, Deserialize, Validate, Clone)] +#[derive(Debug, Deserialize, Validate, Clone, PartialEq, Eq)] pub(crate) struct UserConfig { #[validate(custom = "validate_username")] #[serde(alias = "username")] @@ -187,23 +208,8 @@ pub(crate) struct UserConfig { pub(crate) pass: String, } -#[derive(Debug, Deserialize, Clone, Validate)] -#[validate(schema(function = "validate_mqtt_config", skip_on_field_errors = true))] +#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)] pub(crate) struct MqttConfig { - #[serde(alias = "server")] - pub(crate) broker_addr: String, - - pub(crate) port: u16, - - #[serde(default)] - pub(crate) credentials: Option<(String, String)>, - - #[serde(default)] - pub(crate) ca: Option, - - #[serde(default)] - pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>, - #[serde(default = "default_true")] pub(crate) enable_motion: bool, #[serde(default = "default_true")] @@ -219,14 +225,14 @@ pub(crate) struct MqttConfig { pub(crate) discovery: Option, } -#[derive(Debug, Deserialize, Clone, Validate)] +#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)] pub(crate) struct MqttDiscoveryConfig { pub(crate) topic: String, pub(crate) features: HashSet, } -fn validate_mqtt_config(config: &MqttConfig) -> Result<(), ValidationError> { +fn validate_mqtt_server(config: &MqttServerConfig) -> Result<(), ValidationError> { if config.ca.is_some() && config.client_auth.is_some() { Err(ValidationError::new( "Cannot have both ca and client_auth set", diff --git a/src/main.rs b/src/main.rs index 0e877b4..e1aea34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,7 +116,7 @@ async fn main() -> Result<()> { talk::main(opts, config).await?; } Some(Command::Mqtt(opts)) => { - mqtt::main(opts, config).await?; + mqtt::main(opts, neo_reactor.clone()).await?; } Some(Command::Image(opts)) => { image::main(opts, neo_reactor.clone()).await?; diff --git a/src/mqtt/discovery.rs b/src/mqtt/discovery.rs index 171b0ea..25615e8 100644 --- a/src/mqtt/discovery.rs +++ b/src/mqtt/discovery.rs @@ -5,10 +5,9 @@ use anyhow::{Context, Result}; use heck::ToTitleCase; use log::*; -use std::sync::Arc; -use super::mqttc::MqttSender; -use crate::config::{CameraConfig, MqttDiscoveryConfig}; +use super::mqttc::MqttInstance; +use crate::{common::NeoInstance, config::MqttDiscoveryConfig}; use serde::{Deserialize, Serialize, Serializer}; #[derive(Debug, Deserialize, Clone, PartialEq, Eq, Copy, Hash)] @@ -200,9 +199,10 @@ struct DiscoverySensor { /// Enables MQTT discovery for a camera. See docs at https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery pub(crate) async fn enable_discovery( discovery_config: &MqttDiscoveryConfig, - mqtt_sender: &MqttSender, - cam_config: &Arc, + mqtt: &MqttInstance, + camera: &NeoInstance, ) -> Result<()> { + let cam_config = camera.config().await?.borrow().clone(); debug!("Enabling MQTT discovery for {}", cam_config.name); let mut connections = vec![]; @@ -269,25 +269,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/light/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery light config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/light/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery light config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish floodlight auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish floodlight auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Camera => { let config_data = DiscoveryCamera { @@ -306,25 +304,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/camera/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery camera config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/camera/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery camera config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish camera auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish camera auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Led => { let config_data = DiscoverySwitch { @@ -347,25 +343,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/switch/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery led config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/switch/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery led config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish led auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish led auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Ir => { let config_data = DiscoverySelect { @@ -385,25 +379,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/select/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery led config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/select/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery led config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish led auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish led auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Motion => { let config_data = DiscoveryBinarySensor { @@ -423,25 +415,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/binary_sensor/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery motion config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/binary_sensor/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery motion config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish motion auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish motion auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Reboot => { let config_data = DiscoveryButton { @@ -460,25 +450,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/button/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery reboot config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/button/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery reboot config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish reboot auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish reboot auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } Discoveries::Pt => { for dir in ["left", "right", "up", "down"] { @@ -498,25 +486,23 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/button/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery pt config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/button/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data) + .with_context(|| "Cound not serialise discovery pt config into json")?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish pt auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish pt auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } } Discoveries::Battery => { @@ -537,25 +523,24 @@ pub(crate) async fn enable_discovery( }; // Each feature needs to be individually registered - mqtt_sender - .send_message_with_root_topic( - &format!( - "{}/sensor/{}", - discovery_config.topic, &config_data.unique_id - ), - "config", - &serde_json::to_string(&config_data).with_context(|| { - "Cound not serialise discovery battery config into json" - })?, - true, + mqtt.send_message_with_root_topic( + &format!( + "{}/sensor/{}", + discovery_config.topic, &config_data.unique_id + ), + "config", + &serde_json::to_string(&config_data).with_context(|| { + "Cound not serialise discovery battery config into json" + })?, + true, + ) + .await + .with_context(|| { + format!( + "Failed to publish battery auto-discover data on over MQTT for {}", + cam_config.name ) - .await - .with_context(|| { - format!( - "Failed to publish battery auto-discover data on over MQTT for {}", - cam_config.name - ) - })?; + })?; } } } diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs deleted file mode 100644 index c3095df..0000000 --- a/src/mqtt/event_cam.rs +++ /dev/null @@ -1,711 +0,0 @@ -use crate::config::CameraConfig; -use crate::utils::{timeout, 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, -}; -use std::sync::Arc; -use tokio::{ - sync::mpsc::{channel, Receiver, Sender}, - task::JoinSet, - time::{interval, sleep, Duration}, -}; - -#[derive(Debug, Clone)] -pub(crate) enum Messages { - Login, - MotionStop, - MotionStart, - Reboot, - FloodlightOn, - FloodlightOff, - StatusLedOn, - StatusLedOff, - IRLedOn, - IRLedOff, - IRLedAuto, - Battery, - PIROn, - PIROff, - PIRQuery, - Ptz(Direction), - Snap(Vec), - BatteryLevel(u32), - Preset(u8), - PresetAssign(u8, String), - PresetQuery, -} - -#[derive(Debug, Copy, Clone)] -pub(crate) enum Direction { - Up(f32, f32), - Down(f32, f32), - Left(f32, f32), - Right(f32, f32), - In(f32, f32), - Out(f32, f32), -} - -#[derive(Debug)] -enum ToCamera { - Send(Messages), - SendAndReply { - message: Messages, - reply: Sender, - }, -} - -#[derive(Clone)] -pub(crate) struct EventCamSender { - channel_in: Sender, -} - -impl EventCamSender { - #[allow(dead_code)] - pub(crate) async fn send_message(&self, msg: Messages) -> Result<()> { - 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 (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, - set: JoinSet>, -} - -impl EventCam { - 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 mut set = JoinSet::>::new(); - 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); - loop { - // 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; - } - } - }); - - 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 { - 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 { - self.sender.clone() - } -} - -impl Drop for EventCam { - fn drop(&mut self) { - self.set.abort_all(); - } -} - -struct EventCamThread { - rx: Receiver, - tx: Sender, -} - -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, - &camera_config.discovery, - ) - .unwrap(); - - info!( - "{}: Connecting to camera at {}", - camera_config.name, camera_addr - ); - let camera = camera_addr - .connect_camera(camera_config) - .await - .with_context(|| { - format!( - "Failed to connect to camera {} at {} on channel {}", - camera_config.name, camera_addr, camera_config.channel_id - ) - })?; - - 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, - }; - timeout(camera.login_with_maxenc(max_encryption)) - .await - .context("Failed to login to the camera")??; - info!("{}: Connected and logged in", camera_config.name); - - if let Err(e) = camera.monitor_battery(camera_config.print_format).await { - warn!("Could not monitor battery: {:?}", e); - } - - self.tx.send(Messages::Login).await?; - - // Shararble cameras - let arc_cam = Arc::new(camera); - - let mut motion_thread = MotionThread { - tx: self.tx.clone(), - camera: arc_cam.clone(), - }; - - let mut flight_thread = FloodlightThread { - tx: self.tx.clone(), - camera: arc_cam.clone(), - }; - - let mut snap_thread = SnapThread { - tx: self.tx.clone(), - camera: arc_cam.clone(), - }; - - let mut battery_thread = BatteryLevelThread { - 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, - }; - - tokio::select! { - val = async { - info!("{}: Listening to Camera Motion", camera_config.name); - motion_thread.run().await - }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_motion => { - if let Err(e) = val { - error!("Motion thread aborted: {:?}", e); - Err(e) - } else { - debug!("Normal finish on motion thread"); - Ok(()) - } - }, - val = async { - debug!("{}: Starting Pings", camera_config.name); - keepalive_thread.run().await - }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_pings => { - 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 camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_light => { - if let Err(e) = val { - error!("FloodLight thread aborted: {:?}", e); - Err(e) - } else { - debug!("Normal finish on FloodLight thread"); - Ok(()) - } - }, - val = async { - info!("{}: Updating Preview", camera_config.name); - snap_thread.run().await - }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_preview => { - if let Err(e) = val { - error!("Snap thread aborted: {:?}", e); - Err(e) - } else { - debug!("Normal finish on Snap thread"); - Ok(()) - } - }, - val = async { - info!("{}: Updating Battery Level", camera_config.name); - battery_thread.run().await - }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_battery => { - if let Err(e) = val { - error!("Battery thread aborted: {:?}", e); - Err(e) - } else { - debug!("Normal finish on Battery 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(()) - } - }, - }?; - - Ok(()) - } -} - -struct MotionThread { - tx: Sender, - camera: Arc, -} - -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()?; - loop { - tokio::task::yield_now().await; - for motion_status in queued_motion.drain(..) { - match motion_status { - MotionStatus::Start(_) => { - self.tx - .send(Messages::MotionStart) - .await - .map_err(|_| BcError::Other("Failed to send Message"))?; - } - MotionStatus::Stop(_) => { - self.tx - .send(Messages::MotionStop) - .await - .map_err(|_| BcError::Other("Failed to send Message"))?; - } - _ => {} - } - } - queued_motion.push(motion_data.next_motion().await?); - } - } -} - -struct FloodlightThread { - tx: Sender, - camera: Arc, -} - -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 SnapThread { - tx: Sender, - camera: Arc, -} - -impl SnapThread { - async fn run(&mut self) -> Result<()> { - let mut tries = 0; - let base_duration = Duration::from_millis(500); - loop { - tokio::time::sleep(base_duration.saturating_mul(tries)).await; - let snapshot = match self.camera.get_snapshot().await { - Ok(info) => { - tries = 1; - info - } - Err(neolink_core::Error::UnintelligibleReply { reply, why }) => { - log::debug!("Reply: {:?}, why: {:?}", reply, why); - // Try again later - tries += 1; - continue; - } - Err(neolink_core::Error::CameraServiceUnavaliable) => { - log::debug!("Snap not supported"); - futures::future::pending().await - } - Err(e) => return Err(e.into()), - }; - self.tx.send(Messages::Snap(snapshot)).await?; - } - } -} - -struct BatteryLevelThread { - tx: Sender, - camera: Arc, -} - -impl BatteryLevelThread { - async fn run(&mut self) -> Result<()> { - let mut tries = 0; - let base_duration = Duration::from_secs(15); - loop { - tokio::time::sleep(base_duration.saturating_mul(tries)).await; - let battery = match self.camera.battery_info().await { - Ok(info) => { - tries = 1; - info - } - Err(neolink_core::Error::UnintelligibleReply { .. }) => { - // Try again later - tries += 1; - continue; - } - Err(neolink_core::Error::CameraServiceUnavaliable) => { - log::debug!("Battery not supported"); - futures::future::pending().await - } - Err(e) => return Err(e.into()), - }; - self.tx - .send(Messages::BatteryLevel(battery.battery_percent)) - .await?; - } - } -} - -struct KeepaliveThread { - camera: Arc, -} - -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, - camera: Arc, -} - -impl<'a> MessageHandler<'a> { - async fn listen(&mut self) -> Result<()> { - loop { - 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 mut error = None; - let reply = match message { - Messages::Reboot => { - 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::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 = - Some(format!("Failed to turn on the status light: {:?}", e)); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::StatusLedOff => { - 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 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 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 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(e) => { - error!("Failed to get battery status: {:?}", e); - "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(e) => { - error!("Failed to get pir status: {:?}", e); - "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 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 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() - } - } - Messages::Ptz(direction) => { - let (bc_direction, speed, seconds) = match direction { - Direction::Up(speed, seconds) => (BcDirection::Up, speed, seconds), - Direction::Down(speed, seconds) => { - (BcDirection::Down, speed, seconds) - } - Direction::Left(speed, seconds) => { - (BcDirection::Left, speed, seconds) - } - Direction::Right(speed, seconds) => { - (BcDirection::Right, speed, seconds) - } - Direction::In(speed, seconds) => (BcDirection::In, speed, seconds), - Direction::Out(speed, seconds) => { - (BcDirection::Out, speed, seconds) - } - }; - if let Err(e) = self.camera.send_ptz(bc_direction, speed).await { - error = Some(format!("Failed to send PTZ: {:?}", e)); - "FAIL".to_string() - } else { - // sleep for the designated seconds - sleep(Duration::from_secs_f32(seconds)).await; - - // note that amount is not used in the stop command - if let Err(e) = self.camera.send_ptz(BcDirection::Stop, 0.0).await { - error = Some(format!("Failed to send PTZ: {:?}", e)); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - } - Messages::Preset(id) => { - if let Err(e) = self.camera.moveto_ptz_preset(id).await { - error = Some(format!("Failed to send PTZ preset: {:?}", e)); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::PresetAssign(id, name) => { - if let Err(e) = self.camera.set_ptz_preset(id, name).await { - error = Some(format!("Failed to send PTZ preset: {:?}", e)); - "FAIL".to_string() - } else { - "OK".to_string() - } - } - Messages::PresetQuery => match self.camera.get_ptz_preset().await { - Err(e) => { - error!("Failed to get PTZ preset status: {:?}", e); - "FAIL".to_string() - } - Ok(ptz_info) => { - let bytes_res = yaserde::ser::serialize_with_writer( - &ptz_info, - vec![], - &Default::default(), - ); - match bytes_res { - Ok(bytes) => match String::from_utf8(bytes) { - Ok(str) => str, - Err(_) => { - error!("Failed to encode PTZ preset status"); - "FAIL".to_string() - } - }, - Err(_) => { - error!("Failed to serialise PTZ preset status"); - "FAIL".to_string() - } - } - } - }, - _ => "UNKNOWN COMMAND".to_string(), - }; - if let Some(replier) = replier { - let _ = replier.send(reply).await; - } - if let Some(error) = error { - return Err(anyhow!("{}", error)); - } - } - None => { - return Err(anyhow!("Message handller channel dropped")); - } - } - } - } -} diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 681e1c7..25a50b5 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1,263 +1,407 @@ +//! +//! # Neolink MQTT +//! +//! Handles incoming and outgoing MQTT messages +//! +//! This acts as a bridge between cameras and MQTT servers +//! +//! Messages are prefixed with `neolink/{CAMERANAME}` +//! +//! Control messages: +//! +//! - `/control/floodlight [on|off]` Turns floodlight (if equipped) on/off +//! - `/control/led [on|off]` Turns status LED on/off +//! - `/control/pir [on|off]` Turns PIR on/off +//! - `/control/ir [on|off|auto]` Turn IR lights on/off or automatically via light detection +//! - `/control/reboot` Reboot the camera +//! - `/control/ptz` [up|down|left|right|in|out] (amount) Control the PTZ movements, amount defaults to 32.0 +//! - `/control/ptz/preset` [id] Move the camera to a known preset +//! - `/control/ptz/assign` [id] [name] Assign the current ptz position to an ID and name +//! +//! Status Messages: +//! +//! `/status offline` Sent when the neolink goes offline this is a LastWill message +//! `/status disconnected` Sent when the camera goes offline +//! `/status/battery` Sent in reply to a `/query/battery` +//! `/status/pir` Sent in reply to a `/query/pir` +//! `/status/ptz/preset` Sent in reply to a `/query/ptz/preset` +//! +//! Query Messages: +//! +//! `/query/battery` Request that the camera reports its battery level +//! `/query/pir` Request that the camera reports its pir status +//! `/query/ptz/preset` Request that the camera reports the PTZ presets +//! +//! +//! # Usage +//! +//! ```bash +//! neolink mqtt --config=config.toml +//! ``` +//! +//! # Example Config +//! +//! ```toml +//! [[cameras]] +//! name = "Cammy" +//! username = "****" +//! password = "****" +//! address = "****:9000" +//! [cameras.mqtt] +//! server = "127.0.0.1" +//! port = 1883 +//! credentials = ["username", "password"] +//! ``` +//! +//! `server` is the mqtt server +//! `port` is the mqtt server's port +//! `credentials` are the username and password required to identify with the mqtt server +//! use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; -/// -/// # Neolink MQTT -/// -/// Handles incoming and outgoing MQTT messages -/// -/// This acts as a bridge between cameras and MQTT servers -/// -/// Messages are prefixed with `neolink/{CAMERANAME}` -/// -/// Control messages: -/// -/// - `/control/floodlight [on|off]` Turns floodlight (if equipped) on/off -/// - `/control/led [on|off]` Turns status LED on/off -/// - `/control/pir [on|off]` Turns PIR on/off -/// - `/control/ir [on|off|auto]` Turn IR lights on/off or automatically via light detection -/// - `/control/reboot` Reboot the camera -/// - `/control/ptz` [up|down|left|right|in|out] (amount) Control the PTZ movements, amount defaults to 32.0 -/// - `/control/ptz/preset` [id] Move the camera to a known preset -/// - `/control/ptz/assign` [id] [name] Assign the current ptz position to an ID and name -/// -/// Status Messages: -/// -/// `/status offline` Sent when the neolink goes offline this is a LastWill message -/// `/status disconnected` Sent when the camera goes offline -/// `/status/battery` Sent in reply to a `/query/battery` -/// `/status/pir` Sent in reply to a `/query/pir` -/// `/status/ptz/preset` Sent in reply to a `/query/ptz/preset` -/// -/// Query Messages: -/// -/// `/query/battery` Request that the camera reports its battery level -/// `/query/pir` Request that the camera reports its pir status -/// `/query/ptz/preset` Request that the camera reports the PTZ presets -/// -/// -/// # Usage -/// -/// ```bash -/// neolink mqtt --config=config.toml -/// ``` -/// -/// # Example Config -/// -/// ```toml -// [[cameras]] -// name = "Cammy" -// username = "****" -// password = "****" -// address = "****:9000" -// [cameras.mqtt] -// server = "127.0.0.1" -// port = 1883 -// credentials = ["username", "password"] -// ``` -// -// `server` is the mqtt server -// `port` is the mqtt server's port -// `credentials` are the username and password required to identify with the mqtt server -// -use std::sync::Arc; -use tokio::time::{sleep, Duration}; +use std::collections::{HashMap, HashSet}; +use tokio::{ + sync::mpsc::channel as mpsc, + task::JoinSet, + time::{interval, sleep, Duration, MissedTickBehavior}, +}; +use tokio_stream::{wrappers::IntervalStream, StreamExt}; +use tokio_util::sync::CancellationToken; + +use neolink_core::bc_protocol::{Direction as BcDirection, LightState}; mod cmdline; mod discovery; -mod event_cam; mod mqttc; -use crate::config::{CameraConfig, Config, MqttConfig}; -use anyhow::{anyhow, Context, Error, Result}; +use crate::{ + common::{MdState, NeoInstance, NeoReactor}, + AnyResult, +}; +use anyhow::{anyhow, Context, Result}; pub(crate) use cmdline::Opt; pub(crate) use discovery::Discoveries; -use event_cam::EventCam; -pub(crate) use event_cam::{Direction, Messages}; use log::*; use mqttc::{Mqtt, MqttReplyRef}; use self::{ discovery::enable_discovery, - event_cam::EventCamSender, - mqttc::{MqttReply, MqttSender}, + mqttc::{MqttInstance, MqttReply}, }; /// Entry point for the mqtt subcommand /// /// Opt is the command line options -pub(crate) async fn main(_: Opt, config: Config) -> Result<()> { - if config.cameras.iter().all(|config| config.mqtt.is_none()) { - return Err(anyhow!( - "MQTT command run, but no cameras configured with MQTT settings. Exiting." - )); - } - +pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> { let mut set = tokio::task::JoinSet::new(); - for camera_config in config - .cameras - .iter() - .filter(|c| c.enabled) - .map(|a| Arc::new(a.clone())) - .collect::>() - { - if let Some(mqtt_config) = camera_config.mqtt.as_ref().map(|a| Arc::new(a.clone())) { - info!("{}: Setting up mqtt", camera_config.name); - set.spawn(async move { - let mut wait_for = Duration::from_micros(125); + let global_cancel = CancellationToken::new(); + let config = reactor.config().await?; + let mqtt = Mqtt::new(config.clone()).await; + + // Startup and stop cameras as they are added/removed to the config + let thread_cancel = global_cancel.clone(); + let mut thread_config = config.clone(); + let thread_reactor = reactor.clone(); + let thread_instance = mqtt.subscribe("").await?; + set.spawn(async move { + let mut set = JoinSet::>::new(); + let thread_cancel2 = thread_cancel.clone(); + tokio::select!{ + _ = thread_cancel.cancelled() => AnyResult::Ok(()), + v = async { + let mut cameras: HashMap = Default::default(); + let mut config_names = HashSet::new(); loop { - tokio::task::yield_now().await; - if let Err(e) = listen_on_camera(camera_config.clone(), &mqtt_config).await { - warn!("Error: {:?}. Retrying", e); + thread_config.wait_for(|config| { + let current_names = config.cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::>(); + current_names != config_names + }).await.with_context(|| "Camera Config Watcher")?; + config_names = thread_config.borrow().clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::>(); + + for name in config_names.iter() { + log::info!("{name}: MQTT Staring"); + if ! cameras.contains_key(name) { + let local_cancel = CancellationToken::new(); + cameras.insert(name.clone(),local_cancel.clone()); + + let thread_global_cancel = thread_cancel2.clone(); + let thread_reactor2 = thread_reactor.clone(); + let mqtt_instance = thread_instance.subscribe(name).await?; + let name = name.clone(); + set.spawn_blocking(|| { + let runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + runtime.block_on(async move { + let camera = thread_reactor2.get(&name).await?; + tokio::select!( + _ = thread_global_cancel.cancelled() => { + AnyResult::Ok(()) + }, + _ = local_cancel.cancelled() => { + AnyResult::Ok(()) + }, + v = listen_on_camera(camera, mqtt_instance) => v, + ) + }) + }) ; + } + } + + for (running_name, token) in cameras.iter() { + if ! config_names.contains(running_name) { + token.cancel(); + } } - sleep(wait_for).await; - wait_for *= 2; } - }); + } => v, } - } + }); while let Some(result) = set.join_next().await { - result?; + result??; } Ok(()) } -async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfig) -> Result<()> { - // Camera thread - 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 camera_name = cam_config.name.clone(); - // When one error is recieved we pass it up async - let (error_send, mut error_recv) = tokio::sync::mpsc::channel::(1); - - let mqtt_to_cam = async { - // Select to wait on an error (via the channel) or normal poll ops - tokio::select! { - v = async { - // Normal poll operations - 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 - let event_cam_sender = event_cam_sender.clone(); - let mqtt_sender_mqtt = mqtt_sender_mqtt.clone(); - let error_send = error_send.clone(); - tokio::task::spawn(async move { - // Handle the message and wait for ok/error on this thread - let result: Result<()> = handle_mqtt_message(&msg, event_cam_sender, mqtt_sender_mqtt).await; - // If there is an error we pass it to the channel - // this allows for async error handelling - if let Err(e) = result { - let _ = error_send.try_send(e); - } - }); - } - } => 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"))), - } - }; - - let cam_to_mqtt = async { - loop { - tokio::task::yield_now().await; - 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) - })?; - - if let Some(discovery_config) = &mqtt_config.discovery { - enable_discovery(discovery_config, &mqtt_sender_cam, &cam_config).await?; - } - } - Messages::FloodlightOn => { - mqtt_sender_cam - .send_message("status/floodlight", "on", true) - .await - .with_context(|| { - format!( - "Failed to publish floodlight on over MQTT for {}", - camera_name - ) - })?; - } - Messages::FloodlightOff => { - mqtt_sender_cam - .send_message("status/floodlight", "off", true) - .await - .with_context(|| { - format!( - "Failed to publish floodlight off 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) - })?; - } - 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) - })?; - } - Messages::BatteryLevel(data) => { - mqtt_sender_cam - .send_message("status/battery_level", format!("{}", data).as_str(), true) - .await - .with_context(|| { - format!( - "Failed to publish battery level over MQTT for {}", - camera_name - ) - })?; - } - _ => {} +async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> Result<()> { + let mut watch_config = camera.config().await?; + let camera_name = watch_config.borrow().name.clone(); + let mut config; + let cancel = CancellationToken::new(); + loop { + config = watch_config.borrow().clone().mqtt; + break tokio::select! { + v = watch_config.wait_for(|new_config| config != new_config.mqtt) => { + v?; + continue; } - } - }; + v = async { + mqtt_instance + .send_message("status", "disconnected", true) + .await + .with_context(|| format!("Failed to publish status for {}", camera_name))?; + mqtt_instance + .send_message("status/motion", "unknown", true) + .await + .with_context(|| format!("Failed to publish motion unknown for {}", camera_name))?; - tokio::select! { - v = mqtt_to_cam => {v}, - v = cam_to_mqtt => {v}, + if let Some(config) = config.as_ref() { + //Publish initial states + if let Some(discovery_config) = config.discovery.as_ref() { + enable_discovery(discovery_config, &mqtt_instance, &camera).await?; + } + + let camera_msg = camera.clone(); + let mut mqtt_msg = mqtt_instance.resubscribe().await?; + let cancel_msg = cancel.clone(); + let mut set_msg = JoinSet::new(); + + let mut camera_watch = camera.camera(); + let mqtt_watch = mqtt_instance.resubscribe().await?; + + let camera_floodlight = camera.clone(); + let mqtt_floodlight = mqtt_instance.resubscribe().await?; + + let camera_motion = camera.clone(); + let mqtt_motion = mqtt_instance.resubscribe().await?; + + let camera_snap = camera.clone(); + let mqtt_snap = mqtt_instance.resubscribe().await?; + + let camera_battery = camera.clone(); + let mqtt_battery = mqtt_instance.resubscribe().await?; + + tokio::select! { + _ = cancel.cancelled() => AnyResult::Ok(()), + // Handles incomming requests + v = async { + let (tx, mut rx) = mpsc(1); + tokio::select! { + v = async { + while let Ok(msg) = mqtt_msg.recv().await { + let mqtt_msg = mqtt_msg.resubscribe().await?; + let camera_msg = camera_msg.clone(); + let tx = tx.clone(); + let cancel_msg = cancel_msg.clone(); + set_msg.spawn(async move { + tokio::select!{ + _ = cancel_msg.cancelled() => AnyResult::Ok(()), + v = async { + let res = handle_mqtt_message(msg, &mqtt_msg, &camera_msg).await; + if res.is_err() { + tx.send(res).await?; + } + AnyResult::Ok(()) + } => v, + } + }); + } + AnyResult::Ok(()) + } => v, + v = rx.recv() => { + v.ok_or(anyhow!("All error senders were dropped"))? + }, + }?; + AnyResult::Ok(()) + } => v, + // Handle camera disconnect/connect + v = async { + loop { + camera_watch.wait_for(|cam| cam.upgrade().is_some()).await.with_context(|| { + format!("{}: Online Watch Dropped", camera_name) + })?; + mqtt_watch.send_message("status", "online", true).await.with_context(|| { + format!("{}: Failed to publish Online", camera_name) + })?; + camera_watch.wait_for(|cam| cam.upgrade().is_none()).await.with_context(|| { + format!("{}: Disconnect Watch Dropped", camera_name) + })?; + mqtt_watch.send_message("status", "disconnected", true).await.with_context(|| { + format!("{}: Failed to publish disconnected", camera_name) + })?; + } + } => v, + // Handle the floodlight + v = async { + let (tx, mut rx) = mpsc(100); + tokio::select! { + v = camera_floodlight.run_task(|cam| { + let tx = tx.clone(); + Box::pin( + async move { + let mut reciever = tokio_stream::wrappers::ReceiverStream::new(cam.listen_on_flightlight().await?); + while let Some(flights) = reciever.next().await { + for flight in flights.floodlight_status_list.iter() { + if flight.status == 0 { + tx.send(false).await?; + } else { + tx.send(true).await?; + } + } + } + AnyResult::Ok(()) + } + ) + }) => v, + v = async { + while let Some(on) = rx.recv().await { + if on { + mqtt_floodlight.send_message("status/floodlight", "on", true).await?; + } else { + mqtt_floodlight.send_message("status/floodlight", "off", true).await?; + } + } + AnyResult::Ok(()) + } => v, + }?; + AnyResult::Ok(()) + } => v, + // Handle the motion messages + v = async { + let mut md = camera_motion.motion().await?; + loop { + md.wait_for(|state| matches!(state, MdState::Start(_))).await.with_context(|| { + format!("{}: MdStart Watch Dropped", camera_name) + })?; + mqtt_motion.send_message("status/motion", "on", true).await.with_context(|| { + format!("{}: Failed to publish motion start", camera_name) + })?; + md.wait_for(|state| matches!(state, MdState::Stop(_))).await.with_context(|| { + format!("{}: MdStop Watch Dropped", camera_name) + })?; + mqtt_motion.send_message("status/motion", "off", true).await.with_context(|| { + format!("{}: Failed to publish motion stop", camera_name) + })?; + } + } => v, + // Handle the SNAP (image preview) + v = async { + let mut wait = IntervalStream::new({ + let mut i = interval(Duration::from_millis(500)); + i.set_missed_tick_behavior(MissedTickBehavior::Skip); + i + }); + while wait.next().await.is_some() { + let image = camera_snap.run_task(|cam| { + Box::pin(async move { + let image = cam.get_snapshot().await?; + AnyResult::Ok(image) + }) + }).await; + let image = match image { + Err(e) => match e.downcast::() { + Ok(neolink_core::Error::CameraServiceUnavaliable) => { + log::debug!("Image not supported"); + futures::future::pending().await + }, + Ok(e) => Err(e.into()), + Err(e) => Err(e), + } + n => n, + }?; + mqtt_snap + .send_message("status/preview", BASE64.encode(image).as_str(), true) + .await + .with_context(|| { + format!("{}: Failed to publish preview", camera_name) + })?; + } + AnyResult::Ok(()) + } => v, + // Handle the battery publish + v = async { + let mut wait = IntervalStream::new({ + let mut i = interval(Duration::from_millis(5000)); + i.set_missed_tick_behavior(MissedTickBehavior::Skip); + i + }); + while wait.next().await.is_some() { + let xml = camera_battery.run_task(|cam| { + Box::pin(async move { + let xml = cam.battery_info().await?; + AnyResult::Ok(xml) + }) + }).await; + let xml = match xml { + Err(e) => match e.downcast::() { + Ok(neolink_core::Error::CameraServiceUnavaliable) => { + log::debug!("Battery not supported"); + futures::future::pending().await + }, + Ok(e) => Err(e.into()), + Err(e) => Err(e), + } + n => n, + }?; + mqtt_battery + .send_message("status/battery_level", format!("{}", xml.battery_percent).as_str(), true) + .await + .with_context(|| { + format!("{}: Failed to publish battery", camera_name) + })?; + } + AnyResult::Ok(()) + } => v + }?; + } else { + futures::future::pending().await + } + AnyResult::Ok(()) + } => v, + }; }?; + cancel.cancel(); Ok(()) } async fn handle_mqtt_message( - msg: &MqttReply, - event_cam_sender: EventCamSender, - mqtt_sender_mqtt: MqttSender, + msg: MqttReply, + mqtt: &MqttInstance, + camera: &NeoInstance, ) -> Result<()> { - let mut reply = None; - let mut reply_topic = None; match msg.as_ref() { MqttReplyRef { topic: _, @@ -273,89 +417,185 @@ async fn handle_mqtt_message( 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")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.set_floodlight_manual(true, 180).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn on the floodlight light: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/floodlight", &reply, false) + .await + .with_context(|| "Failed to publish 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")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.set_floodlight_manual(false, 180).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn off the floodlight light: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/floodlight", &reply, false) + .await + .with_context(|| "Failed to publish camera status light off")?; } MqttReplyRef { topic: "control/led", message: "on", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::StatusLedOn) - .await - .with_context(|| "Failed to set camera status light on")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.led_light_set(true).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn on the led: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/led", &reply, false) + .await + .with_context(|| "Failed to publish led on")?; } MqttReplyRef { topic: "control/led", message: "off", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::StatusLedOff) - .await - .with_context(|| "Failed to set camera status light off")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.led_light_set(false).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn off the led: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/led", &reply, false) + .await + .with_context(|| "Failed to publish led off")?; } MqttReplyRef { topic: "control/ir", message: "on", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::IRLedOn) - .await - .with_context(|| "Failed to set camera status light on")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.irled_light_set(LightState::On).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn on the ir: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish ir on")?; } MqttReplyRef { topic: "control/ir", message: "off", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::IRLedOff) - .await - .with_context(|| "Failed to set camera status light off")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.irled_light_set(LightState::Off).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn off the ir: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish ir off")?; } MqttReplyRef { topic: "control/ir", message: "auto", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::IRLedAuto) - .await - .with_context(|| "Failed to set camera status light off")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.irled_light_set(LightState::Auto).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn set to auto on the led: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish ir auto")?; } MqttReplyRef { topic: "control/reboot", .. } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::Reboot) - .await - .with_context(|| "Failed to set camera status light off")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.reboot().await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to reboot the camera: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish reboot on the camera")?; } MqttReplyRef { topic: "control/ptz", @@ -363,60 +603,111 @@ async fn handle_mqtt_message( } => { let lowercase_message = message.to_lowercase(); let mut words = lowercase_message.split_whitespace(); - if let Some(direction_txt) = words.next() { + let reply = if let Some(direction_txt) = words.next() { // Target amount to move let speed = 32f32; let amount = words.next().unwrap_or("32.0"); + if let Ok(amount) = amount.parse::() { let seconds = amount / speed; // range checking on seconds so that you can't sleep for 3.4E+38 seconds - match seconds { - x if (0.0..10.0).contains(&x) => seconds, + let seconds = match seconds { + x if (0.0..10.0).contains(&x) => Some(seconds), _ => { error!("seconds was not a valid number (out of range)"); - return Ok(()); + None } }; - let direction = match direction_txt { - "up" => Direction::Up(speed, seconds), - "down" => Direction::Down(speed, seconds), - "left" => Direction::Left(speed, seconds), - "right" => Direction::Right(speed, seconds), - "in" => Direction::In(speed, seconds), - "out" => Direction::Out(speed, seconds), - _ => { - error!("Unrecognized PTZ direction \"{}\"", direction_txt); - return Ok(()); + let bc_direction = match direction_txt { + "up" => Some(BcDirection::Up), + "down" => Some(BcDirection::Down), + "left" => Some(BcDirection::Left), + "right" => Some(BcDirection::Right), + "in" => Some(BcDirection::In), + "out" => Some(BcDirection::Out), + n => { + error!("Unrecognized PTZ direction \"{}\"", n); + None } }; - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::Ptz(direction)) + + if let (Some(seconds), Some(bc_direction)) = (seconds, bc_direction) { + if let Err(e) = camera + .run_task(|cam| { + Box::pin(async move { + cam.send_ptz(bc_direction, speed).await?; + AnyResult::Ok(()) + }) + }) .await - .with_context(|| "Failed to send PTZ")?, - ); + { + error!("Failed to send PTZ: {:?}", e); + "FAIL" + } else { + // sleep for the designated seconds + sleep(Duration::from_secs_f32(seconds)).await; + + // note that amount is not used in the stop command + if let Err(e) = camera + .run_task(|cam| { + Box::pin(async move { + cam.send_ptz(bc_direction, speed).await?; + AnyResult::Ok(()) + }) + }) + .await + { + error!("Failed to send PTZ: {:?}", e); + "FAIL" + } else { + "OK" + } + } + } else { + "FAIL" + } } else { - error!("No PTZ direction speed was not a valid number"); + error!("No PTZ speed as a valid number"); + "FAIL" } } else { error!("No PTZ Direction given. Please add up/down/left/right/in/out"); + "FAIL" } + .to_string(); + + mqtt.send_message("control/ptz", &reply, false) + .await + .with_context(|| "Failed to publish reboot on the camera")?; } MqttReplyRef { topic: "control/ptz/preset", message, } => { - if let Ok(id) = message.parse::() { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::Preset(id)) - .await - .with_context(|| "Failed to send PTZ preset")?, - ); + let reply = if let Ok(id) = message.parse::() { + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.moveto_ptz_preset(id).await?; + AnyResult::Ok(()) + }) + }) + .await; + if res.is_err() { + error!("Failed to move to ptz preset: {:?}", res.err()); + "FAIL" + } else { + "OK" + } } else { error!("PTZ preset was not a valid number"); + "FAIL" } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish ptz move")?; } MqttReplyRef { topic: "control/ptz/assign", @@ -426,90 +717,218 @@ async fn handle_mqtt_message( let id = words.next(); let name = words.next(); - if let (Some(Ok(id)), Some(name)) = (id.map(|id| id.parse::()), name) { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::PresetAssign(id, name.to_owned())) - .await - .with_context(|| "Failed to send PTZ preset assign")?, - ); + let reply = if let (Some(Ok(id)), Some(name)) = (id.map(|id| id.parse::()), name) { + let name = name.to_owned(); + let res = camera + .run_task(|cam| { + let name = name.clone(); + Box::pin(async move { + cam.set_ptz_preset(id, name).await?; + AnyResult::Ok(()) + }) + }) + .await; + if res.is_err() { + error!("Failed to assign ptz preset: {:?}", res.err()); + "FAIL" + } else { + "OK" + } } else if let (Some(Err(_)), _) = (id.map(|id| id.parse::()), name) { error!("PTZ preset was not a valid number"); + "FAIL" } else if let (_, None) = (id.map(|id| id.parse::()), name) { error!("PTZ preset was not given a name"); + "FAIL" + } else { + "FAIL" } + .to_string(); + mqtt.send_message("control/ir", &reply, false) + .await + .with_context(|| "Failed to publish ptz move")?; } MqttReplyRef { topic: "control/pir", message: "on", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::PIROn) - .await - .with_context(|| "Failed to set pir on")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.pir_set(true).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn on the pir: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/pir", &reply, false) + .await + .with_context(|| "Failed to publish pir on")?; } MqttReplyRef { topic: "control/pir", message: "off", } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::PIROff) - .await - .with_context(|| "Failed to set pir off")?, - ); + let res = camera + .run_task(|cam| { + Box::pin(async move { + cam.pir_set(false).await?; + AnyResult::Ok(()) + }) + }) + .await; + let reply = if res.is_err() { + error!("Failed to turn off the pir: {:?}", res.err()); + "FAIL" + } else { + "OK" + } + .to_string(); + mqtt.send_message("control/pir", &reply, false) + .await + .with_context(|| "Failed to publish pir off")?; } MqttReplyRef { topic: "query/battery", .. } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::Battery) - .await - .with_context(|| "Failed to get battery status")?, - ); - reply_topic = Some("status/battery"); + let res = camera + .run_task(|cam| { + Box::pin(async move { + let xml = cam.battery_info().await?; + AnyResult::Ok(xml) + }) + }) + .await; + let reply = match res { + Err(e) => { + error!("Failed to get battery xml: {:?}", e); + "FAIL" + } + Ok(xml) => { + let bytes_res = + yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default()); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => { + mqtt.send_message("status/battery", &str, false) + .await + .with_context(|| "Failed to publish battery info")?; + "OK" + } + Err(_) => { + error!("Failed to encode battery status"); + "FAIL" + } + }, + Err(_) => { + error!("Failed to serialise battery status"); + "FAIL" + } + } + } + } + .to_string(); + mqtt.send_message("query/battery", &reply, false) + .await + .with_context(|| "Failed to publish battery query")?; } MqttReplyRef { topic: "query/pir", .. } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::PIRQuery) - .await - .with_context(|| "Failed to get pir status")?, - ); - reply_topic = Some("status/pir"); + let res = camera + .run_task(|cam| { + Box::pin(async move { + let xml = cam.get_pirstate().await?; + AnyResult::Ok(xml) + }) + }) + .await; + let reply = match res { + Err(e) => { + error!("Failed to get pir xml: {:?}", e); + "FAIL" + } + Ok(xml) => { + let bytes_res = + yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default()); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => { + mqtt.send_message("status/pir", &str, false) + .await + .with_context(|| "Failed to publish pir info")?; + "OK" + } + Err(_) => { + error!("Failed to encode pur status"); + "FAIL" + } + }, + Err(_) => { + error!("Failed to serialise pir status"); + "FAIL" + } + } + } + } + .to_string(); + mqtt.send_message("query/pir", &reply, false) + .await + .with_context(|| "Failed to publish pir query")?; } MqttReplyRef { topic: "query/ptz/preset", .. } => { - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::PresetQuery) - .await - .with_context(|| "Failed to get prz preset status")?, - ); - reply_topic = Some("status/ptz/preset"); + let res = camera + .run_task(|cam| { + Box::pin(async move { + let xml = cam.get_ptz_preset().await?; + AnyResult::Ok(xml) + }) + }) + .await; + let reply = match res { + Err(e) => { + error!("Failed to get ptz xml: {:?}", e); + "FAIL" + } + Ok(xml) => { + let bytes_res = + yaserde::ser::serialize_with_writer(&xml, vec![], &Default::default()); + match bytes_res { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(str) => { + mqtt.send_message("status/ptz", &str, false) + .await + .with_context(|| "Failed to publish ptz info")?; + "OK" + } + Err(_) => { + error!("Failed to encode ptz status"); + "FAIL" + } + }, + Err(_) => { + error!("Failed to serialise ptz status"); + "FAIL" + } + } + } + } + .to_string(); + mqtt.send_message("query/ptz", &reply, false) + .await + .with_context(|| "Failed to publish ptz query")?; } _ => {} } - 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")?; - } - } Ok(()) } diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index 6f49954..52b091c 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -1,23 +1,357 @@ -use crate::config::MqttConfig; +use crate::{ + config::{Config, MqttServerConfig}, + AnyResult, +}; use anyhow::{anyhow, Context, Result}; +use futures::future::FutureExt; use log::*; use rumqttc::{ - AsyncClient, ClientError, ConnectReturnCode, Event, EventLoop, Incoming, Key, LastWill, - MqttOptions, Publish, QoS, TlsConfiguration, Transport, + AsyncClient, ConnectReturnCode, Event, Incoming, Key, LastWill, MqttOptions, QoS, + TlsConfiguration, Transport, }; use std::sync::Arc; -use std::time::Duration; -use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio::task::JoinSet; +use tokio::{ + sync::{ + broadcast::{channel as broadcast, Sender as BroadcastSender}, + mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender}, + oneshot::{channel as oneshot, Sender as OneshotSender}, + watch::Receiver as WatchReceiver, + }, + time::{sleep, Duration}, +}; +use tokio_stream::{wrappers::BroadcastStream, StreamExt}; +use tokio_util::sync::CancellationToken; pub(crate) struct Mqtt { - client: Arc, - name: String, - incoming: Receiver, + cancel: CancellationToken, + outgoing_tx: MpscSender, set: JoinSet>, - drop_message: Option, } +impl Mqtt { + pub(crate) async fn new(config: WatchReceiver) -> Self { + let (incoming_tx, _) = broadcast::(100); + let (outgoing_tx, mut outgoing_rx) = mpsc::(100); + let cancel = CancellationToken::new(); + let mut set = JoinSet::>::new(); + + // Thread that handles the mqttc side + // including restarting it if the config changes + let thread_cancel = cancel.clone(); + let mut thread_config = config; + let thread_incoming_tx = incoming_tx; + let thread_outgoing_tx = outgoing_tx.clone(); + set.spawn(async move { + let mut mqtt_config = thread_config.borrow().mqtt.clone(); + loop { + break tokio::select! { + _ = thread_cancel.cancelled() => AnyResult::Ok(()), + v = thread_config.wait_for(|config| config.mqtt != mqtt_config).map(|res| res.map(|r| r.clone())) => + { + mqtt_config = v?.mqtt.clone(); + continue; + } + v = async { + run_mqtt_server(thread_incoming_tx.clone(), &mut outgoing_rx, thread_outgoing_tx.clone(), mqtt_config.as_ref().unwrap()).await + }, if mqtt_config.is_some() => { + if let Err(e) = &v { + log::error!("MQTT Client Connection Failed: {:?}", e); + sleep(Duration::from_secs(2)).await; + continue; + } + v + }, + }; + } + }); + + Self { + cancel, + outgoing_tx, + set, + // Send the drop message on clean disconnect + } + } + + pub async fn subscribe>(&self, name: T) -> AnyResult { + let (tx, rx) = oneshot(); + self.outgoing_tx + .send(MqttRequest::Subscribe(name.into(), tx)) + .await?; + rx.await? + } +} + +async fn run_mqtt_server( + incomming_tx: BroadcastSender, + outgoing_rx: &mut MpscReceiver, + outgoing_tx: MpscSender, + config: &MqttServerConfig, +) -> AnyResult<()> { + let mut mqttoptions = MqttOptions::new("Neolink".to_string(), &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 { + if let Ok(ca) = std::fs::read(ca_path) { + // Use client_auth if they have cert and key + let client_auth = if let Some((cert_path, key_path)) = &config.client_auth { + if let (Ok(cert_buf), Ok(key_buf)) = + (std::fs::read(cert_path), std::fs::read(key_path)) + { + Some((cert_buf, Key::RSA(key_buf))) + } else { + error!("Failed to set client tls"); + None + } + } else { + None + }; + + let transport = Transport::Tls(TlsConfiguration::Simple { + ca, + alpn: None, + client_auth, + }); + mqttoptions.set_transport(transport); + } else { + error!("Failed to set CA"); + } + }; + + if let Some((username, password)) = &config.credentials { + mqttoptions.set_credentials(username, password); + } + + mqttoptions.set_keep_alive(Duration::from_secs(5)); + + // On unclean disconnect send this + mqttoptions.set_last_will(LastWill::new( + "neolink/status".to_string(), + "offline", + QoS::AtLeastOnce, + true, + )); + + let (client, mut connection) = AsyncClient::new(mqttoptions, 10); + + let client = Arc::new(client); + let send_client = client.clone(); + send_client + .publish( + "neolink/status".to_string(), + QoS::AtLeastOnce, + true, + "connected".to_string(), + ) + .await?; + loop { + let r = tokio::select! { + v = async { + let msg = outgoing_rx.recv().await.ok_or(anyhow!("All outgoing MQTT channels closed"))?; + match msg { + MqttRequest::Send(msg) => { + send_client.publish( + msg.topic, + QoS::AtLeastOnce, + false, + msg.message, + ).await?; + } + MqttRequest::SendRetained(msg) => { + send_client.publish( + msg.topic, + QoS::AtLeastOnce, + true, + msg.message, + ).await?; + } + MqttRequest::HangUp(reply) => { + send_client.publish( + "neolink/status".to_string(), + QoS::AtLeastOnce, + true, + "disconnected".to_string(), + ).await?; + let _ = reply.send(()); + } + MqttRequest::Subscribe(name, reply) => { + let instance = MqttInstance { + name, + incomming_rx: BroadcastStream::new(incomming_tx.subscribe()), + outgoing_tx: outgoing_tx.clone(), + }; + let _ = reply.send(Ok(instance)); + } + } + + AnyResult::Ok(()) + } => { + v + }, + v = async { + let notification = connection + .poll() + .await + .with_context(|| "MQTT connection dropped")?; + match notification { + Event::Incoming(Incoming::ConnAck(connected)) => { + if ConnectReturnCode::Success == connected.code { + // Publish disconnected at first then wait for connect from the camera insances + client + .publish( + "neolink/status".to_string(), + QoS::AtLeastOnce, + true, + "disconnected", + ) + .await?; + // We succesfully logged in. Now ask for the cameras subscription. + client + .subscribe("neolink/".to_string(), QoS::AtMostOnce) + .await?; + } + } + Event::Incoming(Incoming::Publish(published_message)) => { + if let Some(sub_topic) = published_message + .topic + .strip_prefix("neolink/") + { + let _ = incomming_tx + .send(MqttReply { + topic: sub_topic.to_string(), + message: String::from_utf8_lossy(published_message.payload.as_ref()) + .into_owned(), + }); + } + } + _ => {} + } + AnyResult::Ok(()) + } => { + v + }, + }; + if r.is_ok() { + continue; + } + break r; + }?; + Ok(()) +} + +impl Drop for Mqtt { + fn drop(&mut self) { + tokio::task::block_in_place(move || { + let _ = tokio::runtime::Handle::current().block_on(async move { + let (tx, rx) = oneshot(); + let _ = self.outgoing_tx.send(MqttRequest::HangUp(tx)).await; + let _ = rx.await; + + self.cancel.cancel(); + while self.set.join_next().await.is_some() {} + AnyResult::Ok(()) + }); + }); + } +} + +pub(crate) struct MqttInstance { + outgoing_tx: MpscSender, + incomming_rx: BroadcastStream, + name: String, +} + +impl MqttInstance { + pub async fn subscribe>(&self, name: T) -> AnyResult { + let (tx, rx) = oneshot(); + self.outgoing_tx + .send(MqttRequest::Subscribe(name.into(), tx)) + .await?; + rx.await? + } + + pub async fn resubscribe(&self) -> AnyResult { + let (tx, rx) = oneshot(); + self.outgoing_tx + .send(MqttRequest::Subscribe(self.name.clone(), tx)) + .await?; + rx.await? + } + + pub async fn send_message_with_root_topic( + &self, + root_topic: &str, + sub_topic: &str, + message: &str, + retain: bool, + ) -> AnyResult<()> { + let topics = vec![ + root_topic.to_string(), + self.name.clone(), + sub_topic.to_string(), + ] + .iter() + .filter(|s| !s.is_empty()) + .cloned() + .collect::>(); + if retain { + self.outgoing_tx + .send(MqttRequest::SendRetained(MqttReply { + topic: topics.join("/"), + message: message.to_string(), + })) + .await?; + } else { + self.outgoing_tx + .send(MqttRequest::Send(MqttReply { + topic: topics.join("/"), + message: message.to_string(), + })) + .await?; + } + Ok(()) + } + + pub async fn send_message( + &self, + sub_topic: &str, + message: &str, + retain: bool, + ) -> AnyResult<()> { + self.send_message_with_root_topic("neolink", sub_topic, message, retain) + .await?; + Ok(()) + } + + pub(crate) async fn recv(&mut self) -> AnyResult { + Ok(loop { + let msg = self + .incomming_rx + .next() + .await + .ok_or(anyhow!("End or client data"))? + .with_context(|| "Clinet stream is too far behind")?; + + if self.name.is_empty() { + break msg; + } else { + let topics = msg.topic.split('/').collect::>(); + if topics + .get(2) + .map(|subtopic| *subtopic == self.name) + .unwrap_or(false) + { + break msg; + } + } + }) + } +} + +#[derive(Clone)] pub(crate) struct MqttReply { pub(crate) topic: String, pub(crate) message: String, @@ -37,259 +371,9 @@ pub(crate) struct MqttReplyRef<'a> { pub(crate) message: &'a str, } -struct MqttReciever { - client: MqttSender, - incoming_tx: Sender, - name: String, -} - -#[derive(Clone)] -pub(crate) struct MqttSender { - client: Arc, - name: String, -} - -impl MqttSender { - pub async fn send_message_with_root_topic( - &self, - root_topic: &str, - sub_topic: &str, - message: &str, - retain: bool, - ) -> Result<(), ClientError> { - self.client - .publish( - format!("{}/{}/{}", root_topic, self.name, sub_topic), - QoS::AtLeastOnce, - retain, - message, - ) - .await?; - Ok(()) - } - - pub async fn send_message( - &self, - sub_topic: &str, - message: &str, - retain: bool, - ) -> Result<(), ClientError> { - self.send_message_with_root_topic("neolink", sub_topic, message, retain) - .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); - 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"); - } - } - _ => {} - } - } - } - - 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) async fn new(config: &MqttConfig, name: &str) -> Self { - let (incoming_tx, incoming) = channel::(100); - let mut mqttoptions = MqttOptions::new( - format!("Neolink-{}", name), - &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 { - if let Ok(ca) = std::fs::read(ca_path) { - // Use client_auth if they have cert and key - let client_auth = if let Some((cert_path, key_path)) = &config.client_auth { - if let (Ok(cert_buf), Ok(key_buf)) = - (std::fs::read(cert_path), std::fs::read(key_path)) - { - Some((cert_buf, Key::RSA(key_buf))) - } else { - error!("Failed to set client tls"); - None - } - } else { - None - }; - - let transport = Transport::Tls(TlsConfiguration::Simple { - ca, - alpn: None, - client_auth, - }); - mqttoptions.set_transport(transport); - } else { - error!("Failed to set CA"); - } - }; - - if let Some((username, password)) = &config.credentials { - mqttoptions.set_credentials(username, password); - } - - mqttoptions.set_keep_alive(Duration::from_secs(5)); - - // On unclean disconnect send this - mqttoptions.set_last_will(LastWill::new( - format!("neolink/{}/status", name), - "offline", - QoS::AtLeastOnce, - true, - )); - - let (client, mut connection) = AsyncClient::new(mqttoptions, 10); - - let client = Arc::new(client); - - // Start the mqtt server - let mut set = JoinSet::>::new(); - - let mut reciever = MqttReciever { - incoming_tx, - name: name.to_string(), - client: MqttSender { - client: client.clone(), - name: name.to_string(), - }, - }; - set.spawn(async move { reciever.run(&mut connection).await }); - - Self { - client, - name: name.to_string(), - incoming, - set, - // Send the drop message on clean disconnect - drop_message: Some(MqttReply { - topic: "status".to_string(), - message: "offline".to_string(), - }), - } - } - - pub fn get_sender(&self) -> MqttSender { - MqttSender { - client: self.client.clone(), - name: self.name.to_string(), - } - } - - /// This will also error is the join set errors - pub(crate) async fn poll(&mut self) -> Result { - 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"), - } - } -} - -impl Drop for Mqtt { - fn drop(&mut self) { - if let Some(drop_message) = self.drop_message.as_ref() { - 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(); - } +enum MqttRequest { + Send(MqttReply), + SendRetained(MqttReply), + HangUp(OneshotSender<()>), + Subscribe(String, OneshotSender>), } diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 7fedf33..e21cf1e 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -2,7 +2,6 @@ //! data using an ordinary std::io::Write interface. mod factory; -mod sender; mod server; mod shared; diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/rtsp/gst/sender.rs +++ /dev/null @@ -1 +0,0 @@ -