diff --git a/README.md b/README.md index 19cf17c..1d374af 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - import `msv_webcam_frontend.py` - `chmod +x /opt/msv-webcam-frontend/msv_webcam_frontend.py` - `nano /etc/systemd/system/msv-webcam-frontend.service` -- create dst dir `mkdir -p /var/www/html/msv-buehl-moos.de/webcam/data` +- create dst dir `mkdir -p /var/www/html/msv-buehl-moos.de/webcam/data/camera/` - `systemctl daemon-reload && systemctl enable --now msv-webcam-frontend.service` - copy WebApp to `/var/www/html/msv-buehl-moos.de/webcam/` \ No newline at end of file diff --git a/msv-webcam-frontend.service b/msv-webcam-frontend.service index e14646f..52a5d39 100644 --- a/msv-webcam-frontend.service +++ b/msv-webcam-frontend.service @@ -10,7 +10,7 @@ Group=wwwrun Restart=on-failure RestartSec=5s WorkingDirectory=/opt/msv-webcam-frontend/ -ExecStart=/usr/bin/python3 /opt/msv-webcam-frontend/msv_webcam_frontend.py source_dir=/tmp/msv_webcam_current destination_dir=/var/www/html/msv-buehl-moos.de/webcam/data/ +ExecStart=/usr/bin/python3 /opt/msv-webcam-frontend/msv_webcam_frontend.py source_dir=/tmp/msv_webcam_current destination_dir=/var/www/html/msv-buehl-moos.de/webcam/data/camera/ timeout=300 min_size=100000 [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/msv_webcam_frontend.py b/msv_webcam_frontend.py index 14b5b82..a81f9fa 100644 --- a/msv_webcam_frontend.py +++ b/msv_webcam_frontend.py @@ -6,9 +6,11 @@ import os import errno import time +from datetime import datetime import shutil import sys import subprocess +import threading signal_pipe_path = "/tmp/msv-webcam-signal-pipe" signal_pipe_msg = "msv-webcam-new-images" @@ -27,38 +29,79 @@ def convert_to_avif(input, output): #print(output) temp = subprocess.Popen(['sync',], stdout = subprocess.PIPE) -def process_new_images(source_dir, destination_dir_path): +def process_new_images(source_dir, destination_dir_path, min_size): images = os.listdir(source_dir) for image in images: if ((os.path.splitext(os.path.basename(os.path.normpath(image)))[1] != ".avif")): - converted_file_name = (os.path.splitext(os.path.basename(os.path.normpath(image)))[0] + ".avif") - converted_file_path = os.path.join("/tmp/", converted_file_name) + if(os.stat(os.path.join(source_dir, image)).st_size >= min_size): - convert_to_avif(os.path.join(source_dir, image), converted_file_path) + converted_file_name = (os.path.splitext(os.path.basename(os.path.normpath(image)))[0] + ".avif") + converted_file_path = os.path.join("/tmp/", converted_file_name) + + convert_to_avif(os.path.join(source_dir, image), converted_file_path) - copy_image(converted_file_path, os.path.join(destination_dir_path, converted_file_name)) + copy_image(converted_file_path, os.path.join(destination_dir_path, converted_file_name)) + + else: + print("Image " + str(image) + " too small.") + +def remove_old_image(path): + print("Removing following outdated image: " + str(path)) + os.remove(path) + +def watchdog(timeout, source_dir, destination_dir_path): + known_images = list() + + while True: + images = os.listdir(destination_dir_path) + for image in images: + if ((os.path.splitext(os.path.basename(os.path.normpath(image)))[1] == ".avif")): + current_image = (image, os.stat(os.path.join(destination_dir_path, image)).st_size) + if current_image in known_images: + print("This image is old: " + str(image)) + remove_old_image(os.path.join(destination_dir_path, image)) + + known_images.clear() + + images = os.listdir(destination_dir_path) + for image in images: + if ((os.path.splitext(os.path.basename(os.path.normpath(image)))[1] == ".avif")): + current_image = (image, os.stat(os.path.join(destination_dir_path, image)).st_size) + known_images.append(current_image) + + time.sleep(timeout) def main(): print("starting ...") source_dir = -1 destination_dir_path = -1 + timeout = -1 + min_size = -1 for argument in sys.argv: if argument.startswith('destination_dir'): destination_dir_path = argument.split("=")[1] if argument.startswith('source_dir'): source_dir = argument.split("=")[1] + if argument.startswith('timeout'): + timeout = int(argument.split("=")[1]) + if argument.startswith('min_size'): + min_size = int(argument.split("=")[1]) if ((source_dir == -1) or (destination_dir_path == -1)): print("Unable to parse config!") print("Example usage:") - print(" python msv_webcam_frontend.py source_dir=/tmp/msv_webcam_current destination_dir=www/ ") + print(" python msv_webcam_frontend.py source_dir=/tmp/msv_webcam_current destination_dir=www/camera/ timeout=300 min_size=100000") sys.exit(-1) - # process_new_images(source_dir, destination_dir_path) - # sys.exit(-1) + + watchdog_thread = threading.Thread(target=watchdog, args=((timeout, source_dir, destination_dir_path))) + watchdog_thread.start() + + #process_new_images(source_dir, destination_dir_path) + #sys.exit(-1) while True: try: @@ -73,10 +116,12 @@ def main(): #print("Received: " + str(message)) if(message == signal_pipe_msg): print("received signal about new images") - process_new_images(source_dir, destination_dir_path) - time.sleep(0.1) + process_new_images(source_dir, destination_dir_path, min_size) + time.sleep(1) except Exception as e: print(e) + watchdog_thread.join() + if __name__ == "__main__": main() \ No newline at end of file diff --git a/www/data/camera/msvcamN.avif b/www/data/camera/msvcamN.avif new file mode 100644 index 0000000..89d1525 Binary files /dev/null and b/www/data/camera/msvcamN.avif differ diff --git a/www/data/camera/msvcamS.avif b/www/data/camera/msvcamS.avif new file mode 100644 index 0000000..0619adf Binary files /dev/null and b/www/data/camera/msvcamS.avif differ diff --git a/www/data/msvcamN.avif b/www/data/msvcamN.avif deleted file mode 100644 index d6abad1..0000000 Binary files a/www/data/msvcamN.avif and /dev/null differ diff --git a/www/data/msvcamS.avif b/www/data/msvcamS.avif deleted file mode 100644 index e089632..0000000 Binary files a/www/data/msvcamS.avif and /dev/null differ diff --git a/www/index.html b/www/index.html index fb54878..a690fe1 100644 --- a/www/index.html +++ b/www/index.html @@ -25,12 +25,12 @@