From 9f3c482f5169cb08d993b3004bb5c962ef986c2a Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 14 Dec 2022 11:20:13 +0700 Subject: [PATCH] Added optional amount to ptz controls --- src/mqtt/event_cam.rs | 12 ++++++------ src/mqtt/mod.rs | 39 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 47d1e3e..70873c0 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -23,12 +23,12 @@ pub(crate) enum Messages { } pub(crate) enum Direction { - Up, - Down, - Left, - Right, - In, - Out, + Up(f32), + Down(f32), + Left(f32), + Right(f32), + In(f32), + Out(f32), } enum ToCamera { diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index e264850..65b8907 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -160,20 +160,33 @@ fn listen_on_camera( topic: "control/ptz", message, } => { - let direction = match message.to_lowercase().as_str() { - "up" => Direction::Up, - "down" => Direction::Down, - "left" => Direction::Left, - "right" => Direction::Right, - "in" => Direction::In, - "out" => Direction::Out, - _ => { - error!("Unrecongnized PTZ direction"); - continue; + 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.send_message(Messages::Ptz(direction)).is_err() { + error!("Failed to send PTZ"); + } + } else { + error!("No PTZ direction speed was not a valid number"); } - }; - if event_cam.send_message(Messages::Ptz(direction)).is_err() { - error!("Failed to send PTZ"); + } else { + error!( + "No PTZ Direction given. Please add up/down/left/right/in/out" + ); } } MqttReplyRef {