From 4628946514bad6809ac0b44144cb00f6a5ccef30 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 26 May 2023 00:55:11 +0700 Subject: [PATCH] Use same amount model as in mqtt and add optional speed --- src/mqtt/event_cam.rs | 32 +++++++++++++------------------- src/mqtt/mod.rs | 15 ++++++++------- src/ptz/cmdline.rs | 6 ++++-- src/ptz/mod.rs | 25 +++++++++++++++++-------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 95e4839..6fc652b 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -511,27 +511,23 @@ impl<'a> MessageHandler<'a> { } } Messages::Ptz(direction) => { - let (bc_direction, amount, seconds) = match direction { - Direction::Up(amount, seconds) => { - (BcDirection::Up, amount, seconds) + let (bc_direction, speed, seconds) = match direction { + Direction::Up(speed, seconds) => (BcDirection::Up, speed, seconds), + Direction::Down(speed, seconds) => { + (BcDirection::Down, speed, seconds) } - Direction::Down(amount, seconds) => { - (BcDirection::Down, amount, seconds) + Direction::Left(speed, seconds) => { + (BcDirection::Left, speed, seconds) } - Direction::Left(amount, seconds) => { - (BcDirection::Left, amount, seconds) + Direction::Right(speed, seconds) => { + (BcDirection::Right, speed, 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) + 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, amount).await { + if let Err(e) = self.camera.send_ptz(bc_direction, speed).await { error = Some(format!("Failed to send PTZ: {:?}", e)); "FAIL".to_string() } else { @@ -539,9 +535,7 @@ impl<'a> MessageHandler<'a> { 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 - { + 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 { diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index dab7b9e..d0b0056 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -333,9 +333,10 @@ async fn handle_mqtt_message( let mut words = lowercase_message.split_whitespace(); 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 / 32f32; + 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, @@ -346,12 +347,12 @@ async fn handle_mqtt_message( }; 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), + "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(()); diff --git a/src/ptz/cmdline.rs b/src/ptz/cmdline.rs index 50b16dd..6e54a6d 100644 --- a/src/ptz/cmdline.rs +++ b/src/ptz/cmdline.rs @@ -31,10 +31,12 @@ pub enum PtzCommand { }, /// Performs a movement in the given direction Control { - /// The time in milliseconds to move - duration: u32, + /// The amount to move + amount: u32, /// The direction command #[clap(value_enum)] command: CmdDirection, + /// The speed to move at + speed: Option, }, } diff --git a/src/ptz/mod.rs b/src/ptz/mod.rs index 93f0923..58fe4ad 100644 --- a/src/ptz/mod.rs +++ b/src/ptz/mod.rs @@ -6,14 +6,16 @@ /// # Usage /// /// ```bash -/// # Rotate left for 300 milliseconds -/// neolink status-light --config=config.toml CameraName control 300 left +/// # Rotate left by 32 +/// neolink ptz --config=config.toml CameraName control 32 left +/// # Rotate left by 32 at speed 10 (speed not supported on most camera) +/// neolink ptz --config=config.toml CameraName control 32 left 10 /// # Print the list of preset positions -/// neolink status-light --config=config.toml CameraName preset +/// neolink ptz --config=config.toml CameraName preset /// # Move the camera to preset ID 0 -/// neolink status-light --config=config.toml CameraName preset 0 +/// neolink ptz --config=config.toml CameraName preset 0 /// # Save the current position as preset ID 0 with name PresetName -/// neolink status-light --config=config.toml CameraName preset 0 PresetName +/// neolink ptz --config=config.toml CameraName preset 0 PresetName /// ``` /// use anyhow::{Context, Result}; @@ -53,7 +55,11 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { } } } - PtzCommand::Control { duration, command } => { + PtzCommand::Control { + amount, + command, + speed, + } => { let direction = match command { CmdDirection::Left => Direction::Left, CmdDirection::Right => Direction::Right, @@ -63,11 +69,14 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { CmdDirection::Out => Direction::Out, CmdDirection::Stop => Direction::Stop, }; + let speed = speed.unwrap_or(32) as f32; + let seconds = amount as f32 / speed; + let duration = Duration::from_secs_f32(seconds); camera - .send_ptz(direction, 32_f32) + .send_ptz(direction, speed) .await .context("Unable to execute PTZ move command")?; - sleep(Duration::from_millis(duration as u64)).await; + sleep(duration).await; camera .send_ptz(Direction::Stop, 0_f32) .await