diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 86a51e9..6961221 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -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 diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index f7aeafe..19b0cce 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -76,6 +76,9 @@ pub struct BcXml { /// Sent to move the camera #[yaserde(rename = "PtzControl")] pub ptz_control: Option, + /// Sent or received for the PTZ preset functionality + #[yaserde(rename = "PtzPreset")] + pub ptz_preset: Option, /// Recieved on login/low battery events #[yaserde(rename = "BatteryList")] pub battery_list: Option, @@ -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, +} + +/// A preset list +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct PresetList { + /// List of Presets + pub preset: Vec, +} + +/// 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, + /// Command: Known values: `"toPos"` and `"setPos"` + pub command: Option, +} + /// A list of battery infos. This message is sent from the camera as /// an event #[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs index 3c925d3..0e08843 100644 --- a/crates/core/src/bc_protocol/ptz.rs +++ b/crates/core/src/bc_protocol/ptz.rs @@ -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 { + 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) -> 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", + }) + } + } } diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index d5b2079..6111e96 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -88,6 +88,7 @@ local message_types = { [15]="", [16]="", [18]="", + [19]="", [23]="Reboot", [25]=" (write)", [26]="", -- diff --git a/dissector/messages.md b/dissector/messages.md index cc87cc4..7d5042f 100644 --- a/dissector/messages.md +++ b/dissector/messages.md @@ -354,6 +354,10 @@ Message have zero to two payloads. ``` + + - **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: `` + + - 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 + + + + 0 + + + 0 + setPos + Test + + + + + ``` + + - **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. 0 - + + + 0 + Test + + ``` diff --git a/src/cmdline.rs b/src/cmdline.rs index ea2f319..6e47674 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -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), diff --git a/src/main.rs b/src/main.rs index efa9a3c..f20c6b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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?; } diff --git a/src/ptz/cmdline.rs b/src/ptz/cmdline.rs new file mode 100644 index 0000000..50b16dd --- /dev/null +++ b/src/ptz/cmdline.rs @@ -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, + name: Option, + }, + /// Performs a movement in the given direction + Control { + /// The time in milliseconds to move + duration: u32, + /// The direction command + #[clap(value_enum)] + command: CmdDirection, + }, +} diff --git a/src/ptz/mod.rs b/src/ptz/mod.rs new file mode 100644 index 0000000..8bf2846 --- /dev/null +++ b/src/ptz/mod.rs @@ -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(()) +}