In mod.rs:
Removed the ability to send "stop" PTZ command with MQTT Changed the signature of PTZ MQTT commands to (direction amount seconds), where seconds is currently checked and limited to the range 0.0 - 10.0 In event_cam.rs: stop has been removed from event_cam.Direction, and directions now take an additional f32 seconds parameter in Message::PTZ if the intial send_ptz is successful, will sleep for designated seconds and then issue a stop command
This commit is contained in:
1 parent
29b47aa44e
commit
5ccdc27d37
2 files changed
+60
-36
No files matched your search
+26
-16
@@ -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(),
|
||||
|
||||
+34
-20
@@ -199,28 +199,42 @@ async fn listen_on_camera(cam_config: Arc<CameraConfig>, 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::<f32>() {
|
||||
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::<f32>() {
|
||||
|
||||
// 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");
|
||||
|
||||
Reference in new issue
Block a user