diff --git a/src/common/instance.rs b/src/common/instance.rs index 9339403..ea697c1 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -261,10 +261,16 @@ impl NeoInstance { Ok(instance_rx.await?) } - pub(crate) async fn push_notifications(&self) -> Result>> { - let uid = self - .run_task(|cam| Box::pin(async move { Ok(cam.uid().await?) })) + pub(crate) async fn uid(&self) -> Result { + let (reply_tx, reply_rx) = oneshot(); + self.camera_control + .send(NeoCamCommand::GetUid(reply_tx)) .await?; + Ok(reply_rx.await?) + } + + pub(crate) async fn push_notifications(&self) -> Result>> { + let uid = self.uid().await?; let (instance_tx, instance_rx) = oneshot(); self.camera_control .send(NeoCamCommand::PushNoti(instance_tx)) diff --git a/src/common/neocam.rs b/src/common/neocam.rs index f33220f..85fe80b 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -41,6 +41,7 @@ pub(crate) enum NeoCamCommand { State(OneshotSender), GetPermit(OneshotSender), PushNoti(OneshotSender>>), + GetUid(OneshotSender), } /// The underlying camera binding pub(crate) struct NeoCam { @@ -62,6 +63,7 @@ impl NeoCam { let (stream_request_tx, stream_request_rx) = mpsc(100); let (md_request_tx, md_request_rx) = mpsc(100); let (state_tx, state_rx) = watch(NeoCamThreadState::Connected); + let (uid_tx, uid_rx) = watch(config.camera_uid.clone()); let set = JoinSet::new(); let users = UseCounter::new().await; @@ -175,6 +177,14 @@ impl NeoCam { } ).await?; }, + NeoCamCommand::GetUid(sender) => { + let mut uid_rx = uid_rx.clone(); + tokio::task::spawn(async move { + let uid = uid_rx.wait_for(|v| v.is_some()).await?.clone().unwrap(); + let _ = sender.send(uid); + AnyResult::Ok(()) + }); + }, } } log::debug!("Control thread Senders dropped"); @@ -275,6 +285,26 @@ impl NeoCam { } }); + // This thread will update the UID by asking the camera. + // We cache this in the uid_rx + let uid_instance = instance.clone(); + let uid_cancel = me.cancel.clone(); + me.set.spawn(async move { + tokio::select! { + _ = uid_cancel.cancelled() => { + AnyResult::Ok(()) + }, + v = async { + let uid = uid_instance.run_task(|cam| Box::pin(async move { + let uid = cam.uid().await?; + Ok(uid) + })).await?; + uid_tx.send_replace(Some(uid)); + AnyResult::Ok(()) + } => v, + } + }); + // Handles push notifications let pn_root_instance = instance.subscribe().await?; let pn_cancel = me.cancel.clone(); diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 94d2e61..fe57d4f 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -565,7 +565,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R } => v, // Handle the floodlight task activation v = async { - let flt_status = camera_floodlight_tasks.run_task(|cam| Box::pin(async move { + let flt_status = camera_floodlight_tasks.run_passive_task(|cam| Box::pin(async move { Ok(cam.is_flightlight_tasks_enabled().await?) })).await; if flt_status.is_err() {