Use connected over online for mqtt message
This commit is contained in:
5 files changed
+66
-14
No files matched your search
@@ -8,7 +8,13 @@ use tokio_util::sync::CancellationToken;
|
||||
use crate::{config::CameraConfig, utils::connect_and_login, Result};
|
||||
use neolink_core::bc_protocol::BcCamera;
|
||||
|
||||
pub(crate) enum NeoCamThreadState {
|
||||
Connected,
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
pub(crate) struct NeoCamThread {
|
||||
state: WatchReceiver<NeoCamThreadState>,
|
||||
config: WatchReceiver<CameraConfig>,
|
||||
cancel: CancellationToken,
|
||||
camera_watch: WatchSender<Weak<BcCamera>>,
|
||||
@@ -16,11 +22,13 @@ pub(crate) struct NeoCamThread {
|
||||
|
||||
impl NeoCamThread {
|
||||
pub(crate) async fn new(
|
||||
watch_state_rx: WatchReceiver<NeoCamThreadState>,
|
||||
watch_config_rx: WatchReceiver<CameraConfig>,
|
||||
camera_watch_tx: WatchSender<Weak<BcCamera>>,
|
||||
cancel: CancellationToken,
|
||||
) -> Self {
|
||||
Self {
|
||||
state: watch_state_rx,
|
||||
config: watch_config_rx,
|
||||
cancel,
|
||||
camera_watch: camera_watch_tx,
|
||||
@@ -77,15 +85,23 @@ impl NeoCamThread {
|
||||
let mut backoff = MIN_BACKOFF;
|
||||
|
||||
loop {
|
||||
self.state
|
||||
.clone()
|
||||
.wait_for(|state| matches!(state, NeoCamThreadState::Connected))
|
||||
.await?;
|
||||
let mut config_rec = self.config.clone();
|
||||
|
||||
let config = config_rec.borrow_and_update().clone();
|
||||
let now = Instant::now();
|
||||
|
||||
let mut state = self.state.clone();
|
||||
let res = tokio::select! {
|
||||
Ok(_) = config_rec.changed() => {
|
||||
None
|
||||
}
|
||||
Ok(_) = state.wait_for(|state| matches!(state, NeoCamThreadState::Disconnected)) => {
|
||||
None
|
||||
}
|
||||
v = self.run_camera(&config) => {
|
||||
Some(v)
|
||||
}
|
||||
|
||||
@@ -171,4 +171,20 @@ impl NeoInstance {
|
||||
pub(crate) fn camera(&self) -> WatchReceiver<Weak<BcCamera>> {
|
||||
self.camera_watch.clone()
|
||||
}
|
||||
|
||||
pub(crate) async fn connect(&self) -> Result<()> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::Connect(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
pub(crate) async fn disconnect(&self) -> Result<()> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::Disconnect(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
}
|
||||
+30
-10
@@ -18,8 +18,8 @@ use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{
|
||||
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoInstance,
|
||||
StreamInstance, StreamRequest,
|
||||
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoCamThreadState,
|
||||
NeoInstance, StreamInstance, StreamRequest,
|
||||
};
|
||||
use crate::{config::CameraConfig, AnyResult, Result};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
@@ -34,6 +34,8 @@ pub(crate) enum NeoCamCommand {
|
||||
Streams(OneshotSender<Vec<StreamInstance>>),
|
||||
Motion(OneshotSender<WatchReceiver<MdState>>),
|
||||
Config(OneshotSender<WatchReceiver<CameraConfig>>),
|
||||
Disconnect(OneshotSender<()>),
|
||||
Connect(OneshotSender<()>),
|
||||
}
|
||||
/// The underlying camera binding
|
||||
pub(crate) struct NeoCam {
|
||||
@@ -51,6 +53,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 (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
|
||||
|
||||
let set = JoinSet::new();
|
||||
let mut me = Self {
|
||||
@@ -133,6 +136,20 @@ impl NeoCam {
|
||||
},
|
||||
NeoCamCommand::Config(sender) => {
|
||||
let _ = sender.send(thread_watch_config_rx.clone());
|
||||
},
|
||||
NeoCamCommand::Connect(sender) => {
|
||||
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
|
||||
state_tx.send_replace(NeoCamThreadState::Connected);
|
||||
log::info!("{}: Connect On Request", thread_watch_config_rx.borrow().name);
|
||||
}
|
||||
let _ = sender.send(());
|
||||
}
|
||||
NeoCamCommand::Disconnect(sender) => {
|
||||
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
|
||||
state_tx.send_replace(NeoCamThreadState::Disconnected);
|
||||
log::info!("{}: Disconnect On Request", thread_watch_config_rx.borrow().name);
|
||||
}
|
||||
let _ = sender.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,8 +172,13 @@ impl NeoCam {
|
||||
//
|
||||
// It will keep it logged and reconnect
|
||||
let thread_watch_config_rx = watch_config_rx.clone();
|
||||
let mut cam_thread =
|
||||
NeoCamThread::new(thread_watch_config_rx, camera_watch_tx, me.cancel.clone()).await;
|
||||
let mut cam_thread = NeoCamThread::new(
|
||||
state_rx,
|
||||
thread_watch_config_rx,
|
||||
camera_watch_tx,
|
||||
me.cancel.clone(),
|
||||
)
|
||||
.await;
|
||||
me.set.spawn(async move { cam_thread.run().await });
|
||||
|
||||
// This thread maintains the streams
|
||||
@@ -216,6 +238,9 @@ impl NeoCam {
|
||||
}
|
||||
});
|
||||
|
||||
// This thread will apply battery saving by disconnecting the camera on certain events (like no motion)
|
||||
// and reconnecting on other events like push notifications
|
||||
|
||||
Ok(me)
|
||||
}
|
||||
|
||||
@@ -227,9 +252,8 @@ impl NeoCam {
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn update_config(&self, config: CameraConfig) -> Result<()> {
|
||||
self.config_watch.send(config)?;
|
||||
self.config_watch.send_replace(config);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -238,10 +262,6 @@ impl NeoCam {
|
||||
self.set.shutdown().await;
|
||||
AnyResult::Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_config_watch(&self) -> &WatchSender<CameraConfig> {
|
||||
&self.config_watch
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for NeoCam {
|
||||
|
||||
@@ -95,7 +95,7 @@ impl NeoReactor {
|
||||
instances.retain(|name, _| names.contains_key(name));
|
||||
for (name, instance) in instances.iter() {
|
||||
if let Some(conf) = names.remove(name) {
|
||||
let _ = instance.get_config_watch().send_replace(conf);
|
||||
let _ = instance.update_config(conf).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -333,13 +333,13 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
format!("{}: Online Watch Dropped", camera_name)
|
||||
})?;
|
||||
log::trace!("Publish online");
|
||||
mqtt_watch.send_message("status", "online", true).await.with_context(|| {
|
||||
format!("{}: Failed to publish Online", camera_name)
|
||||
mqtt_watch.send_message("status", "connected", true).await.with_context(|| {
|
||||
format!("{}: Failed to publish connected", camera_name)
|
||||
})?;
|
||||
camera_watch.wait_for(|cam| cam.upgrade().is_none()).await.with_context(|| {
|
||||
format!("{}: Disconnect Watch Dropped", camera_name)
|
||||
})?;
|
||||
mqtt_watch.send_message("status", "disconnected4", true).await.with_context(|| {
|
||||
mqtt_watch.send_message("status", "disconnected", true).await.with_context(|| {
|
||||
format!("{}: Failed to publish disconnected", camera_name)
|
||||
})?;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user