mqtt motion adjustments

This commit is contained in:
Blake Blackshear 2022-05-15 07:45:04 -05:00
parent efe3f96223
commit 7c8142174e

View File

@ -647,7 +647,7 @@ class TrackedObjectProcessor(threading.Thread):
self.stop_event = stop_event self.stop_event = stop_event
self.camera_states: dict[str, CameraState] = {} self.camera_states: dict[str, CameraState] = {}
self.frame_manager = SharedMemoryFrameManager() self.frame_manager = SharedMemoryFrameManager()
self.last_motion_updates: dict[str, int] = {} self.last_motion_detected: dict[str, float] = {}
def start(camera, obj: TrackedObject, current_frame_time): def start(camera, obj: TrackedObject, current_frame_time):
self.event_queue.put(("start", camera, obj.to_dict())) self.event_queue.put(("start", camera, obj.to_dict()))
@ -840,11 +840,11 @@ class TrackedObjectProcessor(threading.Thread):
return True return True
def should_mqtt_motion(self, camera, motion_boxes): def update_mqtt_motion(self, camera, frame_time, motion_boxes):
# publish if motion is currently being detected # publish if motion is currently being detected
if motion_boxes: if motion_boxes:
# only send True if motion hasn't been detected recently # only send ON if motion isn't already active
if self.last_motion_updates.get(camera, 0) == 0: if self.last_motion_detected.get(camera, 0) == 0:
self.client.publish( self.client.publish(
f"{self.topic_prefix}/{camera}/motion", f"{self.topic_prefix}/{camera}/motion",
"ON", "ON",
@ -852,20 +852,19 @@ class TrackedObjectProcessor(threading.Thread):
) )
# always updated latest motion # always updated latest motion
self.last_motion_updates[camera] = int(datetime.datetime.now().timestamp()) self.last_motion_detected[camera] = frame_time
elif not motion_boxes and self.last_motion_updates.get(camera, 0) != 0: elif self.last_motion_detected.get(camera, 0) > 0:
mqtt_delay = self.config.cameras[camera].motion.mqtt_off_delay mqtt_delay = self.config.cameras[camera].motion.mqtt_off_delay
now = int(datetime.datetime.now().timestamp())
# If no motion, make sure the off_delay has passed # If no motion, make sure the off_delay has passed
if now - self.last_motion_updates.get(camera, 0) >= mqtt_delay: if frame_time - self.last_motion_detected.get(camera, 0) >= mqtt_delay:
self.client.publish( self.client.publish(
f"{self.topic_prefix}/{camera}/motion", f"{self.topic_prefix}/{camera}/motion",
"OFF", "OFF",
retain=False, retain=False,
) )
# reset the last_motion so redundant `off` commands aren't sent # reset the last_motion so redundant `off` commands aren't sent
self.last_motion_updates[camera] = 0 self.last_motion_detected[camera] = 0
def get_best(self, camera, label): def get_best(self, camera, label):
# TODO: need a lock here # TODO: need a lock here
@ -902,7 +901,7 @@ class TrackedObjectProcessor(threading.Thread):
frame_time, current_tracked_objects, motion_boxes, regions frame_time, current_tracked_objects, motion_boxes, regions
) )
self.should_mqtt_motion(camera, motion_boxes) self.update_mqtt_motion(camera, frame_time, motion_boxes)
tracked_objects = [ tracked_objects = [
o.to_dict() for o in camera_state.tracked_objects.values() o.to_dict() for o in camera_state.tracked_objects.values()