WIP: Add support for floodlight control over MQTT

This commit is contained in:
Kevin David committed 2023-01-24 22:38:00 -05:00
1 parent 2e6f9c7190
commit 1f8296dd01
8 files changed
+222

No files matched your search

+3
View File
@@ -14,3 +14,6 @@
# Committing IntelliJ project files is a violation of intergalactic law
.idea/
# We prefer to keep .vscode files outside the repo
.vscode/
+18
View File
@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}",
"args": ["mqtt", "--config", "target/config.toml"],
"cwd": "${workspaceRoot}",
"sourceLanguages": [
"rust"
],
"env": {
"RUST_LOG": "debug"
}
}
]}
+5
View File
@@ -46,6 +46,11 @@ pub const MSG_ID_GET_LED_STATUS: u32 = 208;
pub const MSG_ID_SET_LED_STATUS: u32 = 209;
/// UDP Keep alive
pub const MSG_ID_UDP_KEEP_ALIVE: u32 = 234;
/// Manual Floodlight Control
pub const MSG_ID_FLOODLIGHT_MANUAL: u32 = 288;
/// Floodlight status report from the camera
pub const MSG_ID_FLOODLIGHT_STATUS_LIST: u32 = 291;
/// An empty password in legacy format
pub const EMPTY_LEGACY_PASSWORD: &str =
+49
View File
@@ -76,6 +76,12 @@ pub struct BcXml {
/// Sent to move the camera
#[yaserde(rename = "PtzControl")]
pub ptz_control: Option<PtzControl>,
/// Sent to manually control the floodlight
#[yaserde(rename = "FloodlightManual")]
pub floodlight_manual: Option<FloodlightManual>,
/// Received when the floodlight status is updated
#[yaserde(rename = "FloodlightStatusList")]
pub floodlight_status_list: Option<FloodlightStatusList>,
}
impl BcXml {
@@ -312,6 +318,49 @@ pub struct LedState {
pub light_state: String,
}
/// FloodlightStatus xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct FloodlightStatus {
/// XML Version
#[yaserde(attribute)]
pub version: String,
/// Channel ID of floodlight
#[yaserde(rename = "channel")]
pub channel_id: u8,
/// On or off
#[yaserde(rename = "status")]
pub status: u8,
}
/// FloodlightStatusList xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct FloodlightStatusList {
/// XML Version
#[yaserde(attribute)]
pub version: String,
/// List of events
#[yaserde(rename = "FloodlightStatusList")]
pub floodlight_status_list: Vec<FloodlightStatus>,
}
/// FloodlightManual xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct FloodlightManual {
/// XML Version
#[yaserde(attribute)]
pub version: String,
/// Channel ID of floodlight
#[yaserde(rename = "channelId")]
pub channel_id: u8,
/// On or off
#[yaserde(rename = "status")]
pub status: u8,
/// How long the manual control should apply for
pub duration: u16,
}
/// rfAlarmCfg xml
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
pub struct RfAlarmCfg {
+1
View File
@@ -11,6 +11,7 @@ use Md5Trunc::*;
mod connection;
mod errors;
mod floodlight_status;
mod ledstate;
mod login;
mod logout;
@@ -0,0 +1,110 @@
use super::{BcCamera, Error, Result, RX_TIMEOUT};
use crate::bc::{model::*, xml::*};
impl BcCamera {
/// Get the [FloodlightStatusList] xml which contains the Floodlist status of the camera
pub fn get_floodlight_status(&self) -> Result<FloodlightStatusList> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let sub_get = connection.subscribe(msg_num)?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_FLOODLIGHT_STATUS_LIST,
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)?;
let msg = sub_get.rx.recv_timeout(RX_TIMEOUT)?;
if msg.meta.response_code != 200 {
return Err(Error::CameraServiceUnavaliable);
}
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
floodlight_status_list: Some(floodlight_status_list),
..
})),
..
}) = msg.body
{
Ok(floodlight_status_list)
} else {
Err(Error::UnintelligibleReply {
reply: Box::new(msg),
why: "Expected FloodlightStatusList xml but it was not recieved",
})
}
}
/// Set the floodlight status using the [FloodlightManual] xml
pub fn set_floodlight_manual(&self, floodlight_manual: FloodlightManual) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let sub_set = connection.subscribe(msg_num)?;
let get = Bc {
meta: BcMeta {
msg_id: MSG_ID_FLOODLIGHT_MANUAL,
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 {
floodlight_manual: Some(floodlight_manual),
..Default::default()
})),
}),
};
sub_set.send(get)?;
let msg = sub_set.rx.recv_timeout(RX_TIMEOUT)?;
if let BcMeta {
response_code: 200, ..
} = msg.meta
{
Ok(())
} else {
Err(Error::UnintelligibleReply {
reply: Box::new(msg),
why: "The camera did not accept the Floodlight manual state",
})
}
}
/// Set the floodlight status for a duration
pub fn floodlight_light_set(&self, state: bool, duration: u16) -> Result<()> {
let mut floodlight_state = self.get_floodlight_status()?;
for floodlight in floodlight_state.floodlight_status_list.iter_mut() {
if floodlight.channel_id == self.channel_id {
self.set_floodlight_manual(FloodlightManual { version: "1".to_string(), channel_id: floodlight.channel_id, status: match state {
true => 1,
false => 0,
} , duration: duration })?;
}
}
Ok(())
}
}
+20
View File
@@ -15,6 +15,8 @@ pub(crate) enum Messages {
MotionStop,
MotionStart,
Reboot,
FloodlightOn,
FloodlightOff,
StatusLedOn,
StatusLedOff,
IRLedOn,
@@ -263,6 +265,24 @@ impl MessageHandler {
"OK".to_string()
}
}
Messages::FloodlightOn => {
if self.camera.floodlight_light_set(true, 180).is_err() {
error!("Failed to turn on the floodlight light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::FloodlightOff => {
if self.camera.floodlight_light_set(false, 180).is_err() {
error!("Failed to turn off the floodlight light");
self.abort();
"FAIL".to_string()
} else {
"OK".to_string()
}
}
Messages::StatusLedOn => {
if self.camera.led_light_set(true).is_err() {
error!("Failed to turn on the status light");
+16
View File
@@ -148,6 +148,22 @@ fn listen_on_camera(
while app.running(&format!("app: {}", cam_config.name)) {
if let Ok(msg) = mqtt.poll() {
match msg.as_ref() {
MqttReplyRef {
topic: "control/floodlight",
message: "on",
} => {
if event_cam.send_message(Messages::FloodlightOn).is_err() {
error!("Failed to set floodlight on");
}
}
MqttReplyRef {
topic: "control/floodlight",
message: "off",
} => {
if event_cam.send_message(Messages::FloodlightOff).is_err() {
error!("Failed to set floodlight off");
}
}
MqttReplyRef {
topic: "control/led",
message: "on",