Use same amount model as in mqtt and add optional speed

This commit is contained in:
QuantumEntangledAndy committed 2023-05-26 00:55:11 +07:00
1 parent da3afe228e
commit 4628946514
4 files changed
+42 -36

No files matched your search

+13 -19
View File
@@ -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 {
+8 -7
View File
@@ -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::<f32>() {
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(());
+4 -2
View File
@@ -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<u32>,
},
}
+17 -8
View File
@@ -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