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

+4
View File
@@ -19,6 +19,8 @@ pub const MSG_ID_TALKABILITY: u32 = 10;
pub const MSG_ID_TALKRESET: u32 = 11;
/// PtzControl messages have this ID
pub const MSG_ID_PTZ_CONTROL: u32 = 18;
/// PTZ goto preset position
pub const MSG_ID_PTZ_CONTROL_PRESET: u32 = 19;
/// Reboot messages have this ID
pub const MSG_ID_REBOOT: u32 = 23;
/// Request motion detection messages
@@ -35,6 +37,8 @@ pub const MSG_ID_GET_GENERAL: u32 = 104;
pub const MSG_ID_ABILITY_INFO: u32 = 151;
/// Setting general system info (clock mostly) messages have this ID
pub const MSG_ID_SET_GENERAL: u32 = 105;
/// Get the available PTZ position presets
pub const MSG_ID_GET_PTZ_PRESET: u32 = 190;
/// Will send the talk config for talk back data to follow this msg
pub const MSG_ID_TALKCONFIG: u32 = 201;
/// Used to send talk back binary data
+37 -1
View File
@@ -76,6 +76,9 @@ pub struct BcXml {
/// Sent to move the camera
#[yaserde(rename = "PtzControl")]
pub ptz_control: Option<PtzControl>,
/// Sent or received for the PTZ preset functionality
#[yaserde(rename = "PtzPreset")]
pub ptz_preset: Option<PtzPreset>,
/// Recieved on login/low battery events
#[yaserde(rename = "BatteryList")]
pub battery_list: Option<BatteryList>,
@@ -519,10 +522,43 @@ pub struct PtzControl {
pub channel_id: u8,
/// The amount of movement to perform
pub speed: f32,
/// The direction to transverse. Known directions: "right"
/// The direction to transverse. Known values are `"left"`, `"right"`, `"up"`, `"down"`,
/// `"leftUp"`, `"leftDown"`, `"rightUp"`, `"rightDown"` and `"stop"`
pub command: String,
}
/// An XML that describes a list of available PTZ presets
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct PtzPreset {
/// XML Version
#[yaserde(attribute)]
pub version: String,
/// The channel ID. Usually zero unless from an NVR
#[yaserde(rename = "channelId")]
pub channel_id: u8,
/// List of presets
#[yaserde(rename = "presetList")]
pub preset_list: Option<PresetList>,
}
/// A preset list
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct PresetList {
/// List of Presets
pub preset: Vec<Preset>,
}
/// A preset. Either contains the ID and the name or the ID and the command
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct Preset {
/// The ID of the preset
pub id: i8,
/// The preset name
pub name: Option<String>,
/// Command: Known values: `"toPos"` and `"setPos"`
pub command: Option<String>,
}
/// A list of battery infos. This message is sent from the camera as
/// an event
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
+105
View File
@@ -82,4 +82,109 @@ impl BcCamera {
})
}
}
/// Get the [PtzPreset] XML which contains the list of the preset positions known to the camera
pub async fn get_ptz_preset(&self) -> Result<PtzPreset> {
self.has_ability_rw("control").await?;
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_set = connection.subscribe(msg_num).await?;
let send = Bc {
meta: BcMeta {
msg_id: MSG_ID_GET_PTZ_PRESET,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: Some(Extension {
channel_id: Some(self.channel_id),
..Default::default()
}),
payload: None,
}),
};
sub_set.send(send).await?;
let msg = sub_set.recv().await?;
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
ptz_preset: Some(ptz_preset),
..
})),
..
}) = msg.body
{
Ok(ptz_preset)
} else {
Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "The camera did not return a valid PtzPreset xml",
})
}
}
/// Set a PTZ preset. If a [name] is given the current position will be saved as a preset
/// with the given [preset_id] and [name], otherwise the camera will attempt to move to the
/// preset with the given ID.
pub async fn set_ptz_preset(&self, preset_id: i8, name: Option<String>) -> Result<()> {
self.has_ability_rw("control").await?;
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub_set = connection.subscribe(msg_num).await?;
let command = if name.is_some() { "setPos" } else { "toPos" };
let preset = Preset {
id: preset_id,
name,
command: Some(command.to_string()),
..Default::default()
};
let send = Bc {
meta: BcMeta {
msg_id: MSG_ID_PTZ_CONTROL_PRESET,
channel_id: self.channel_id,
msg_num,
response_code: 0,
stream_type: 0,
class: 0x6414,
},
body: BcBody::ModernMsg(ModernMsg {
extension: Some(Extension {
channel_id: Some(self.channel_id),
..Default::default()
}),
payload: Some(BcPayloads::BcXml(BcXml {
ptz_preset: Some(PtzPreset {
preset_list: Some(PresetList {
preset: vec![preset],
}),
..Default::default()
}),
..Default::default()
})),
}),
};
sub_set.send(send).await?;
let msg = sub_set.recv().await?;
if let BcMeta {
response_code: 200, ..
} = msg.meta
{
Ok(())
} else {
Err(Error::UnintelligibleReply {
reply: std::sync::Arc::new(Box::new(msg)),
why: "The camera did not accept the PtzPreset xml",
})
}
}
}
+1
View File
@@ -88,6 +88,7 @@ local message_types = {
[15]="<FileInfoList>",
[16]="<FileInfoList>",
[18]="<PtzControl>",
[19]="<PtzPreset>",
[23]="Reboot",
[25]="<VideoInput> (write)",
[26]="<VideoInput>", -- <InputAdvanceCfg>
+49 -1
View File
@@ -354,6 +354,10 @@ Message have zero to two payloads.
</PtzControl>
</body>
```
- **Notes** : The known movement commands are `"left"`, `"right"`, `"up"`, `"down"`, `"leftUp"`,
`"leftDown"`, `"rightUp"`, `"rightDown"` and `"stop"` although the diagonal movement
does not seem to work.
- Camera
@@ -363,6 +367,45 @@ Message have zero to two payloads.
|--------------|--------------|----------------|-------------------|-------------------|---------------|---------------|
| 0a bc de f0 | 00 00 00 12 | 00 00 00 00 | 1e 00 00 00 | c8 00 | 00 00 | 00 00 00 00 |
- 19: `<PtzPreset>`
- Client
- Header
| Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Payload Offset |
|--------------|-------------|----------------|-------------------|-------------------|---------------|---------------|
| 0a bc de f0 | 00 00 00 13 | 00 00 01 44 | 1e 00 00 00 | 00 00 | 64 14 | 00 00 00 00 |
- Payload
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PtzPreset version="1.1">
<channelId>0</channelId>
<presetList>
<preset>
<id>0</id>
<command>setPos</command>
<name>Test</name>
</preset>
</presetList>
</PtzPreset>
</body>
```
- **Notes** : The known values for command are `"setPos"` and `"toPos"`
- Camera
- Header
| Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Payload Offset |
|--------------|-------------|----------------|-------------------|-------------------|---------------|---------------|
| 0a bc de f0 | 00 00 00 13 | 00 00 00 00 | 1e 00 00 00 | c8 00 | 00 00 | 00 00 00 00 |
- 23: `Reboot`
- Client
@@ -1987,7 +2030,12 @@ Message have zero to two payloads.
<body>
<PtzPreset version="1.1">
<channelId>0</channelId>
<presetList />
<presetList>
<preset>
<id>0</id>
<name>Test</name>
</preset>
</presetList>
</PtzPreset>
</body>
```
+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(())
}