Saturating subs

This commit is contained in:
QuantumEntangledAndy committed 2023-09-15 07:23:33 +07:00
1 parent 5561236323
commit 3391666598
6 files changed
+274 -210

No files matched your search

@@ -504,7 +504,7 @@ impl UdpPayloadInner {
for (_, resend) in self.sent.iter() {
self.socket_in.feed(BcUdp::Data(resend.clone())).await?;
}
self.ack_tx.send(self.build_send_ack()).await?; // Ensure we update this sometimes too
self.ack_tx.send(self.build_send_ack()).await?; // Ensure we update the ack packet sometimes too
Result::Ok(())
},
v = self.thread_sink.next() => {
+41 -12
View File
@@ -2,7 +2,7 @@ use crate::mqtt::Discoveries;
use lazy_static::lazy_static;
use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat, StreamKind};
use regex::Regex;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::clone::Clone;
use std::collections::HashSet;
use validator::{Validate, ValidationError};
@@ -15,7 +15,7 @@ lazy_static! {
Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Clone)]
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
pub(crate) struct Config {
#[validate]
pub(crate) cameras: Vec<CameraConfig>,
@@ -49,7 +49,7 @@ pub(crate) struct Config {
pub(crate) users: Vec<UserConfig>,
}
#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, Clone, Validate, PartialEq, Eq)]
#[validate(schema(function = "validate_mqtt_server", skip_on_field_errors = true))]
pub(crate) struct MqttServerConfig {
#[serde(alias = "server")]
@@ -67,7 +67,7 @@ pub(crate) struct MqttServerConfig {
pub(crate) client_auth: Option<(std::path::PathBuf, std::path::PathBuf)>,
}
#[derive(Debug, Deserialize, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Clone, Copy, Eq, PartialEq)]
pub(crate) enum StreamConfig {
#[serde(alias = "none")]
None,
@@ -123,7 +123,7 @@ impl StreamConfig {
}
}
#[derive(Debug, Deserialize, Validate, Clone, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
#[validate(schema(function = "validate_camera_config"))]
pub(crate) struct CameraConfig {
pub(crate) name: String,
@@ -148,7 +148,7 @@ pub(crate) struct CameraConfig {
#[validate]
#[serde(default = "default_mqtt")]
pub(crate) mqtt: Option<MqttConfig>,
pub(crate) mqtt: MqttConfig,
#[validate]
#[serde(default = "default_pause")]
@@ -198,7 +198,7 @@ pub(crate) struct CameraConfig {
pub(crate) max_discovery_retries: usize,
}
#[derive(Debug, Deserialize, Validate, Clone, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq)]
pub(crate) struct UserConfig {
#[validate(custom = "validate_username")]
#[serde(alias = "username")]
@@ -208,7 +208,7 @@ pub(crate) struct UserConfig {
pub(crate) pass: String,
}
#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, Clone, Validate, PartialEq, Eq)]
pub(crate) struct MqttConfig {
#[serde(default = "default_true")]
pub(crate) enable_motion: bool,
@@ -218,14 +218,30 @@ pub(crate) struct MqttConfig {
pub(crate) enable_light: bool,
#[serde(default = "default_true")]
pub(crate) enable_battery: bool,
/// Update time in ms
#[serde(default = "default_2000")]
#[validate(range(
min = 500,
message = "Update ms should be > 500",
code = "battery_update"
))]
pub(crate) battery_update: u64,
#[serde(default = "default_true")]
pub(crate) enable_preview: bool,
/// Update time in ms
#[validate(range(
min = 500,
message = "Update ms should be > 500",
code = "preview_update"
))]
#[serde(default = "default_2000")]
pub(crate) preview_update: u64,
#[serde(default)]
pub(crate) discovery: Option<MqttDiscoveryConfig>,
}
#[derive(Debug, Deserialize, Clone, Validate, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, Clone, Validate, PartialEq, Eq)]
pub(crate) struct MqttDiscoveryConfig {
pub(crate) topic: String,
@@ -250,8 +266,17 @@ const fn default_false() -> bool {
false
}
fn default_mqtt() -> Option<MqttConfig> {
None
fn default_mqtt() -> MqttConfig {
MqttConfig {
enable_pings: true,
enable_motion: true,
enable_light: true,
enable_battery: true,
battery_update: 2000,
enable_preview: true,
preview_update: 2000,
discovery: Default::default(),
}
}
fn default_print() -> PrintFormat {
@@ -266,7 +291,7 @@ fn default_maxenc() -> String {
"Aes".to_string()
}
#[derive(Debug, Deserialize, Validate, Clone, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq)]
pub(crate) struct PauseConfig {
#[serde(default = "default_on_motion")]
pub(crate) on_motion: bool,
@@ -355,6 +380,10 @@ fn default_max_discovery_retries() -> usize {
10
}
fn default_2000() -> u64 {
2000
}
pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"];
fn validate_username(name: &str) -> Result<(), ValidationError> {
if name.trim().is_empty() {
+1 -1
View File
@@ -10,7 +10,7 @@ use super::mqttc::MqttInstance;
use crate::{common::NeoInstance, config::MqttDiscoveryConfig};
use serde::{Deserialize, Serialize, Serializer};
#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Copy, Hash)]
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Copy, Hash)]
pub(crate) enum Discoveries {
#[serde(alias = "floodlight", alias = "light")]
Floodlight,
+220 -188
View File
@@ -159,6 +159,41 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
}
});
// This threads prints the config
let mut thread_config = config.clone();
let thread_instance = mqtt.subscribe("").await?;
set.spawn(async move {
let mut curr_config = thread_config.borrow().clone();
let str = toml::to_string(&curr_config)?;
thread_instance.send_message("config", &str, false).await?;
loop {
curr_config = thread_config
.wait_for(|new_conf| new_conf != &curr_config)
.await?
.clone();
let str = toml::to_string(&curr_config)?;
thread_instance.send_message("config", &str, false).await?;
}
});
// This threads checks for config changes on the mqtt
let mut thread_config = config.clone();
let mut thread_instance = mqtt.subscribe("").await?;
set.spawn(async move {
while let Ok(msg) = {
log::info!("Trying to pull msg");
let v = thread_instance.recv().await;
if let Err(e) = &v {
log::info!("{:?}", e);
}
v
} {
log::info!("{}", msg.topic);
}
log::info!("Shuting down MQTT Config Updater");
AnyResult::Ok(())
});
while let Some(result) = set.join_next().await {
result??;
}
@@ -179,6 +214,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
continue;
}
v = async {
//Publish initial states
mqtt_instance
.send_message("status", "disconnected", true)
.await
@@ -188,207 +224,203 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
.await
.with_context(|| format!("Failed to publish motion unknown for {}", camera_name))?;
if let Some(config) = config.as_ref() {
//Publish initial states
if let Some(discovery_config) = config.discovery.as_ref() {
enable_discovery(discovery_config, &mqtt_instance, &camera).await?;
}
if let Some(discovery_config) = config.discovery.as_ref() {
enable_discovery(discovery_config, &mqtt_instance, &camera).await?;
}
let camera_msg = camera.clone();
let mut mqtt_msg = mqtt_instance.resubscribe().await?;
let cancel_msg = cancel.clone();
let mut set_msg = JoinSet::new();
let camera_msg = camera.clone();
let mut mqtt_msg = mqtt_instance.resubscribe().await?;
let cancel_msg = cancel.clone();
let mut set_msg = JoinSet::new();
let mut camera_watch = camera.camera();
let mqtt_watch = mqtt_instance.resubscribe().await?;
let mut camera_watch = camera.camera();
let mqtt_watch = mqtt_instance.resubscribe().await?;
let camera_floodlight = camera.clone();
let mqtt_floodlight = mqtt_instance.resubscribe().await?;
let camera_floodlight = camera.clone();
let mqtt_floodlight = mqtt_instance.resubscribe().await?;
let camera_motion = camera.clone();
let mqtt_motion = mqtt_instance.resubscribe().await?;
let camera_motion = camera.clone();
let mqtt_motion = mqtt_instance.resubscribe().await?;
let camera_snap = camera.clone();
let mqtt_snap = mqtt_instance.resubscribe().await?;
let camera_snap = camera.clone();
let mqtt_snap = mqtt_instance.resubscribe().await?;
let camera_battery = camera.clone();
let mqtt_battery = mqtt_instance.resubscribe().await?;
let camera_battery = camera.clone();
let mqtt_battery = mqtt_instance.resubscribe().await?;
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
// Handles incomming requests
v = async {
let (tx, mut rx) = mpsc(1);
tokio::select! {
v = async {
while let Ok(msg) = mqtt_msg.recv().await {
let mqtt_msg = mqtt_msg.resubscribe().await?;
let camera_msg = camera_msg.clone();
let tx = tx.clone();
let cancel_msg = cancel_msg.clone();
set_msg.spawn(async move {
tokio::select!{
_ = cancel_msg.cancelled() => AnyResult::Ok(()),
v = async {
let res = handle_mqtt_message(msg, &mqtt_msg, &camera_msg).await;
if res.is_err() {
tx.send(res).await?;
}
AnyResult::Ok(())
} => v,
}
});
}
AnyResult::Ok(())
} => v,
v = rx.recv() => {
v.ok_or(anyhow!("All error senders were dropped"))?
},
}?;
AnyResult::Ok(())
} => v,
// Handle camera disconnect/connect
v = async {
loop {
camera_watch.wait_for(|cam| cam.upgrade().is_some()).await.with_context(|| {
format!("{}: Online Watch Dropped", camera_name)
})?;
mqtt_watch.send_message("status", "online", true).await.with_context(|| {
format!("{}: Failed to publish Online", 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", "disconnected", true).await.with_context(|| {
format!("{}: Failed to publish disconnected", camera_name)
})?;
}
} => v,
// Handle the floodlight
v = async {
let (tx, mut rx) = mpsc(100);
tokio::select! {
v = camera_floodlight.run_task(|cam| {
tokio::select! {
_ = cancel.cancelled() => AnyResult::Ok(()),
// Handles incomming requests
v = async {
let (tx, mut rx) = mpsc(1);
tokio::select! {
v = async {
while let Ok(msg) = mqtt_msg.recv().await {
let mqtt_msg = mqtt_msg.resubscribe().await?;
let camera_msg = camera_msg.clone();
let tx = tx.clone();
Box::pin(
async move {
let mut reciever = tokio_stream::wrappers::ReceiverStream::new(cam.listen_on_flightlight().await?);
while let Some(flights) = reciever.next().await {
for flight in flights.floodlight_status_list.iter() {
if flight.status == 0 {
tx.send(false).await?;
} else {
tx.send(true).await?;
}
let cancel_msg = cancel_msg.clone();
set_msg.spawn(async move {
tokio::select!{
_ = cancel_msg.cancelled() => AnyResult::Ok(()),
v = async {
let res = handle_mqtt_message(msg, &mqtt_msg, &camera_msg).await;
if res.is_err() {
tx.send(res).await?;
}
AnyResult::Ok(())
} => v,
}
});
}
AnyResult::Ok(())
} => v,
v = rx.recv() => {
v.ok_or(anyhow!("All error senders were dropped"))?
},
}?;
AnyResult::Ok(())
} => v,
// Handle camera disconnect/connect
v = async {
loop {
camera_watch.wait_for(|cam| cam.upgrade().is_some()).await.with_context(|| {
format!("{}: Online Watch Dropped", camera_name)
})?;
log::info!("Publish online");
mqtt_watch.send_message("status", "online", true).await.with_context(|| {
format!("{}: Failed to publish Online", 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(|| {
format!("{}: Failed to publish disconnected", camera_name)
})?;
}
} => v,
// Handle the floodlight
v = async {
let (tx, mut rx) = mpsc(100);
tokio::select! {
v = camera_floodlight.run_task(|cam| {
let tx = tx.clone();
Box::pin(
async move {
let mut reciever = tokio_stream::wrappers::ReceiverStream::new(cam.listen_on_flightlight().await?);
while let Some(flights) = reciever.next().await {
for flight in flights.floodlight_status_list.iter() {
if flight.status == 0 {
tx.send(false).await?;
} else {
tx.send(true).await?;
}
}
AnyResult::Ok(())
}
)
}) => v,
v = async {
while let Some(on) = rx.recv().await {
if on {
mqtt_floodlight.send_message("status/floodlight", "on", true).await?;
} else {
mqtt_floodlight.send_message("status/floodlight", "off", true).await?;
}
AnyResult::Ok(())
}
AnyResult::Ok(())
} => v,
)
}) => v,
v = async {
while let Some(on) = rx.recv().await {
if on {
mqtt_floodlight.send_message("status/floodlight", "on", true).await?;
} else {
mqtt_floodlight.send_message("status/floodlight", "off", true).await?;
}
}
AnyResult::Ok(())
} => v,
}?;
AnyResult::Ok(())
} => v,
// Handle the motion messages
v = async {
let mut md = camera_motion.motion().await?;
loop {
md.wait_for(|state| matches!(state, MdState::Start(_))).await.with_context(|| {
format!("{}: MdStart Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "on", true).await.with_context(|| {
format!("{}: Failed to publish motion start", camera_name)
})?;
md.wait_for(|state| matches!(state, MdState::Stop(_))).await.with_context(|| {
format!("{}: MdStop Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "off", true).await.with_context(|| {
format!("{}: Failed to publish motion stop", camera_name)
})?;
}
} => v,
// Handle the SNAP (image preview)
v = async {
let mut wait = IntervalStream::new({
let mut i = interval(Duration::from_millis(config.preview_update));
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let image = camera_snap.run_task(|cam| {
Box::pin(async move {
let image = cam.get_snapshot().await?;
AnyResult::Ok(image)
})
}).await;
let image = match image {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Image not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
AnyResult::Ok(())
} => v,
// Handle the motion messages
v = async {
let mut md = camera_motion.motion().await?;
loop {
md.wait_for(|state| matches!(state, MdState::Start(_))).await.with_context(|| {
format!("{}: MdStart Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "on", true).await.with_context(|| {
format!("{}: Failed to publish motion start", camera_name)
})?;
md.wait_for(|state| matches!(state, MdState::Stop(_))).await.with_context(|| {
format!("{}: MdStop Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "off", true).await.with_context(|| {
format!("{}: Failed to publish motion stop", camera_name)
})?;
}
} => v,
// Handle the SNAP (image preview)
v = async {
let mut wait = IntervalStream::new({
let mut i = interval(Duration::from_millis(500));
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let image = camera_snap.run_task(|cam| {
Box::pin(async move {
let image = cam.get_snapshot().await?;
AnyResult::Ok(image)
})
}).await;
let image = match image {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Image not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_snap
.send_message("status/preview", BASE64.encode(image).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish preview", camera_name)
})?;
}
AnyResult::Ok(())
} => v,
// Handle the battery publish
v = async {
let mut wait = IntervalStream::new({
let mut i = interval(Duration::from_millis(5000));
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let xml = camera_battery.run_task(|cam| {
Box::pin(async move {
let xml = cam.battery_info().await?;
AnyResult::Ok(xml)
})
}).await;
let xml = match xml {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Battery not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_battery
.send_message("status/battery_level", format!("{}", xml.battery_percent).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish battery", camera_name)
})?;
}
AnyResult::Ok(())
} => v
}?;
} else {
futures::future::pending().await
}
mqtt_snap
.send_message("status/preview", BASE64.encode(image).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish preview", camera_name)
})?;
}
AnyResult::Ok(())
} => v,
// Handle the battery publish
v = async {
let mut wait = IntervalStream::new({
let mut i = interval(Duration::from_millis(config.preview_update));
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let xml = camera_battery.run_task(|cam| {
Box::pin(async move {
let xml = cam.battery_info().await?;
AnyResult::Ok(xml)
})
}).await;
let xml = match xml {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Battery not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_battery
.send_message("status/battery_level", format!("{}", xml.battery_percent).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish battery", camera_name)
})?;
}
AnyResult::Ok(())
} => v
}?;
AnyResult::Ok(())
} => v,
};
+5 -2
View File
@@ -176,6 +176,7 @@ async fn run_mqtt_server(
"disconnected".to_string(),
).await?;
let _ = reply.send(());
return Err(anyhow!("Disconneting"));
}
MqttRequest::Subscribe(name, reply) => {
let instance = MqttInstance {
@@ -199,13 +200,13 @@ async fn run_mqtt_server(
match notification {
Event::Incoming(Incoming::ConnAck(connected)) => {
if ConnectReturnCode::Success == connected.code {
// Publish disconnected at first then wait for connect from the camera insances
// Publish connected now that we are online
client
.publish(
"neolink/status".to_string(),
QoS::AtLeastOnce,
true,
"disconnected",
"connected",
)
.await?;
// We succesfully logged in. Now ask for the cameras subscription.
@@ -215,10 +216,12 @@ async fn run_mqtt_server(
}
}
Event::Incoming(Incoming::Publish(published_message)) => {
log::info!("Got MQTT message: {:?}", published_message);
if let Some(sub_topic) = published_message
.topic
.strip_prefix("neolink/")
{
let _ = incomming_tx
.send(MqttReply {
topic: sub_topic.to_string(),
+6 -6
View File
@@ -380,7 +380,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
#[derive(Clone, Debug)]
enum StreamData {
Media { data: Vec<u8>, ts: Duration },
Media { data: Arc<Vec<u8>>, ts: Duration },
Seek { ts: Duration, reply: MpscSender<()> },
}
@@ -569,7 +569,7 @@ async fn stream_run(
if let Ok(data) = data {
thread_vid_data_tx.send(
StreamData::Media {
data: data.data,
data: Arc::new(data.data),
ts: data.ts
}
)?;
@@ -595,7 +595,7 @@ async fn stream_run(
if let Ok(data) = data {
thread_aud_data_tx.send(
StreamData::Media {
data: data.data,
data: Arc::new(data.data),
ts: data.ts
}
)?;
@@ -828,14 +828,14 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
// Update rt
if let Some(rt_i) = get_runtime(app) {
if let Some(last_rt) = last_rt {
let delta_rt = rt_i - last_rt;
let delta_rt = rt_i.saturating_sub(last_rt);
rt += delta_rt;
}
last_rt = Some(rt_i);
}
// Update ft
if let Some(last_ft) = last_ft {
let delta_ft = ft_i - last_ft;
let delta_ft = ft_i.saturating_sub(last_ft);
ft += delta_ft;
}
last_ft = Some(ft_i);
@@ -902,7 +902,7 @@ fn get_runtime(app: &AppSrc) -> Option<Duration> {
if let Some(clock) = app.clock() {
if let Some(time) = clock.time() {
if let Some(base_time) = app.base_time() {
let runtime = time.saturating_sub(base_time);
let runtime = timesaturating_sub(base_time);
return Some(Duration::from_micros(runtime.useconds()));
}
}