Push notifications and disconnect on idle

This commit is contained in:
QuantumEntangledAndy committed 2023-09-26 14:44:59 +07:00
1 parent 061cba4f6d
commit 0f28154fdb
18 files changed
+474 -56

No files matched your search

Generated
+175 -17
View File
@@ -225,6 +225,15 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.13.0"
@@ -511,7 +520,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn 2.0.15",
"syn 2.0.28",
]
[[package]]
@@ -522,7 +531,7 @@ checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a"
dependencies = [
"darling_core",
"quote",
"syn 2.0.15",
"syn 2.0.28",
]
[[package]]
@@ -541,6 +550,59 @@ name = "deranged"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7684a49fb1af197853ef7b2ee694bc1f5b4179556f1e5710e1760c5db6f5e929"
dependencies = [
"serde",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys",
]
[[package]]
name = "ece"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5463ffecc0677adcd786c4481f73b215714d4757edf2eb37a573c03d00459"
dependencies = [
"base64 0.13.1",
"byteorder",
"hex",
"hkdf",
"lazy_static",
"once_cell",
"openssl",
"serde",
"sha2",
"thiserror",
]
[[package]]
name = "either"
@@ -623,7 +685,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51e1e9327cb6abc9da64951727a3cf534ae7101e529382dac7f5d350bccdba42"
dependencies = [
"base64 0.21.0",
"base64 0.21.2",
"ece",
"log",
"prost",
@@ -632,7 +694,7 @@ dependencies = [
"serde",
"serde_with",
"tokio",
"tokio-rustls",
"tokio-rustls 0.23.4",
"uuid",
"webpki-roots",
]
@@ -1191,6 +1253,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hex-string"
version = "0.1.0"
@@ -1357,6 +1425,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
"serde",
]
[[package]]
@@ -1384,6 +1453,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "ipnet"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
[[package]]
name = "is-terminal"
version = "0.4.9"
@@ -1608,7 +1683,9 @@ dependencies = [
"clap",
"console-subscriber",
"crossbeam-channel",
"dirs",
"env_logger",
"fcm-push-listener",
"futures",
"gstreamer",
"gstreamer-app",
@@ -1617,6 +1694,7 @@ dependencies = [
"heck",
"lazy_static",
"log",
"md5",
"neolink_core",
"regex",
"rumqttc",
@@ -1759,7 +1837,7 @@ version = "0.10.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01b8574602df80f7b85fdfc5392fa884a4e3b3f4f35402c070ab34c3d3f78d56"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"cfg-if",
"foreign-types",
"libc",
@@ -1776,7 +1854,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.15",
"syn 2.0.28",
]
[[package]]
@@ -1797,6 +1875,12 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "option-operations"
version = "0.5.0"
@@ -1824,7 +1908,7 @@ checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.3.5",
"smallvec",
"windows-targets",
]
@@ -1848,7 +1932,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4"
dependencies = [
"fixedbitset",
"indexmap",
"indexmap 1.9.3",
]
[[package]]
@@ -1972,7 +2056,7 @@ checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270"
dependencies = [
"bytes",
"heck",
"itertools",
"itertools 0.10.5",
"lazy_static",
"log",
"multimap",
@@ -2066,6 +2150,15 @@ dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.3.5"
@@ -2075,6 +2168,17 @@ dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall 0.2.16",
"thiserror",
]
[[package]]
name = "regex"
version = "1.9.1"
@@ -2151,7 +2255,7 @@ version = "0.11.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cde824a14b7c14f85caff81225f411faacc04a2013f41670f41443742b1c1c55"
dependencies = [
"base64 0.21.0",
"base64 0.21.2",
"bytes",
"encoding_rs",
"futures-core",
@@ -2212,7 +2316,7 @@ dependencies = [
"rustls-webpki 0.100.1",
"thiserror",
"tokio",
"tokio-rustls",
"tokio-rustls 0.24.1",
]
[[package]]
@@ -2234,6 +2338,18 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "rustls"
version = "0.20.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b80e3dec595989ea8510028f30c408a4630db12c9cbb8de34203b89d6577e99"
dependencies = [
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls"
version = "0.21.6"
@@ -2408,7 +2524,7 @@ dependencies = [
"base64 0.13.1",
"chrono",
"hex",
"indexmap",
"indexmap 1.9.3",
"serde",
"serde_json",
"serde_with_macros",
@@ -2424,7 +2540,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.15",
"syn 2.0.28",
]
[[package]]
@@ -2608,7 +2724,7 @@ checksum = "5486094ee78b2e5038a6382ed7645bc084dc2ec433426ca4c3cb61e2007b8998"
dependencies = [
"cfg-if",
"fastrand",
"redox_syscall",
"redox_syscall 0.3.5",
"rustix",
"windows-sys",
]
@@ -2690,6 +2806,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fdd63d58b18d663fbdf70e049f00a22c8e42be082203be7f26589213cd75ea"
dependencies = [
"deranged",
"itoa",
"libc",
"num_threads",
"serde",
@@ -2779,13 +2896,24 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59"
dependencies = [
"rustls 0.20.9",
"tokio",
"webpki",
]
[[package]]
name = "tokio-rustls"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081"
dependencies = [
"rustls",
"rustls 0.21.6",
"tokio",
]
@@ -3060,7 +3188,7 @@ checksum = "3f67b459f42af2e6e1ee213cb9da4dbd022d3320788c3fb3e1b893093f1e45da"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.15",
"syn 2.0.28",
]
[[package]]
@@ -3219,6 +3347,36 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "webpki"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0e74f82d49d545ad128049b7e88f6576df2da6b02e9ce565c6f533be576957e"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "webpki-roots"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
dependencies = [
"webpki",
]
[[package]]
name = "which"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269"
dependencies = [
"either",
"libc",
"once_cell",
]
[[package]]
name = "winapi"
version = "0.2.8"
@@ -3262,7 +3420,7 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
"windows-targets 0.48.0",
"windows-targets",
]
[[package]]
+3
View File
@@ -18,7 +18,9 @@ byte-slice-cast = "1.2.2"
clap = { version = "4.2.2", features = ["derive", "cargo"] }
console-subscriber = "0.1.8"
crossbeam-channel = "0.5.8"
dirs = "5.0.1"
env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
futures = "0.3.28"
gstreamer = "0.21.0"
gstreamer-app = { version = "0.21.0", features = ["v1_16"] }
@@ -27,6 +29,7 @@ gstreamer-rtsp-server = { version = "0.21.0", features = ["v1_16"] }
heck = "0.4.1"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
md5 = "0.7.0"
neolink_core = { path = "crates/core", version = "0.6.2" }
regex = "1.7.3"
rumqttc = "0.22.0"
+2 -4
View File
@@ -41,16 +41,14 @@ pub const MSG_ID_GET_GENERAL: u32 = 104;
pub const MSG_ID_SET_GENERAL: u32 = 105;
/// Snapshot to get a jpeg image
pub const MSG_ID_SNAP: u32 = 109;
/// Used to pass the token and client ID for push notifications
pub const MSG_ID_PUSH_INFO: u32 = 124;
/// StreamInfoList messages have this ID
pub const MSG_ID_STREAM_INFO_LIST: u32 = 146;
/// Used to get the abilities of a user
pub const MSG_ID_ABILITY_INFO: u32 = 151;
/// Get the available PTZ position presets
pub const MSG_ID_GET_PTZ_PRESET: u32 = 190;
/// Setting general system info (clock mostly) messages have this ID
pub const MSG_ID_SET_GENERAL: u32 = 105;
/// Used to pass the token and client ID for push notifications
pub const MSG_ID_PUSH_INFO: u32 = 124;
/// Will send the talk config for talk back data to follow this msg
pub const MSG_ID_TALKCONFIG: u32 = 201;
/// Used to send talk back binary data
@@ -795,7 +795,7 @@ async fn connect_try_port(port: u16) -> Result<UdpSocket> {
drop(rng); // Do not hold RNG over an await
}
let addrs: Vec<_> = vec![port]
let addrs: Vec<_> = [port]
.iter()
.chain(ports.iter())
.map(|&port| SocketAddr::from(([0, 0, 0, 0], port)))
+1 -1
View File
@@ -32,7 +32,7 @@ impl BcCamera {
) -> Result<()> {
let connection = self.get_connection();
let msg_num = self.new_message_num();
let mut sub = connection.subscribe(msg_num).await?;
let mut sub = connection.subscribe(MSG_ID_PUSH_INFO, msg_num).await?;
let phone_type_str = match phone_type {
PhoneType::Ios => "reo_iphone",
+1 -1
View File
@@ -121,7 +121,7 @@ impl BcCamera {
expected_size
);
if result.len() != expected_size {
log::warn!("Snap did not recieve expected number of byes");
log::debug!("Snap did not recieve expected number of bytes");
}
} else {
return Err(Error::UnintelligibleReply {
+1 -1
View File
@@ -12,7 +12,7 @@ env_logger = "0.10.0"
fcm-push-listener = "2.0.1"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "../core", version = "0.5" }
neolink_core = { path = "../core", version = "0.6.2" }
regex = "1.8.1"
serde = "1.0.163"
tokio = { version = "1.28.1", features = ["full"] }
+1 -8
View File
@@ -1,5 +1,5 @@
use lazy_static::lazy_static;
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat};
use neolink_core::bc_protocol::DiscoveryMethods;
use regex::Regex;
use serde::Deserialize;
use std::clone::Clone;
@@ -49,13 +49,6 @@ pub(crate) struct CameraConfig {
code = "max_encryption"
))]
pub(crate) max_encryption: String,
#[serde(default = "default_print", alias = "print")]
pub(crate) print_format: PrintFormat,
}
fn default_print() -> PrintFormat {
PrintFormat::None
}
fn default_discovery() -> DiscoveryMethods {
+2 -1
View File
@@ -86,11 +86,12 @@ impl AddressOrUid {
uid: camera_config.camera_uid.clone(),
protocol: ConnectionProtocol::TcpUdp,
discovery: camera_config.discovery,
aux_printing: camera_config.print_format,
max_discovery_retries: 10,
credentials: Credentials {
username: camera_config.username.clone(),
password: camera_config.password.clone(),
},
debug: false,
};
trace!("Camera Info: {:?}", options);
+10 -1
View File
@@ -11,7 +11,7 @@ use tokio::sync::{
};
use tokio_util::sync::CancellationToken;
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, StreamInstance};
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, PushNoti, StreamInstance};
use crate::{config::CameraConfig, Result};
use neolink_core::bc_protocol::{BcCamera, StreamKind};
@@ -176,6 +176,14 @@ impl NeoInstance {
Ok(instance_rx.await?)
}
pub(crate) async fn push_notifications(&self) -> Result<WatchReceiver<Option<PushNoti>>> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::PushNoti(instance_tx))
.await?;
Ok(instance_rx.await?)
}
pub(crate) async fn motion(&self) -> Result<WatchReceiver<MdState>> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
@@ -212,6 +220,7 @@ impl NeoInstance {
Ok(instance_rx.await?)
}
#[allow(dead_code)]
pub(crate) async fn get_state(&self) -> Result<NeoCamThreadState> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
+2
View File
@@ -2,6 +2,7 @@ mod camthread;
mod instance;
mod mdthread;
mod neocam;
mod pushnoti;
mod reactor;
mod streamthread;
mod usecounter;
@@ -10,6 +11,7 @@ pub(crate) use camthread::*;
pub(crate) use instance::*;
pub(crate) use mdthread::*;
pub(crate) use neocam::*;
pub(crate) use pushnoti::*;
pub(crate) use reactor::*;
pub(crate) use streamthread::*;
pub(crate) use usecounter::*;
+102 -17
View File
@@ -13,13 +13,15 @@ use tokio::{
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
},
task::JoinSet,
time::{sleep, Duration},
};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;
use super::{
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoCamThreadState,
NeoInstance, Permit, StreamInstance, StreamRequest, UseCounter,
NeoInstance, Permit, PnRequest, PushNoti, PushNotiThread, StreamInstance, StreamRequest,
UseCounter,
};
use crate::{config::CameraConfig, AnyResult, Result};
use neolink_core::bc_protocol::{BcCamera, StreamKind};
@@ -38,6 +40,7 @@ pub(crate) enum NeoCamCommand {
Connect(OneshotSender<()>),
State(OneshotSender<NeoCamThreadState>),
GetPermit(OneshotSender<Permit>),
PushNoti(OneshotSender<WatchReceiver<Option<PushNoti>>>),
}
/// The underlying camera binding
pub(crate) struct NeoCam {
@@ -55,6 +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 (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
let set = JoinSet::new();
@@ -142,16 +146,16 @@ impl NeoCam {
let _ = sender.send(thread_watch_config_rx.clone());
},
NeoCamCommand::Connect(sender) => {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Connected) {
state_tx.send_replace(NeoCamThreadState::Connected);
log::info!("{}: Connect On Request", thread_watch_config_rx.borrow().name);
log::debug!("{}: 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);
log::debug!("{}: Disconnect On Request", thread_watch_config_rx.borrow().name);
}
let _ = sender.send(());
}
@@ -161,6 +165,13 @@ impl NeoCam {
NeoCamCommand::GetPermit(sender) => {
let _ = sender.send(users.create_activated().await?);
}
NeoCamCommand::PushNoti(sender) => {
pn_request_tx.send(
PnRequest::Get {
sender,
}
).await?;
},
}
}
log::debug!("Control thread Senders dropped");
@@ -248,29 +259,103 @@ 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
let connect_instance = instance.subscribe().await?;
let connect_cancel = me.cancel.clone();
let connect_name = config.name.clone();
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,
}
});
// 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
}
} => v,
}
});
}
// MD permits
let md_permit_instance = instance.subscribe().await?;
let md_permit_cancel = me.cancel.clone();
me.set.spawn(async move {
tokio::select! {
_ = connect_cancel.cancelled() => {
_ = md_permit_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");
permit.dropped_users().await?;
log::debug!("{connect_name}: Idle");
let mut md = md_permit_instance.motion().await?;
loop{
md.wait_for(|md| matches!(md, MdState::Start(_))).await?;
let _permit = Some(md_permit_instance.permit().await?);
md.wait_for(|md| matches!(md, MdState::Stop(_))).await?;
// Try waiting for 30s
// If in those 30s we get motion then return to
// loop early to reaquire the permit
tokio::select!{
_ = sleep(Duration::from_secs(30)) => {},
_ = md.wait_for(|md| matches!(md, MdState::Start(_))) => {},
}
}
} => v,
}
});
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?;
}
} => v,
}
});
}
Ok(me)
}
+131
View File
@@ -0,0 +1,131 @@
//! This thread handles push notifications
//! the last notification is pushed into a watcher
//! as is, which comes fromt the json structure
//!
use fcm_push_listener::*;
use std::{fs, sync::Arc};
use tokio::sync::{
mpsc::Receiver as MpscReceiver,
oneshot::Sender as OneshotSender,
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
};
use super::NeoInstance;
use crate::AnyResult;
pub(crate) struct PushNotiThread {
pn_watcher: Arc<WatchSender<Option<PushNoti>>>,
pn_request_rx: MpscReceiver<PnRequest>,
instance: NeoInstance,
}
// The push notification
#[derive(Clone, Eq, PartialEq)]
pub(crate) struct PushNoti {
pub(crate) message: String,
pub(crate) id: Option<String>,
}
pub(crate) enum PnRequest {
Get {
sender: OneshotSender<WatchReceiver<Option<PushNoti>>>,
},
}
impl PushNotiThread {
pub(crate) async fn new(
pn_request_rx: MpscReceiver<PnRequest>,
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<()> {
let sender_id = "743639030586"; // andriod
// let sender_id = "696841269229"; // ios
let token_path = dirs::config_dir().map(|mut d| {
d.push("./neolink_token.toml");
d
});
let registration = if let Some(Ok(Ok(registration))) =
token_path.as_ref().map(|token_path| {
fs::read_to_string(token_path).map(|v| toml::from_str::<Registration>(&v))
}) {
log::debug!("Loaded push notification token");
registration
} else {
log::debug!("Registering new push notification token");
let registration = fcm_push_listener::register(sender_id).await?;
let new_token = toml::to_string(&registration)?;
if let Some(Err(e)) = token_path
.as_ref()
.map(|token_path| fs::write(token_path, &new_token))
{
log::warn!(
"Unable to save push notification details ({}) to {:#?} because of the error {:#?}",
new_token,
token_path,
e
);
}
registration
};
// Send registration.fcm_token to the server to allow it to send push messages to you.
log::debug!("registration.fcm_token: {}", registration.fcm_token);
let md5ed = md5::compute(format!("WHY_REOLINK_{:?}", registration.fcm_token));
let uid = format!("{:X}", md5ed);
log::debug!("push notification UID: {}", uid);
self.instance
.run_task(|camera| {
let uid = uid.clone();
let fcm_token = registration.fcm_token.clone();
Box::pin(async move {
camera.send_pushinfo_android(&fcm_token, &uid).await?;
AnyResult::Ok(())
})
})
.await?;
log::debug!("Push notification Listening");
let thread_pn_watcher = self.pn_watcher.clone();
let mut listener = FcmPushListener::create(
registration,
|message: FcmMessage| {
thread_pn_watcher.send_replace(Some(PushNoti {
message: message.payload_json,
id: message.persistent_id,
}));
},
vec![],
);
tokio::select! {
v = async {
listener.connect().await?;
AnyResult::Ok(())
} => v,
v = async {
while let Some(msg) = self.pn_request_rx.recv().await {
match msg {
PnRequest::Get{sender} => {
let _ = sender.send(self.pn_watcher.subscribe());
}
}
}
AnyResult::Ok(())
} => v,
}?;
AnyResult::Ok(())
}
}
+3 -1
View File
@@ -357,7 +357,7 @@ impl StreamData {
let thread_inuse = me.users.create_deactivated().await?;
let vid_history = me.vid_history.clone();
let aud_history = me.aud_history.clone();
let mut permit = instance.permit().await?;
me.handle = Some(tokio::task::spawn(async move {
let r = tokio::select! {
_ = cancel.cancelled() => {
@@ -370,8 +370,10 @@ impl StreamData {
v = thread_inuse.dropped_users() => {
// Handles the stop and restart when no active users
log::debug!("Streaming STOP");
permit.deactivate().await?;
v?;
thread_inuse.aquired_users().await?; // Wait for new users of the stream
permit.activate().await?;
log::debug!("Streaming START");
AnyResult::Ok(())
},
+6
View File
@@ -199,6 +199,12 @@ pub(crate) struct CameraConfig {
alias = "max_retries"
)]
pub(crate) max_discovery_retries: usize,
#[serde(default = "default_true", alias = "push", alias = "push_noti")]
pub(crate) push_notifications: bool,
#[serde(default = "default_false", alias = "idle", alias = "idle_disc")]
pub(crate) idle_disconnect: bool,
}
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq, Hash)]
+31 -1
View File
@@ -276,6 +276,10 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
.send_message("status/motion", "unknown", true)
.await
.with_context(|| format!("Failed to publish motion unknown for {}", camera_name))?;
mqtt_instance
.send_message("status/notification", "unknown", true)
.await
.with_context(|| format!("Failed to publish push notification unknown for {}", camera_name))?;
let _drop_message2 = mqtt_instance.drop_guard_message("status/motion", "unknown").await?;
if let Some(discovery_config) = config.discovery.as_ref() {
@@ -296,6 +300,9 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
let camera_motion = camera.clone();
let mqtt_motion = mqtt_instance.resubscribe().await?;
let camera_pn = camera.clone();
let mqtt_pn = mqtt_instance.resubscribe().await?;
let camera_snap = camera.clone();
let mqtt_snap = mqtt_instance.resubscribe().await?;
@@ -510,7 +517,30 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
Err(Err(e)) => Err(e),
}?;
AnyResult::Ok(())
} => v
} => v,
// Handle the push notification messages
v = async {
let mut pn = camera_pn.push_notifications().await?;
let mut prev_noti = None;
loop {
let v = async {
let noti = pn.wait_for(|noti| noti != &prev_noti && noti.is_some()).await.with_context(|| {
format!("{}: PushNoti Watch Dropped", camera_name)
})?.clone();
mqtt_pn.send_message("status/notification", &noti.as_ref().unwrap().message, true).await.with_context(|| {
format!("{}: Failed to publish push notification", camera_name)
})?;
prev_noti = noti;
AnyResult::Ok(())
}.await;
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
Err(Ok(e)) => Err(e.into()),
Err(Err(e)) => Err (e),
}?;
}
} => v,
}?;
AnyResult::Ok(())
} => v,
+1 -1
View File
@@ -367,7 +367,7 @@ impl MqttInstance {
message: &str,
retain: bool,
) -> AnyResult<()> {
let topics = vec![
let topics = [
root_topic.to_string(),
self.name.clone(),
sub_topic.to_string(),
+1 -1
View File
@@ -270,7 +270,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
let mut i = IntervalStream::new(interval(Duration::from_secs(15)));
while i.next().await.is_some() {
let stream_info = later_camera
.run_task(|cam| Box::pin(async move { Ok(cam.get_stream_info().await?) }))
.run_passive_task(|cam| Box::pin(async move { Ok(cam.get_stream_info().await?) }))
.await?;
let new_supported_streams = stream_info