diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 788f85f..80c9cf0 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -9,8 +9,11 @@ use std::sync::Arc; use tokio::{ sync::mpsc::{channel, Receiver, Sender}, task::JoinSet, + time::sleep }; +use core::time::Duration; + #[derive(Debug, Copy, Clone)] pub(crate) enum Messages { Login, @@ -31,13 +34,12 @@ pub(crate) enum Messages { #[derive(Debug, Copy, Clone)] pub(crate) enum Direction { - Up(f32), - Down(f32), - Left(f32), - Right(f32), - In(f32), - Out(f32), - Stop(f32) + Up(f32, f32), + Down(f32, f32), + Left(f32, f32), + Right(f32, f32), + In(f32, f32), + Out(f32, f32) } #[derive(Debug)] @@ -409,20 +411,28 @@ impl<'a> MessageHandler<'a> { } } 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), - Direction::Stop(amount) => (BcDirection::Stop, amount) + let (bc_direction, amount, seconds) = match direction { + Direction::Up(amount, seconds) => (BcDirection::Up, amount, seconds), + Direction::Down(amount, seconds) => (BcDirection::Down, amount, seconds), + Direction::Left(amount, seconds) => (BcDirection::Left, amount, seconds), + Direction::Right(amount, seconds) => (BcDirection::Right, amount, seconds), + Direction::In(amount, seconds) => (BcDirection::In, amount, seconds), + Direction::Out(amount, seconds) => (BcDirection::Out, amount, seconds), }; 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() + // 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, amount).await { + error = Some(format!("Failed to send PTZ: {:?}", e)); + "FAIL".to_string() + } else { + "OK".to_string() + } } } _ => "UNKNOWN COMMAND".to_string(), diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 5bd0fd3..300814d 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -199,28 +199,42 @@ async fn listen_on_camera(cam_config: Arc, mqtt_config: &MqttConfi let mut words = lowercase_message.split_whitespace(); if let Some(direction_txt) = words.next() { let amount = words.next().unwrap_or("32.0"); + let seconds = words.next().unwrap_or("1.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), - "stop" => Direction::Stop(amount), - _ => { - error!("Unrecognized PTZ direction \"{}\"", direction_txt); - continue; - } - }; - reply = Some( - event_cam_sender - .send_message_with_reply(Messages::Ptz(direction)) - .await - .with_context(|| "Failed to send PTZ")?, - ); + if let Ok(seconds) = seconds.parse::() { + + // 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, + _ => { + error!("seconds was not a valid number (out of range)"); + continue; + } + }; + + let direction = match direction_txt { + "up" => Direction::Up(amount, seconds), + "down" => Direction::Down(amount, seconds), + "left" => Direction::Left(amount, seconds), + "right" => Direction::Right(amount, seconds), + "in" => Direction::In(amount, seconds), + "out" => Direction::Out(amount, seconds), + _ => { + error!("Unrecognized PTZ direction \"{}\"", direction_txt); + continue; + } + }; + reply = Some( + event_cam_sender + .send_message_with_reply(Messages::Ptz(direction)) + .await + .with_context(|| "Failed to send PTZ")?, + ); + } else { + error!("seconds was not a valid number"); + } } else { - error!("No PTZ direction speed was not a valid number"); + error!("No PTZ direction speed was not a valid number (unrecognized)"); } } else { error!("No PTZ Direction given. Please add up/down/left/right/in/out");