diff --git a/.gitignore b/.gitignore index e8ecc57..8ca6116 100644 --- a/.gitignore +++ b/.gitignore @@ -189,3 +189,4 @@ cython_debug/ # .nfs files are created when an open file is removed but is still being accessed .nfs* +test_images/ \ No newline at end of file diff --git a/README.md b/README.md index 3a9e278..e43e5dc 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,9 @@ # msv-webcam-backend - -useradd --system msvwebcambackend -m -passwd -l msvwebcambackend - - - `mkdir /opt/msv-webcam-backend/` - `cd /opt/msv-webcam-backend/` -- import `msv_webcam_backend.py` and `config.py` -- Set the constants in `config.py` +- import `msv_webcam_backend.py` - `chmod +x /opt/msv-webcam-backend/msv_webcam_backend.py` -- `chown -R msvwebcambackend /opt/msv-webcam-backend/` - `nano /etc/systemd/system/msv-webcam-backend.service` +- create dst dir `mkdir -p /var/www/html/msv-buehl-moos.de/webcam/data` - `systemctl daemon-reload && systemctl enable --now msv-webcam-backend.service` - - -mkdir /var/www/html/msv-buehl-moos.de/webcam/ - -chown -R wwwrun:wwwrun /var/www/html/msv-buehl-moos.de/ - - - - - location /webcam { - root /var/www/html/msv-buehl-moos.de; - index index.html; - } diff --git a/config.py b/config.py deleted file mode 100644 index e2c1a84..0000000 --- a/config.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" Author: Hendrik Schutter, mail@hendrikschutter.com -""" - -image_search_path = "./test_images/" -destination_image_full_path = "./www/data/still.jpg" -update_interval = 10 # 10sec diff --git a/msv-webcam-backend.service b/msv-webcam-backend.service index fdf0d64..3f0906d 100644 --- a/msv-webcam-backend.service +++ b/msv-webcam-backend.service @@ -4,11 +4,13 @@ After=syslog.target After=network.target [Service] -RestartSec=2s -Type=oneshot -User=msvwebcambackend +Type=simple +User=root +Group=root +Restart=on-failure +RestartSec=5s WorkingDirectory=/opt/msv-webcam-backend/ -ExecStart=/usr/bin/python3 /opt/msv-webcam-backend/msv_webcam_backend.py +ExecStart=/usr/bin/python3 /opt/msv-webcam-backend/msv_webcam_backend.py interval=10 search_dir=/home/dockeruser/ftps_server/files/msvcamS search_dir=/home/dockeruser/ftps_server/files/msvcamN destination_dir=/var/www/html/msv-buehl-moos.de/webcam/data/ destination_owner=wwwrun [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/msv_webcam_backend.py b/msv_webcam_backend.py index 42893a5..e70cedb 100644 --- a/msv_webcam_backend.py +++ b/msv_webcam_backend.py @@ -3,53 +3,84 @@ """ Author: Hendrik Schutter, mail@hendrikschutter.com """ -import config import glob import os import time import shutil +import sys -def copy_image(src): - shutil.copy(src, config.destination_image_full_path) - os.chmod(config.destination_image_full_path, 644) +def copy_image(src, destination_dir_path, destination_owner): + # print("src: " + str(src)) + # print("dst: " + str(destination_dir_path)) + shutil.copyfile(src, destination_dir_path) + os.chmod(destination_dir_path, 0o0644) + shutil.chown(destination_dir_path, destination_owner) def main(): print("starting ...") - newest_file_path = "" - newest_file_size = 0 + update_interval = -1 + destination_dir_path = -1 + destination_owner = -1 + search_dirs = list() + + for argument in sys.argv: + if argument.startswith('interval'): + update_interval = int(argument.split("=")[1]) + if argument.startswith('destination_dir'): + destination_dir_path = argument.split("=")[1] + if argument.startswith('destination_owner'): + destination_owner = argument.split("=")[1] + if argument.startswith('search_dir'): + tmp_search_dir = { + "search_dir_path" : argument.split("=")[1], + "newest_file_path" : "", + "newest_file_size" : 0 + } + search_dirs.append(tmp_search_dir) + + if ((update_interval == -1) or (destination_dir_path == -1) or (destination_owner == -1) or (len(search_dirs) == 0)): + print("Unable to parse config!") + print("Example usage:") + print(" python msv_webcam_backend.py interval=10 search_dir=test_images/msvcamS search_dir=test_images/msvcamN destination_dir=www/ destination_owner=hendrik") + sys.exit(-1) while True: - - while True: + for search_dir in search_dirs: try: -# print(config.image_search_path) - list_of_files = glob.glob(config.image_search_path+"/**/*.jpg", recursive=True) - # print(list_of_files) + # print("\nSearch dir: " + str(search_dir["search_dir_path"])) + list_of_files = glob.glob(search_dir["search_dir_path"]+"/**/*.jpg", recursive=True) + # print(list_of_files) newest_file_path_tmp = max(list_of_files, key=os.path.getctime) newest_file_size_tmp = os.stat(newest_file_path_tmp).st_size + list_of_files.clear() - if (newest_file_path_tmp == newest_file_path) and (newest_file_size_tmp == newest_file_size): + if (newest_file_path_tmp == search_dir["newest_file_path"]) and (newest_file_size_tmp == search_dir["newest_file_size"]): # already found the newest file print("no newer file found") - break + else: + time.sleep(1) # wait 1sec to see if the upload is still in progress - time.sleep(1) # wait 1sec to see if the upload is still in progress + list_of_files = glob.glob(search_dir["search_dir_path"]+"/**/*.jpg", recursive=True) + newest_file_path_tmp_delayed = max(list_of_files, key=os.path.getctime) + newest_file_size_tmp_delayed = os.stat(newest_file_path_tmp_delayed).st_size + list_of_files.clear() - list_of_files = glob.glob(config.image_search_path+"/**/*.jpg", recursive=True) - newest_file_path = max(list_of_files, key=os.path.getctime) - newest_file_size = os.stat(newest_file_path).st_size - - if (newest_file_path_tmp == newest_file_path) and (newest_file_size_tmp == newest_file_size): - print("Found new file:") - print("Name: "+str(newest_file_path)) - print("Size: " + str(newest_file_size)) - copy_image(newest_file_path) - break + if (newest_file_path_tmp == newest_file_path_tmp_delayed) and (newest_file_size_tmp == newest_file_size_tmp_delayed): + search_dir["newest_file_path"] = newest_file_path_tmp_delayed + search_dir["newest_file_size"] = newest_file_size_tmp_delayed + print("Found new file:") + print("Name: " + str(search_dir["newest_file_path"])) + print("Size: " + str(search_dir["newest_file_size"])) + dst_file_tmp = os.path.join(destination_dir_path, (os.path.basename(os.path.normpath(search_dir["search_dir_path"])) + os.path.splitext(search_dir["newest_file_path"])[1])) + copy_image(search_dir["newest_file_path"], dst_file_tmp, destination_owner) + #break + else: + print("Upload not finished yet!") except Exception as e: print(e) #break - time.sleep(config.update_interval) + time.sleep(update_interval) if __name__ == "__main__": main() diff --git a/www/data/msvcamN.jpg b/www/data/msvcamN.jpg new file mode 100644 index 0000000..a7a69f5 Binary files /dev/null and b/www/data/msvcamN.jpg differ diff --git a/www/data/msvcamS.jpg b/www/data/msvcamS.jpg new file mode 100644 index 0000000..9cd2364 Binary files /dev/null and b/www/data/msvcamS.jpg differ diff --git a/www/index.html b/www/index.html index 087b180..33d6cf4 100644 --- a/www/index.html +++ b/www/index.html @@ -8,7 +8,9 @@

You are connected!

-still webcam image +msvcamN still webcam image + +msvcamS still webcam image