only use images that have a minimum size and delete outdated images

This commit is contained in:
Hendrik Schutter 2023-07-10 22:34:51 +02:00
parent 9290cf195b
commit 52ddfe1f59
9 changed files with 95 additions and 15 deletions

View File

@ -7,7 +7,7 @@
- import `msv_webcam_frontend.py` - import `msv_webcam_frontend.py`
- `chmod +x /opt/msv-webcam-frontend/msv_webcam_frontend.py` - `chmod +x /opt/msv-webcam-frontend/msv_webcam_frontend.py`
- `nano /etc/systemd/system/msv-webcam-frontend.service` - `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` - `systemctl daemon-reload && systemctl enable --now msv-webcam-frontend.service`
- copy WebApp to `/var/www/html/msv-buehl-moos.de/webcam/` - copy WebApp to `/var/www/html/msv-buehl-moos.de/webcam/`

View File

@ -10,7 +10,7 @@ Group=wwwrun
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
WorkingDirectory=/opt/msv-webcam-frontend/ 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] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -6,9 +6,11 @@
import os import os
import errno import errno
import time import time
from datetime import datetime
import shutil import shutil
import sys import sys
import subprocess import subprocess
import threading
signal_pipe_path = "/tmp/msv-webcam-signal-pipe" signal_pipe_path = "/tmp/msv-webcam-signal-pipe"
signal_pipe_msg = "msv-webcam-new-images" signal_pipe_msg = "msv-webcam-new-images"
@ -27,38 +29,79 @@ def convert_to_avif(input, output):
#print(output) #print(output)
temp = subprocess.Popen(['sync',], stdout = subprocess.PIPE) 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) images = os.listdir(source_dir)
for image in images: for image in images:
if ((os.path.splitext(os.path.basename(os.path.normpath(image)))[1] != ".avif")): 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") if(os.stat(os.path.join(source_dir, image)).st_size >= min_size):
converted_file_path = os.path.join("/tmp/", converted_file_name)
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(): def main():
print("starting ...") print("starting ...")
source_dir = -1 source_dir = -1
destination_dir_path = -1 destination_dir_path = -1
timeout = -1
min_size = -1
for argument in sys.argv: for argument in sys.argv:
if argument.startswith('destination_dir'): if argument.startswith('destination_dir'):
destination_dir_path = argument.split("=")[1] destination_dir_path = argument.split("=")[1]
if argument.startswith('source_dir'): if argument.startswith('source_dir'):
source_dir = argument.split("=")[1] 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)): if ((source_dir == -1) or (destination_dir_path == -1)):
print("Unable to parse config!") print("Unable to parse config!")
print("Example usage:") 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) 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: while True:
try: try:
@ -73,10 +116,12 @@ def main():
#print("Received: " + str(message)) #print("Received: " + str(message))
if(message == signal_pipe_msg): if(message == signal_pipe_msg):
print("received signal about new images") print("received signal about new images")
process_new_images(source_dir, destination_dir_path) process_new_images(source_dir, destination_dir_path, min_size)
time.sleep(0.1) time.sleep(1)
except Exception as e: except Exception as e:
print(e) print(e)
watchdog_thread.join()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -25,12 +25,12 @@
<div class="w3-panel w3-card-4 w3-white w3-padding w3-center"> <div class="w3-panel w3-card-4 w3-white w3-padding w3-center">
<h3 class="w3-container w3-left cameratext">Blick auf die Piste</h3> <h3 class="w3-container w3-left cameratext">Blick auf die Piste</h3>
<img src="data/msvcamN.avif" class="w3-center current_webcam_image" style="width:100%;" alt="Bild der Webcam mit Blick auf die Piste"> <img src="data/camera/msvcamN.avif" class="w3-center current_webcam_image" style="width:100%;" alt="Bild der Webcam mit Blick auf die Piste">
</div> </div>
<div class="w3-panel w3-card-4 w3-white w3-padding w3-center"> <div class="w3-panel w3-card-4 w3-white w3-padding w3-center">
<h3 class="w3-container w3-left cameratext">Blick nach Süden</h3> <h3 class="w3-container w3-left cameratext">Blick nach Süden</h3>
<img src="data/msvcamS.avif" class="w3-center current_webcam_image" style="width:100%;" alt="Bild der Webcam mit Blick nach Süden"> <img src="data/camera/msvcamS.avif" class="w3-center current_webcam_image" style="width:100%;" alt="Bild der Webcam mit Blick nach Süden">
</div> </div>
<div class="w3-panel w3-card-4 w3-white w3-padding w3-center"> <div class="w3-panel w3-card-4 w3-white w3-padding w3-center">
@ -42,7 +42,7 @@
<div class="w3-panel w3-card w3-dark-gray"> <div class="w3-panel w3-card w3-dark-gray">
<div class="w3-center-align w3-text-black infobox"> <div class="w3-center-align w3-text-black infobox">
<a style="text-decoration: none" href="mailto:pressewart@msv-buehl-moos.de?subject=MSV%20Webcam"><h6>pressewart@msv-buehl-moos.de</h6></a> <a style="text-decoration: none" href="mailto:pressewart@msv-buehl-moos.de?subject=MSV%20Webcam"><h6>pressewart@msv-buehl-moos.de</h6></a>
<p>Last update: 2023/07/07<p> <p>Last update: 2023/07/10<p>
<a style="text-decoration: none" href="https://git.mosad.xyz/localhorst/msv-webcam-frontend" target="_blank" rel="noopener noreferrer"><h6>View code in GIT</h6></a> <a style="text-decoration: none" href="https://git.mosad.xyz/localhorst/msv-webcam-frontend" target="_blank" rel="noopener noreferrer"><h6>View code in GIT</h6></a>
</div> </div>
</div> </div>

35
www/js/webcam.js Normal file
View File

@ -0,0 +1,35 @@
const images = document.getElementsByClassName('current_webcam_image');
const reload_interval = setInterval(reloadImages, 10000);
window.onload = function() {
reloadImages();
};
function checkImageSource(image_url) {
var http_request = new XMLHttpRequest();
http_request.open('HEAD', image_url, false);
http_request.send();
return http_request.status;
}
function reloadImages() {
console.log('reload in progress');
Array.prototype.forEach.call(images, function(image) {
if (checkImageSource(image.src) == 200) {
if (!image.src.includes('?')) {
image.src = `${image.src}?${Date.now()}`;
} else {
image.src = image.src.slice(0, image.src.indexOf('?') + 1) + Date.now();
}
} else {
console.log('unable to find image on server');
image.alt = "Webcam nicht erreichbar."
}
});
console.log('reload finished');
}