diff --git a/src/battery/mod.rs b/src/battery/mod.rs index e37fa1f..1e76326 100644 --- a/src/battery/mod.rs +++ b/src/battery/mod.rs @@ -14,20 +14,28 @@ use anyhow::{Context, Result}; mod cmdline; +use crate::common::neocam::NeoReactor; + use super::config::Config; -use crate::utils::find_and_connect; pub(crate) use cmdline::Opt; /// Entry point for the battery subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera = find_and_connect(&config, &opt.camera).await?; +pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { + let config = config.get_camera_config(&opt.camera)?; + let camera = reactor.get_or_insert(config.clone()).await?; let state = camera - .battery_info() - .await - .context("Unable to get camera Battery state")?; + .run_task(|cam| { + Box::pin(async move { + cam.battery_info() + .await + .context("Unable to get camera Battery state") + }) + }) + .await?; + let ser = String::from_utf8( yaserde::ser::serialize_with_writer(&state, vec![], &Default::default()) .expect("Should Ser the struct"), @@ -35,8 +43,7 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { .expect("Should be UTF8"); println!("{}", ser); - let _ = camera.logout().await; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) } diff --git a/src/common/neocam.rs b/src/common/neocam.rs index 51e5235..b84958c 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -291,7 +291,8 @@ impl NeoInstance { enum NeoReactorCommand { HangUp, Get(String, OneshotSender>), - GetOrInsert(String, OneshotSender>, CameraConfig), + GetOrInsert(CameraConfig, OneshotSender>), + UpdateOrInsert(CameraConfig, OneshotSender>), } struct NeoReactorData { @@ -345,7 +346,8 @@ impl NeoReactor { .await; let _ = sender.send(new); } - NeoReactorCommand::GetOrInsert(name, sender, config) => { + NeoReactorCommand::GetOrInsert(config, sender) => { + let name = config.name.clone(); let new = match instances.entry(name) { Entry::Occupied(occ) => occ.get().instance.subscribe().await, Entry::Vacant(vac) => { @@ -360,6 +362,26 @@ impl NeoReactor { } }; let _ = sender.send(new); + }, + NeoReactorCommand::UpdateOrInsert(config, sender) => { + let name = config.name.clone(); + let new = match instances.entry(name) { + Entry::Occupied(occ) => { + occ.get().config_sender.send(config)?; + occ.get().instance.subscribe().await + }, + Entry::Vacant(vac) => { + let (config_sender, instance) = NeoCam::init(config).await?; + vac.insert(NeoReactorData { + instance, + config_sender, + }) + .instance + .subscribe() + .await + } + }; + let _ = sender.send(new); } } } @@ -372,6 +394,7 @@ impl NeoReactor { } #[allow(dead_code)] + /// Get camera by name but do not create pub(crate) async fn get(&self, name: &str) -> Result { let (sender_tx, sender_rx) = oneshot(); self.commander @@ -381,18 +404,22 @@ impl NeoReactor { sender_rx.await? } - pub(crate) async fn get_or_insert( - &self, - name: &str, - config: CameraConfig, - ) -> Result { + /// Get or create a camera + pub(crate) async fn get_or_insert(&self, config: CameraConfig) -> Result { let (sender_tx, sender_rx) = oneshot(); self.commander - .send(NeoReactorCommand::GetOrInsert( - name.to_string(), - sender_tx, - config, - )) + .send(NeoReactorCommand::GetOrInsert(config, sender_tx)) + .await?; + + sender_rx.await? + } + + #[allow(dead_code)] + /// Update a camera to a new config or create a camera + pub(crate) async fn update_or_insert(&self, config: CameraConfig) -> Result { + let (sender_tx, sender_rx) = oneshot(); + self.commander + .send(NeoReactorCommand::UpdateOrInsert(config, sender_tx)) .await?; sender_rx.await? diff --git a/src/main.rs b/src/main.rs index 7e579ac..29b7734 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,10 +100,10 @@ async fn main() -> Result<()> { rtsp::main(opts, config).await?; } Some(Command::StatusLight(opts)) => { - statusled::main(opts, config).await?; + statusled::main(opts, config, neo_reactor.clone()).await?; } Some(Command::Reboot(opts)) => { - reboot::main(opts, config).await?; + reboot::main(opts, config, neo_reactor.clone()).await?; } Some(Command::Pir(opts)) => { pir::main(opts, config, neo_reactor.clone()).await?; @@ -121,7 +121,7 @@ async fn main() -> Result<()> { image::main(opts, config).await?; } Some(Command::Battery(opts)) => { - battery::main(opts, config).await?; + battery::main(opts, config, neo_reactor.clone()).await?; } } diff --git a/src/pir/mod.rs b/src/pir/mod.rs index 3a2b2d4..5ff7ce8 100644 --- a/src/pir/mod.rs +++ b/src/pir/mod.rs @@ -26,7 +26,7 @@ pub(crate) use cmdline::Opt; /// Opt is the command line options pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { let config = config.get_camera_config(&opt.camera)?; - let camera = reactor.get_or_insert(&opt.camera, config.clone()).await?; + let camera = reactor.get_or_insert(config.clone()).await?; if let Some(on) = opt.on { camera diff --git a/src/reboot/mod.rs b/src/reboot/mod.rs index dc4a358..26e2c08 100644 --- a/src/reboot/mod.rs +++ b/src/reboot/mod.rs @@ -16,21 +16,28 @@ use anyhow::{Context, Result}; mod cmdline; use super::config::Config; -use crate::utils::find_and_connect; +use crate::common::neocam::NeoReactor; pub(crate) use cmdline::Opt; /// Entry point for the reboot subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera = find_and_connect(&config, &opt.camera).await?; +pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { + let config = config.get_camera_config(&opt.camera)?; + let camera = reactor.get_or_insert(config.clone()).await?; camera - .reboot() - .await - .context("Could not send reboot command to the camera")?; + .run_task(|camera| { + Box::pin(async move { + camera + .reboot() + .await + .context("Could not send reboot command to the camera") + }) + }) + .await?; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) } diff --git a/src/statusled/mod.rs b/src/statusled/mod.rs index 75aa817..b32a9a4 100644 --- a/src/statusled/mod.rs +++ b/src/statusled/mod.rs @@ -20,22 +20,29 @@ use anyhow::{Context, Result}; mod cmdline; use super::config::Config; -use crate::utils::find_and_connect; +use crate::common::neocam::NeoReactor; pub(crate) use cmdline::Opt; /// Entry point for the ledstatus subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera = find_and_connect(&config, &opt.camera).await?; +pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { + let config = config.get_camera_config(&opt.camera)?; + let camera = reactor.get_or_insert(config.clone()).await?; + let on = opt.on; camera - .led_light_set(opt.on) - .await - .context("Unable to set camera light state")?; + .run_task(|camera| { + Box::pin(async move { + camera + .led_light_set(on) + .await + .context("Unable to set camera light state") + }) + }) + .await?; - let _ = camera.logout().await; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) }