Skip to content

Commit

Permalink
always emit motion detected even if same after some interval
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Dec 8, 2024
1 parent 8647dca commit fdf49d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
51 changes: 43 additions & 8 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const PORT: u16 = 8080;

const SENSOR_REQUEST_INTERVAL: Duration = Duration::from_millis(200);

const MOTION_ACTIVE_RESET_DELAY: Duration = Duration::from_secs(3);

#[derive(Debug, Default, Clone, PartialEq, Eq, glib::Boxed)]
#[boxed_type(name = "UetsCameraState")]
pub enum CameraState {
Expand Down Expand Up @@ -58,8 +60,10 @@ mod imp {
pub(super) pipeline: RefCell<Option<(gst::Pipeline, BusWatchGuard)>>,
pub(super) ip_addr: RefCell<String>,

pub(super) motion_active: Cell<(u64, bool)>,
pub(super) sensor_request_handle: RefCell<Option<glib::JoinHandle<()>>>,

pub(super) motion_active: Cell<Option<(u64, bool)>>,
pub(super) motion_active_reset_timeout: RefCell<Option<glib::SourceId>>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -321,22 +325,53 @@ impl Camera {
.await
.map_err(|err| err.into_inner())?;

let (prev_ts, prev_motion_active) = imp.motion_active.get();
let (ts, motion_active) = data.motion_active.motion_active_latest()?;

if ts > prev_ts && motion_active != prev_motion_active {
imp.motion_active.set((ts, motion_active));
if imp
.motion_active
.get()
.is_some_and(|(prev_ts, prev_motion_active)| {
ts <= prev_ts || motion_active == prev_motion_active
})
{
return Ok(());
}

tracing::debug!(motion_active);
tracing::debug!(motion_active);

if motion_active && !prev_motion_active {
self.emit_by_name::<()>("motion-detected", &[]);
}
if motion_active && imp.motion_active.get().map_or(true, |(_, active)| !active) {
self.emit_by_name::<()>("motion-detected", &[]);
}

imp.motion_active.set(Some((ts, motion_active)));
self.restart_motion_active_reset_timeout();

Ok(())
}

fn restart_motion_active_reset_timeout(&self) {
let imp = self.imp();

if let Some(source_id) = imp.motion_active_reset_timeout.take() {
source_id.remove();
}

let source_id = glib::timeout_add_local_once(
MOTION_ACTIVE_RESET_DELAY,
clone!(
#[weak(rename_to = obj)]
self,
move || {
let imp = obj.imp();
imp.motion_active_reset_timeout.replace(None);

imp.motion_active.replace(None);
},
),
);
imp.motion_active_reset_timeout.replace(Some(source_id));
}

fn handle_bus_message(&self, message: &gst::Message) -> glib::ControlFlow {
use gst::MessageView;

Expand Down
13 changes: 7 additions & 6 deletions src/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod imp {
pub(super) camera: RefCell<Option<Camera>>,
pub(super) aux_cameras: RefCell<Vec<(Camera, Vec<glib::SignalHandlerId>)>>,
pub(super) camera_last_detected: RefCell<Option<String>>,
pub(super) camera_last_detected_timeout: RefCell<Option<glib::SourceId>>,
pub(super) camera_last_detected_reset_timeout: RefCell<Option<glib::SourceId>>,

pub(super) detected_wo_id_capture: RefCell<Option<(DateTimeBoxed, Option<JpegImage>)>>,
pub(super) detected_wo_id_alert_timeout: RefCell<Option<glib::SourceId>>,
Expand Down Expand Up @@ -218,7 +218,7 @@ impl Detector {
}

imp.camera_last_detected.replace(Some(code.to_string()));
obj.restart_camera_last_detected_timeout();
obj.restart_camera_last_detected_reset_timeout();
}
)),
camera.connect_motion_detected(clone!(
Expand Down Expand Up @@ -303,10 +303,10 @@ impl Detector {
}
}

fn restart_camera_last_detected_timeout(&self) {
fn restart_camera_last_detected_reset_timeout(&self) {
let imp = self.imp();

if let Some(source_id) = imp.camera_last_detected_timeout.take() {
if let Some(source_id) = imp.camera_last_detected_reset_timeout.take() {
source_id.remove();
}

Expand All @@ -317,13 +317,14 @@ impl Detector {
self,
move || {
let imp = obj.imp();
imp.camera_last_detected_timeout.replace(None);
imp.camera_last_detected_reset_timeout.replace(None);

imp.camera_last_detected.replace(None);
},
),
);
imp.camera_last_detected_timeout.replace(Some(source_id));
imp.camera_last_detected_reset_timeout
.replace(Some(source_id));
}
}

Expand Down

0 comments on commit fdf49d2

Please sign in to comment.