From c6505952d5a6e64e86c7ac32b1a6edc6db66c80c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 8 Jun 2023 16:42:20 +0700 Subject: [PATCH] Add config options for mqtt threads --- README.md | 23 +++++++++++++++++++++++ src/config.rs | 15 +++++++++++++++ src/mqtt/event_cam.rs | 10 +++++----- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c3ea4de..89ab8a8 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ Status Messages: - `/status/motion` Contains the motion detection alarm status. `on` for motion and `off` for still - `/status/ptz/preset` Sent in reply to a `/query/ptz/preset` an XML encoded version of the PTZ presets +- `/status/preview` a base64 encoded camera image updated every 0.5s Query Messages: @@ -216,6 +217,28 @@ Query Messages: - `/query/pir` Request that the camera reports its pir status - `/query/ptz/preset` Request that the camera reports its PTZ presets +### MQTT Disable Features + +Certain features like preview and motion detection may not be desired +you can disable them by them with the following config options. +Disabling these may help to conserve battery + +```toml +enable_motion = false # motion detection + # (limited battery drain since it + # is a passive listening connection) + +enable_pings = false # keep alive pings that keep the camera connected + +enable_light = false # flood lights only avaliable on some camera + # (limited battery drain since it + # is a passive listening connection) + +enable_battery = false # battery updates in `/status/battery_level` + +enable_preview = false # preview image in `/status/preview` +``` + #### MQTT Discovery [MQTT Discovery](https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery) is partially supported. diff --git a/src/config.rs b/src/config.rs index 87de87a..2b0bf12 100644 --- a/src/config.rs +++ b/src/config.rs @@ -148,6 +148,17 @@ pub(crate) struct MqttConfig { #[serde(default)] pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>, + + #[serde(default = "default_true")] + pub(crate) enable_motion: bool, + #[serde(default = "default_true")] + pub(crate) enable_pings: bool, + #[serde(default = "default_true")] + pub(crate) enable_light: bool, + #[serde(default = "default_true")] + pub(crate) enable_battery: bool, + #[serde(default = "default_true")] + pub(crate) enable_preview: bool, #[serde(default)] pub(crate) discovery: Option, @@ -170,6 +181,10 @@ fn validate_mqtt_config(config: &MqttConfig) -> Result<(), ValidationError> { } } +const fn default_true() -> bool { + true +} + fn default_mqtt() -> Option { None } diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 15c28ad..197db83 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -237,7 +237,7 @@ impl EventCamThread { val = async { info!("{}: Listening to Camera Motion", camera_config.name); motion_thread.run().await - } => { + }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_motion => { if let Err(e) = val { error!("Motion thread aborted: {:?}", e); Err(e) @@ -249,7 +249,7 @@ impl EventCamThread { val = async { debug!("{}: Starting Pings", camera_config.name); keepalive_thread.run().await - } => { + }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_pings => { if let Err(e) = val { debug!("Ping thread aborted: {:?}", e); Err(e) @@ -261,7 +261,7 @@ impl EventCamThread { val = async { info!("{}: Listening to FloodLight Status", camera_config.name); flight_thread.run().await - } => { + }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_light => { if let Err(e) = val { error!("FloodLight thread aborted: {:?}", e); Err(e) @@ -273,7 +273,7 @@ impl EventCamThread { val = async { info!("{}: Updating Preview", camera_config.name); snap_thread.run().await - } => { + }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_preview => { if let Err(e) = val { error!("Snap thread aborted: {:?}", e); Err(e) @@ -285,7 +285,7 @@ impl EventCamThread { val = async { info!("{}: Updating Battery Level", camera_config.name); battery_thread.run().await - } => { + }, if camera_config.mqtt.as_ref().expect("Should have an mqtt config at this point").enable_battery => { if let Err(e) = val { error!("Battery thread aborted: {:?}", e); Err(e)