From 61a5073adb907eaa47a6b961073ceeac77b6aa29 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 24 Oct 2023 10:39:30 +0700 Subject: [PATCH] Add methods to get current zoom and its limits --- crates/core/src/bc/model.rs | 6 ++- crates/core/src/bc/xml.rs | 26 +++++++++-- crates/core/src/bc_protocol/ptz.rs | 69 +++++++++++++++++++++++++++--- dissector/baichuan.lua | 2 + dissector/messages.md | 31 +++++++++++++- 5 files changed, 121 insertions(+), 13 deletions(-) diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index aa43820..3353fe3 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -77,8 +77,10 @@ pub const MSG_ID_FLOODLIGHT_MANUAL: u32 = 288; pub const MSG_ID_FLOODLIGHT_TASKS_WRITE: u32 = 290; /// Floodlight status report from the camera pub const MSG_ID_FLOODLIGHT_STATUS_LIST: u32 = 291; -/// Used for camera Zoom -pub const MSG_ID_ZOOM_FOCUS: u32 = 295; +/// Used for camera Zoom read +pub const MSG_ID_GET_ZOOM_FOCUS: u32 = 294; +/// Used for camera Zoom write +pub const MSG_ID_SET_ZOOM_FOCUS: u32 = 295; /// Get the floodlight task xml pub const MSG_ID_FLOODLIGHT_TASKS_READ: u32 = 438; diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index a909cbd..1aafe0a 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -984,11 +984,29 @@ pub struct StartZoomFocus { /// Channel ID #[yaserde(rename = "channelId")] pub channel_id: u8, - /// Command: Observed values: zoomPos - pub command: String, - /// Target Position: Observed Values: 2994, 2508, 2888, 3089, 3194, 3163 + /// Command: Observed values: zoomPos. (Write Only) + pub command: Option, + /// Target Position: Observed Values: 2994, 2508, 2888, 3089, 3194, 3163. (Write Only) #[yaserde(rename = "movePos")] - pub move_pos: u32, + pub move_pos: Option, + /// Max, min and current zoom. (Read Only) + pub zoom: Option, + /// Max, min and current focus. (Read Only) + pub focus: Option, +} + +/// Helper for Max, Min, Curr pos of zoom/focus +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct HelperPosition { + /// Max value + #[yaserde(rename = "maxPos")] + pub maxPos: u32, + /// Min value + #[yaserde(rename = "minPos")] + pub minPos: u32, + /// Curr value + #[yaserde(rename = "curPos")] + pub curPos: u32, } /// Support xml diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs index 9b7c5ac..8c8d746 100644 --- a/crates/core/src/bc_protocol/ptz.rs +++ b/crates/core/src/bc_protocol/ptz.rs @@ -237,15 +237,23 @@ impl BcCamera { } /// The camera will zoom to a given zoom amount. - /// Not sure what the units for this are. Or how to get the min/max values - pub async fn zoom_to(&self, zoom_pos: u32) -> Result<()> { + /// Not sure what the units for this are, seems to be 1000 is 1x and 2000 is 2x + pub async fn zoom_to(&self, in_zoom_pos: u32) -> Result<()> { + let current = self.get_zoom().await?; + let zoom_pos; + if let Some(zoom) = current.zoom { + zoom_pos = in_zoom_pos.clamp(zoom.minPos, zoom.maxPos); + } else { + zoom_pos = in_zoom_pos; + } + 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_ID_ZOOM_FOCUS, msg_num).await?; + let mut sub_set = connection.subscribe(MSG_ID_SET_ZOOM_FOCUS, msg_num).await?; let send = Bc { meta: BcMeta { - msg_id: MSG_ID_ZOOM_FOCUS, + msg_id: MSG_ID_SET_ZOOM_FOCUS, channel_id: self.channel_id, msg_num, response_code: 0, @@ -262,8 +270,9 @@ impl BcCamera { start_zoom_focus: Some(StartZoomFocus { version: xml_ver(), channel_id: self.channel_id, - command: "zoomPos".to_string(), - move_pos: zoom_pos, + command: Some("zoomPos".to_string()), + move_pos: Some(zoom_pos), + ..Default::default() }), ..Default::default() })), @@ -286,4 +295,52 @@ impl BcCamera { }) } } + + /// Get the zoom xml, that has current min and max zoom values + pub async fn get_zoom(&self) -> Result { + self.has_ability_ro("control").await?; + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let mut sub_get = connection.subscribe(MSG_ID_GET_ZOOM_FOCUS, msg_num).await?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_GET_ZOOM_FOCUS, + 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_get.send(get).await?; + let msg = sub_get.recv().await?; + if msg.meta.response_code != 200 { + return Err(Error::CameraServiceUnavaliable(msg.meta.response_code)); + } + + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + start_zoom_focus: Some(xml), + .. + })), + .. + }) = msg.body + { + Ok(xml) + } else { + Err(Error::UnintelligibleReply { + reply: std::sync::Arc::new(Box::new(msg)), + why: "Expected StartZoomFocus xml but it was not recieved", + }) + } + } } diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index e9b403f..e55a6ee 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -190,6 +190,8 @@ local message_types = { [273]="", [274]="", [291]="", + [294]=" (read)", + [295]=" (write)", [299]="", [319]="", [342]="", diff --git a/dissector/messages.md b/dissector/messages.md index 60ef45e..0583e9a 100644 --- a/dissector/messages.md +++ b/dissector/messages.md @@ -2968,7 +2968,36 @@ Message have zero to two payloads. ``` -- 295: `` +- 294: `` (Read) + + - Client: Standard header and channel extension + + - Camera: + + Usual header and Extension XML with channel + + - Payload: + + ```xml + + + + 1 + + 6000 + 1000 + 2501 + + + 100 + 1 + 51 + + + + ``` + +- 295: `` (Write) - Client