Rework how the wakecommand works

This commit is contained in:
QuantumEntangledAndy committed 2023-10-19 09:41:10 +07:00
1 parent 7809f6db78
commit 93bf955a37
1 file changed
+5 -12
+5 -12
View File
@@ -969,22 +969,15 @@ async fn handle_mqtt_message(
let reply = match message.parse::<u64>() {
Ok(secs) => {
if let Ok(permit) = camera.permit().await {
let (tx, mut rx) = mpsc(1);
// By using a run_task we can delay the countdown until AFTER we are connected
let _ = camera.run_task(|_cam| {
let tx = tx.clone();
Box::pin(async move {
let _ = tx.try_send(()); // Camera online, start the countdown
AnyResult::Ok(())
})
});
// This task waits for the `run_task` to send the OK then starts the countdown
// to drop the permit
let camera = camera.clone();
tokio::task::spawn(async move {
// Wait for connection then start the countdown
let _ = rx.recv().await;
// By using a run_task we can delay the countdown until AFTER we are connected
let _ = camera
.run_task(|_cam| Box::pin(async move { AnyResult::Ok(()) }))
.await;
log::debug!("Wakeup counting down");
sleep(Duration::from_secs(secs * 60)).await;