Skip to content

Commit

Permalink
Add webcam archive cleanup and fixed webcam notifications. #963
Browse files Browse the repository at this point in the history
  • Loading branch information
theyosh committed Oct 31, 2024
1 parent 29d0f25 commit 39b6ddc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
33 changes: 26 additions & 7 deletions hardware/webcam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from io import BytesIO
from time import time
from time import sleep
from shutil import rmtree
from func_timeout import func_timeout, FunctionTimedOut

import math
Expand Down Expand Up @@ -570,14 +571,18 @@ def archive(self, timeout):
archive = (
self.__last_archive_image is None or int(time() - self.__last_archive_image.stat().st_mtime) >= timeout
)
if archive:
start = time()
self.__last_archive_image = self.raw_archive_path
self.__last_archive_image.parent.mkdir(parents=True, exist_ok=True)
self.__raw_image.save(self.__last_archive_image, "jpeg", quality=self.__JPEG_QUALITY, exif=self.__exit_data)
logger.debug(f"Webcam {self.name}: Archiving image to disk took: {time()-start:.3f} seconds")

return archive
if not archive:
return False

start = time()
self.__last_archive_image = self.raw_archive_path
self.__last_archive_image.parent.mkdir(parents=True, exist_ok=True)
self.__raw_image.save(self.__last_archive_image, "jpeg", quality=self.__JPEG_QUALITY, exif=self.__exit_data)
logger.debug(f"Webcam {self.name}: Archiving image to disk took: {time()-start:.3f} seconds")
print(f"Webcam {self.name}: Archiving image to disk took: {time()-start:.3f} seconds")

return True

def motion_capture(self, motion_frame="last", motion_threshold=25, motion_area=500, motion_boxes="green"):
if not self.state:
Expand Down Expand Up @@ -654,6 +659,20 @@ def motion_capture(self, motion_frame="last", motion_threshold=25, motion_area=5

return motion_detected

def clear_archive(self, period=365):
startDate = datetime.now() - timedelta(days=period)
logger.debug(f"Webcam {self.name}: Start clearing archive older than {startDate}")
for counter in range(10):
while Path.exists(self.__ARCHIVE_LOCATION.joinpath(self.id, startDate.strftime("%Y/%m/%d"))):
start = time()
rmtree(self.__ARCHIVE_LOCATION.joinpath(self.id, startDate.strftime("%Y/%m/%d")))
logger.info(
f"Webcam {self.name}: Removed folder {self.__ARCHIVE_LOCATION.joinpath(self.id, startDate.strftime('%Y/%m/%d'))} in {time()-start:.3f} seconds"
)
startDate = startDate - timedelta(days=1)

startDate = startDate - timedelta(days=1)

# TODO: What to stop....?
def stop(self):
pass
Expand Down
7 changes: 5 additions & 2 deletions terrariumEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,17 +1201,20 @@ def __process_webcam(self, webcam, current_state, relays):
webcam.motion["boxes"],
)
if newImage:
self.__environment.notification.message(
self.notification.message(
"webcam_motion", webcam.to_dict(), [self.webcams[webcam.id].last_archived_image]
)

else:
newImage = self.webcams[webcam.id].archive(int(webcam.archive["state"]))
if newImage:
self.__environment.notification.message(
self.notification.message(
"webcam_archive", webcam.to_dict(), [self.webcams[webcam.id].last_archived_image]
)

if newImage and int(webcam.archive["history"] or 0) > 0:
self.webcams[webcam.id].clear_archive(int(webcam.archive["history"]))

webcamLogger.info(f"Updated webcam {webcam} in {time.time()-start:.2f} seconds.")

with futures.ThreadPoolExecutor() as pool:
Expand Down

0 comments on commit 39b6ddc

Please sign in to comment.