From 77be9483b00bdd57882be8e36f898076e46e93a5 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 2 Apr 2023 18:39:36 +0700 Subject: [PATCH] Rework PIR data and fetch status --- crates/core/src/bc/model.rs | 8 +-- crates/core/src/bc/xml.rs | 49 ++++++++++++++++++- .../src/bc_protocol/connection/udpsource.rs | 13 +++-- dissector/baichuan.lua | 3 ++ src/pir/cmdline.rs | 2 +- src/pir/mod.rs | 21 ++++++-- 6 files changed, 81 insertions(+), 15 deletions(-) diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 141c84f..05de6e9 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -27,10 +27,6 @@ pub const MSG_ID_MOTION_REQUEST: u32 = 31; pub const MSG_ID_MOTION: u32 = 33; /// Version messages have this ID pub const MSG_ID_VERSION: u32 = 80; -/// Getting PIR status messages have this ID -pub const MSG_ID_GET_PIR_ALARM: u32 = 212; -/// Setting PIR status messages have this ID -pub const MSG_ID_START_PIR_ALARM: u32 = 213; /// Ping messages have this ID pub const MSG_ID_PING: u32 = 93; /// General system info messages have this ID @@ -45,6 +41,10 @@ pub const MSG_ID_TALK: u32 = 202; pub const MSG_ID_GET_LED_STATUS: u32 = 208; /// Setting the LED status is done with this ID pub const MSG_ID_SET_LED_STATUS: u32 = 209; +/// Getting PIR status messages have this ID +pub const MSG_ID_GET_PIR_ALARM: u32 = 212; +/// Setting PIR status messages have this ID +pub const MSG_ID_START_PIR_ALARM: u32 = 213; /// UDP Keep alive pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234; /// Battery message initiaed by the camera diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index add10c2..5a979e6 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -336,7 +336,54 @@ pub struct RfAlarmCfg { /// reduce False alarm boolean pub reduceFalseAlarm: u8, /// XML time block for all week days - pub timeBlockList: String, + #[yaserde(rename = "timeBlockList")] + pub time_block_list: TimeBlockList, + /// The alarm handle to attach to this Rf + #[yaserde(rename = "alarmHandle")] + pub alarm_handle: AlarmHandle, +} + +/// TimeBlockList XML +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename = "timeBlockList")] +pub struct TimeBlockList { + /// List of time block entries which disable/enable the PIR at a time + #[yaserde(rename = "timeBlock")] + pub time_block: Vec, +} + +/// TimeBlock XML Used to set the time to enable/disable PIR dectection +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[yaserde(rename = "timeBlock")] +pub struct TimeBlock { + /// Whether to enable or disable for this time block + pub enable: u8, + /// The day of the week for this block + pub weekDay: String, + /// Time to start this block + #[yaserde(rename = "beginHour")] + pub begin_hour: u8, + /// Time to end this block + #[yaserde(rename = "endHour")] + pub end_hour: u8, +} + +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +/// AlarmHandle Xml +pub struct AlarmHandle { + /// Items in the alarm handle + pub item: Vec, +} + +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +/// An item in the alarm handle +#[yaserde(rename = "item")] +pub struct AlarmHandleItem { + /// The channel ID + pub channel: u8, + /// The handle type: Known values, comma seperated list of snap,rec,push + #[yaserde(rename = "handleType")] + pub handle_type: String, } /// TalkConfig xml diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 3bc0ddc..3ab3733 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -253,12 +253,15 @@ impl UdpPayloadSource { fn handle_ack(&mut self, ack: UdpAck) { let start = ack.packet_id; - self.sent.retain(|&k, _| k > start); + // -1 is the special case of no messages recieved + if start != 0xFFFFFFFF { + self.sent.retain(|&k, _| k > start); - for (idx, &value) in ack.payload.iter().enumerate() { - let packet_id = (start + 1) + idx as u32; - if value > 0 { - self.sent.remove(&packet_id); + for (idx, &value) in ack.payload.iter().enumerate() { + let packet_id = (start + 1) + idx as u32; + if value > 0 { + self.sent.remove(&packet_id); + } } } } diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 34259c5..d5b2079 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -166,6 +166,7 @@ local message_types = { [210]="", [211]=" (write)", [212]="", + [213]=" (write)", [216]=" (write)", [217]="", [218]=" (write)", @@ -179,6 +180,8 @@ local message_types = { [272]="", [273]="", [274]="", + [319]="", + [342]="", } local message_classes = { diff --git a/src/pir/cmdline.rs b/src/pir/cmdline.rs index 36d2ed4..0933c89 100644 --- a/src/pir/cmdline.rs +++ b/src/pir/cmdline.rs @@ -19,5 +19,5 @@ pub struct Opt { pub camera: String, /// Whether to turn the PIR ON or OFF #[structopt(parse(try_from_str = onoff_parse), name = "on|off")] - pub on: bool, + pub on: Option, } diff --git a/src/pir/mod.rs b/src/pir/mod.rs index 724e699..6a028de 100644 --- a/src/pir/mod.rs +++ b/src/pir/mod.rs @@ -27,9 +27,22 @@ pub(crate) use cmdline::Opt; pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { let camera = find_and_connect(&config, &opt.camera).await?; - camera - .pir_set(opt.on) - .await - .context("Unable to set camera PIR state")?; + if let Some(on) = opt.on { + camera + .pir_set(on) + .await + .context("Unable to set camera PIR state")?; + } else { + let pir_state = camera + .get_pirstate() + .await + .context("Unable to get camera PIR state")?; + let pir_ser = String::from_utf8( + yaserde::ser::serialize_with_writer(&pir_state, vec![], &Default::default()) + .expect("Should Ser the struct"), + ) + .expect("Should be UTF8"); + println!("{}", pir_ser); + } Ok(()) }