Handle restart of push notification client

This commit is contained in:
QuantumEntangledAndy committed 2023-09-26 18:30:14 +07:00
1 parent f4b852f1e6
commit 3f95aafe99
3 files changed
+121 -79

No files matched your search

+101 -68
View File
@@ -4,7 +4,7 @@
//! Shared stream BC delivery
//! Common restart code
//! Clonable interface to share amongst threadsanyhow::anyhow;
use futures::stream::StreamExt;
use futures::{stream::StreamExt, TryFutureExt};
use std::sync::Weak;
use tokio::{
sync::{
@@ -58,7 +58,7 @@ impl NeoCam {
let (camera_watch_tx, camera_watch_rx) = watch(Weak::new());
let (stream_request_tx, stream_request_rx) = mpsc(100);
let (md_request_tx, md_request_rx) = mpsc(100);
let (pn_request_tx, pn_request_rx) = mpsc(100);
let (pn_request_tx, mut pn_request_rx) = mpsc(100);
let (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
let set = JoinSet::new();
@@ -259,40 +259,54 @@ impl NeoCam {
}
});
if config.push_notifications {
// This thread handles the push notfications
let pn_instance = instance.subscribe().await?;
let mut push_notifier = PushNotiThread::new(pn_request_rx, pn_instance).await?;
let pn_cancel = me.cancel.clone();
me.set.spawn(async move {
tokio::select! {
_ = pn_cancel.cancelled() => {
AnyResult::Ok(())
},
v = push_notifier.run() => v,
}
});
// Handles push notifications
let pn_root_instance = instance.subscribe().await?;
let pn_cancel = me.cancel.clone();
me.set.spawn(async move {
tokio::select!{
_ = pn_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let mut config_rx = pn_root_instance.config().await?;
loop {
// Wait for the green light
config_rx.wait_for(|config| config.push_notifications).await?;
let pn_instance = pn_root_instance.subscribe().await?;
let mut push_notifier = PushNotiThread::new(pn_instance).await?;
// Push notification permits
let pn_permit_instance = instance.subscribe().await?;
let pn_permit_cancel = me.cancel.clone();
me.set.spawn(async move {
tokio::select! {
_ = pn_permit_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let mut prev_noti = None;
let mut pn = pn_permit_instance.push_notifications().await?;
loop{
prev_noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.map(|noti| noti.clone())?;
let _permit = pn_permit_instance.permit().await?;
sleep(Duration::from_secs(30)).await; // Push notification will wake us up for 30s
let pn_permit_instance = pn_root_instance.subscribe().await?;
let r = tokio::select! {
// This thread handles the push notfications
v = async {
while push_notifier.run(&mut pn_request_rx).await.is_err() {}
AnyResult::Ok(())
} => v,
// Push notification permits
v = async {
let mut prev_noti = None;
let mut pn = pn_permit_instance.push_notifications().await?;
loop{
prev_noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.map(|noti| noti.clone())?;
let _permit = pn_permit_instance.permit().await?;
sleep(Duration::from_secs(30)).await; // Push notification will wake us up for 30s
}
} => v,
// Continue loop on Red light
v = config_rx.wait_for(|config| !config.push_notifications).map_ok(|_| ()) => {
v?;
AnyResult::Ok(())
},
};
if r.is_err() {
log::debug!("Push notifications stopped: {:?}", r);
break r;
}
} => v,
}
});
}
}?;
AnyResult::Ok(())
} => v,
}
});
// MD permits
let md_permit_instance = instance.subscribe().await?;
@@ -320,41 +334,60 @@ impl NeoCam {
}
});
if config.idle_disconnect {
// This thread will apply battery saving by disconnecting the camera when there are no
// active permits.
//
// Permits are created when a camera runs a user requested task, or when motion or push
// notifications are observed
let connect_instance = instance.subscribe().await?;
let connect_cancel = me.cancel.clone();
let connect_name = config.name.clone();
me.set.spawn(async move {
tokio::select! {
_ = connect_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let mut permit = connect_instance.permit().await?;
permit.deactivate().await?; // Watching only from here
loop {
permit.aquired_users().await?;
log::debug!("{connect_name}: InUse");
connect_instance.connect().await?;
permit.dropped_users().await?;
log::debug!("{connect_name}: Idle Wait");
// Wait 30s or if we hit another use then go back and wait again
tokio::select! {
_ = sleep(Duration::from_secs(30)) => {},
_ = permit.aquired_users() => continue,
};
log::debug!("{connect_name}: Idle");
connect_instance.disconnect().await?;
// This thread will apply battery saving by disconnecting the camera when there are no
// active permits.
//
// Permits are created when a camera runs a user requested task, or when motion or push
// notifications are observed
let connect_instance = instance.subscribe().await?;
let connect_cancel = me.cancel.clone();
let connect_name = config.name.clone();
me.set.spawn(async move {
tokio::select!{
_ = connect_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let mut config_rx = connect_instance.config().await?;
loop {
// Wait for the green light
config_rx.wait_for(|config| config.idle_disconnect).await?;
let r = tokio::select!{
// Wait for red light
v = config_rx.wait_for(|config| !config.idle_disconnect).map_ok(|_| ()) => {
v?;
connect_instance.connect().await?; // Ensure we are online now that we are not idle_disconnect
AnyResult::Ok(())
}
// Handle disconnects when no active permits
v = async {
let mut permit = connect_instance.permit().await?;
permit.deactivate().await?; // Watching only from here
loop {
permit.aquired_users().await?;
log::debug!("{connect_name}: InUse");
connect_instance.connect().await?;
permit.dropped_users().await?;
log::debug!("{connect_name}: Idle Wait");
// Wait 30s or if we hit another use then go back and wait again
tokio::select! {
_ = sleep(Duration::from_secs(30)) => {},
_ = permit.aquired_users() => continue,
};
log::debug!("{connect_name}: Idle");
connect_instance.disconnect().await?;
}
} => v,
};
if r.is_err() {
break r;
}
} => v,
}
});
}
}?;
AnyResult::Ok(())
} => v,
}
});
Ok(me)
}
+16 -9
View File
@@ -16,7 +16,6 @@ use crate::AnyResult;
pub(crate) struct PushNotiThread {
pn_watcher: Arc<WatchSender<Option<PushNoti>>>,
pn_request_rx: MpscReceiver<PnRequest>,
instance: NeoInstance,
}
@@ -34,20 +33,19 @@ pub(crate) enum PnRequest {
}
impl PushNotiThread {
pub(crate) async fn new(
pn_request_rx: MpscReceiver<PnRequest>,
instance: NeoInstance,
) -> AnyResult<Self> {
pub(crate) async fn new(instance: NeoInstance) -> AnyResult<Self> {
let (pn_watcher, _) = watch(None);
Ok(PushNotiThread {
pn_watcher: Arc::new(pn_watcher),
pn_request_rx,
instance,
})
}
pub(crate) async fn run(&mut self) -> AnyResult<()> {
pub(crate) async fn run(
&mut self,
pn_request_rx: &mut MpscReceiver<PnRequest>,
) -> AnyResult<()> {
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
@@ -55,6 +53,7 @@ impl PushNotiThread {
d.push("./neolink_token.toml");
d
});
log::debug!("Push notification details are saved to {:?}", token_path);
let registration = if let Some(Ok(Ok(registration))) =
token_path.as_ref().map(|token_path| {
@@ -111,11 +110,19 @@ impl PushNotiThread {
);
tokio::select! {
v = async {
listener.connect().await?;
let r = listener.connect().await;
if r.is_err() {
// Wipe data so next call is a new token
token_path.map(|token_path|
fs::write(token_path, "")
);
log::debug!("Error on push notification listener: {:?}. Clearing token", r);
}
r?;
AnyResult::Ok(())
} => v,
v = async {
while let Some(msg) = self.pn_request_rx.recv().await {
while let Some(msg) = pn_request_rx.recv().await {
match msg {
PnRequest::Get{sender} => {
let _ = sender.send(self.pn_watcher.subscribe());
+4 -2
View File
@@ -44,7 +44,7 @@ impl Mqtt {
let thread_outgoing_tx = outgoing_tx.clone();
set.spawn(async move {
let mut mqtt_config = thread_config.borrow().mqtt.clone();
loop {
let r = loop {
break tokio::select! {
_ = thread_cancel.cancelled() => AnyResult::Ok(()),
v = thread_config.wait_for(|config| config.mqtt != mqtt_config).map(|res| res.map(|r| r.clone())) =>
@@ -70,7 +70,9 @@ impl Mqtt {
v
},
};
}
};
log::debug!("MQTT thread stopped: {:?}", r);
r
});
Self {