From 17e8ab0f37b34b8a42fbf726e8dfe22223515b65 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 7 Aug 2023 19:03:06 +0700 Subject: [PATCH] Add gracefull shutdown to sub commands --- crates/core/src/bc_protocol.rs | 5 ----- src/cmdline.rs | 1 + src/main.rs | 4 ++++ src/mqtt/event_cam.rs | 4 ++++ src/pir/mod.rs | 4 ++++ src/ptz/mod.rs | 4 ++++ src/reboot/mod.rs | 3 +++ src/rtsp/states/connected.rs | 10 ++++++++++ src/statusled/mod.rs | 4 ++++ src/talk/mod.rs | 3 +++ src/utils.rs | 1 - 11 files changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index db6c118..2691c7b 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -85,8 +85,6 @@ pub struct BcCameraOpt { pub protocol: ConnectionProtocol, /// Discovery method to allow pub discovery: DiscoveryMethods, - /// Printing format for auxilaary data such as battery levels - pub aux_printing: PrintFormat, /// Credentials for login pub credentials: Credentials, } @@ -343,9 +341,6 @@ impl BcCamera { abilities: Default::default(), }; me.keepalive().await?; - if let Err(e) = me.monitor_battery(options.aux_printing).await { - warn!("Could not monitor battery: {:?}", e); - } Ok(me) } diff --git a/src/cmdline.rs b/src/cmdline.rs index 6e47674..56a1004 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -25,4 +25,5 @@ pub enum Command { Talk(super::talk::Opt), Mqtt(super::mqtt::Opt), Image(super::image::Opt), + Battery(super::battery::Opt), } diff --git a/src/main.rs b/src/main.rs index f20c6b4..0776ba1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ use log::*; use std::fs; use validator::Validate; +mod battery; mod cmdline; mod config; mod image; @@ -115,6 +116,9 @@ async fn main() -> Result<()> { Some(Command::Image(opts)) => { image::main(opts, config).await?; } + Some(Command::Battery(opts)) => { + battery::main(opts, config).await?; + } } Ok(()) diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 072bd04..c3095df 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -198,6 +198,10 @@ impl EventCamThread { .context("Failed to login to the camera")??; info!("{}: Connected and logged in", camera_config.name); + if let Err(e) = camera.monitor_battery(camera_config.print_format).await { + warn!("Could not monitor battery: {:?}", e); + } + self.tx.send(Messages::Login).await?; // Shararble cameras diff --git a/src/pir/mod.rs b/src/pir/mod.rs index 6a028de..84ecb8f 100644 --- a/src/pir/mod.rs +++ b/src/pir/mod.rs @@ -44,5 +44,9 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .expect("Should be UTF8"); println!("{}", pir_ser); } + + let _ = camera.logout().await; + camera.disconnect().await?; + Ok(()) } diff --git a/src/ptz/mod.rs b/src/ptz/mod.rs index 74188c9..77eb04a 100644 --- a/src/ptz/mod.rs +++ b/src/ptz/mod.rs @@ -90,5 +90,9 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .context("Unable to execute PTZ move command")?; } }; + + let _ = camera.logout().await; + camera.disconnect().await?; + Ok(()) } diff --git a/src/reboot/mod.rs b/src/reboot/mod.rs index 903aac5..f5f7ca3 100644 --- a/src/reboot/mod.rs +++ b/src/reboot/mod.rs @@ -29,5 +29,8 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .reboot() .await .context("Could not send reboot command to the camera")?; + + camera.disconnect().await?; + Ok(()) } diff --git a/src/rtsp/states/connected.rs b/src/rtsp/states/connected.rs index e20493c..427ef13 100644 --- a/src/rtsp/states/connected.rs +++ b/src/rtsp/states/connected.rs @@ -30,6 +30,16 @@ impl Camera { }; timeout(self.state.camera.login_with_maxenc(max_encryption)).await??; + + if let Err(e) = self + .state + .camera + .monitor_battery(self.shared.config.print_format) + .await + { + log::warn!("Could not monitor battery: {:?}", e); + } + Ok(Camera { shared: self.shared, state: LoggedIn { diff --git a/src/statusled/mod.rs b/src/statusled/mod.rs index b1f9b24..c2e264d 100644 --- a/src/statusled/mod.rs +++ b/src/statusled/mod.rs @@ -33,5 +33,9 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .led_light_set(opt.on) .await .context("Unable to set camera light state")?; + + let _ = camera.logout().await; + camera.disconnect().await?; + Ok(()) } diff --git a/src/talk/mod.rs b/src/talk/mod.rs index 65ec928..6cb5999 100644 --- a/src/talk/mod.rs +++ b/src/talk/mod.rs @@ -86,5 +86,8 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .await .context("Talk stream ended early")?; + let _ = camera.logout().await; + camera.disconnect().await?; + Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index ffbabaa..9cd2d4e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -93,7 +93,6 @@ impl AddressOrUid { uid: camera_config.camera_uid.clone(), protocol: ConnectionProtocol::TcpUdp, discovery: camera_config.discovery, - aux_printing: camera_config.print_format, credentials: Credentials { username: camera_config.username.clone(), password: camera_config.password.clone(),