Add PTZ presets and PTZ command line

This commit is contained in:
Daniel Kaiser committed 2023-05-24 15:19:46 +02:00
1 parent 384a0271bd
commit aee14bb411
9 files changed
+320 -2

No files matched your search

+1
View File
@@ -21,6 +21,7 @@ pub enum Command {
StatusLight(super::statusled::Opt),
Reboot(super::reboot::Opt),
Pir(super::pir::Opt),
Ptz(super::ptz::Opt),
Talk(super::talk::Opt),
Mqtt(super::mqtt::Opt),
Image(super::image::Opt),
+4
View File
@@ -34,6 +34,7 @@ mod config;
mod image;
mod mqtt;
mod pir;
mod ptz;
mod reboot;
mod rtsp;
mod statusled;
@@ -102,6 +103,9 @@ async fn main() -> Result<()> {
Some(Command::Pir(opts)) => {
pir::main(opts, config).await?;
}
Some(Command::Ptz(opts)) => {
ptz::main(opts, config).await?;
}
Some(Command::Talk(opts)) => {
talk::main(opts, config).await?;
}
+40
View File
@@ -0,0 +1,40 @@
use clap::Parser;
#[derive(clap::ValueEnum, Clone, Debug)]
pub enum CmdDirection {
Left,
Right,
Up,
Down,
In,
Out,
Stop,
}
/// The ptz command will control the positioning of the camera
#[derive(Parser, Debug)]
pub struct Opt {
/// The name of the camera to change the lights of. Must be a name in the config
pub camera: String,
#[command(subcommand)]
pub cmd: PtzCommand,
}
#[derive(Parser, Debug)]
pub enum PtzCommand {
/// Gets the available presets on the camera, moves the camera to a given preset ID or saves
/// the current position as a preset with name and ID.
Preset {
preset_id: Option<i8>,
name: Option<String>,
},
/// Performs a movement in the given direction
Control {
/// The time in milliseconds to move
duration: u32,
/// The direction command
#[clap(value_enum)]
command: CmdDirection,
},
}
+79
View File
@@ -0,0 +1,79 @@
///
/// # Neolink PTZ Control
///
/// This module handles the controls of the PTZ commands
///
/// # Usage
///
/// ```bash
/// # Rotate left for 300 milliseconds
/// neolink status-light --config=config.toml CameraName control 300 left
/// # Print the list of preset positions
/// neolink status-light --config=config.toml CameraName preset
/// # Move the camera to preset ID 0
/// neolink status-light --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
/// ```
///
use anyhow::{Context, Result};
use std::thread::sleep;
use std::time::Duration;
mod cmdline;
use super::config::Config;
use crate::ptz::cmdline::CmdDirection;
use crate::ptz::cmdline::PtzCommand;
use crate::utils::find_and_connect;
pub(crate) use cmdline::Opt;
use neolink_core::bc_protocol::Direction;
/// Entry point for the ptz subcommand
///
/// Opt is the command line options
pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> {
let camera = find_and_connect(&config, &opt.camera).await?;
match opt.cmd {
PtzCommand::Preset { preset_id, name } => {
if preset_id.is_some() {
camera
.set_ptz_preset(preset_id.unwrap(), name)
.await
.context("Unable to set PTZ preset")
.expect("TODO: panic message");
} else {
let preset_list = camera
.get_ptz_preset()
.await
.context("Unable to get PTZ presets")?;
println!("Available presets:\nID Name");
for preset in preset_list.preset_list.unwrap().preset {
println!("{:<2} {}", preset.id, preset.name.unwrap());
}
}
}
PtzCommand::Control { duration, command } => {
let direction = match command {
CmdDirection::Left => Direction::Left,
CmdDirection::Right => Direction::Right,
CmdDirection::Up => Direction::Up,
CmdDirection::Down => Direction::Down,
CmdDirection::In => Direction::In,
CmdDirection::Out => Direction::Out,
CmdDirection::Stop => Direction::Stop,
};
camera
.send_ptz(direction, 32_f32)
.await
.context("Unable to execute PTZ move command")?;
sleep(Duration::from_millis(duration as u64));
camera
.send_ptz(Direction::Stop, 0_f32)
.await
.context("Unable to execute PTZ move command")?;
}
};
Ok(())
}