diff --git a/hardware/webcam/__init__.py b/hardware/webcam/__init__.py index 07b39ff68..653776589 100644 --- a/hardware/webcam/__init__.py +++ b/hardware/webcam/__init__.py @@ -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 @@ -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: @@ -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 diff --git a/terrariumEngine.py b/terrariumEngine.py index 9fb0a9e29..0e93f5258 100644 --- a/terrariumEngine.py +++ b/terrariumEngine.py @@ -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: