From e8bb476cd6f5af4e765009ec7527c2b23bb6ca6c Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 8 Mar 2014 11:20:04 +0200 Subject: [PATCH] added a current picture cache timeout; settings are no longer mandatory --- motioneye.py | 159 +++++++++++++++++++++++++------------------- settings_default.py | 3 + src/mediafiles.py | 11 ++- 3 files changed, 102 insertions(+), 71 deletions(-) diff --git a/motioneye.py b/motioneye.py index 8cb916b..746ea3b 100755 --- a/motioneye.py +++ b/motioneye.py @@ -27,11 +27,99 @@ import sys import settings -sys.path.append(os.path.join(settings.PROJECT_PATH, 'src')) +sys.path.append(os.path.join(getattr(settings, 'PROJECT_PATH', os.path.dirname(sys.argv[0])), 'src')) VERSION = '0.10' +def _configure_settings(): + def set_default_setting(name, value): + if not hasattr(settings, name): + setattr(settings, name, value) + + set_default_setting('PROJECT_PATH', os.path.dirname(sys.argv[0])) + set_default_setting('TEMPLATE_PATH', os.path.join(settings.PROJECT_PATH, 'templates')) # @UndefinedVariable + set_default_setting('STATIC_PATH', os.path.join(settings.PROJECT_PATH, 'static')) # @UndefinedVariable + set_default_setting('STATIC_URL', '/static/') + set_default_setting('CONF_PATH', os.path.join(settings.PROJECT_PATH, 'conf')) # @UndefinedVariable + set_default_setting('RUN_PATH', os.path.join(settings.PROJECT_PATH, 'run')) # @UndefinedVariable + set_default_setting('REPO', ('ccrisan', 'motioneye')) + set_default_setting('LOG_LEVEL', logging.INFO) + set_default_setting('LISTEN', '0.0.0.0') + set_default_setting('PORT', 8765) + set_default_setting('MOTION_CHECK_INTERVAL', 10) + set_default_setting('CLEANUP_INTERVAL', 43200) + set_default_setting('THUMBNAILER_INTERVAL', 60) + set_default_setting('REMOTE_REQUEST_TIMEOUT', 10) + set_default_setting('MJPG_CLIENT_TIMEOUT', 10) + set_default_setting('PICTURE_CACHE_SIZE', 8) + set_default_setting('PICTURE_CACHE_LIFETIME', 60) + + length = len(sys.argv) - 1 + for i in xrange(length): + arg = sys.argv[i + 1] + + if not arg.startswith('--'): + continue + + next_arg = None + if i < length - 1: + next_arg = sys.argv[i + 2] + + name = arg[2:].upper().replace('-', '_') + + if name == 'HELP': + _print_help() + sys.exit(0) + + if hasattr(settings, name): + curr_value = getattr(settings, name) + + if next_arg.lower() == 'debug': + next_arg = logging.DEBUG + + elif next_arg.lower() == 'info': + next_arg = logging.INFO + + elif next_arg.lower() == 'warn': + next_arg = logging.WARN + + elif next_arg.lower() == 'error': + next_arg = logging.ERROR + + elif next_arg.lower() == 'fatal': + next_arg = logging.FATAL + + elif next_arg.lower() == 'true': + next_arg = True + + elif next_arg.lower() == 'false': + next_arg = False + + elif isinstance(curr_value, int): + next_arg = int(next_arg) + + elif isinstance(curr_value, float): + next_arg = float(next_arg) + + setattr(settings, name, next_arg) + + else: + return arg[2:] + + try: + os.makedirs(settings.CONF_PATH) + + except: + pass + + try: + os.makedirs(settings.RUN_PATH) + + except: + pass + + def _test_requirements(): try: @@ -132,72 +220,6 @@ def _configure_logging(): format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') -def _configure_settings(): - length = len(sys.argv) - 1 - for i in xrange(length): - arg = sys.argv[i + 1] - - if not arg.startswith('--'): - continue - - next_arg = None - if i < length - 1: - next_arg = sys.argv[i + 2] - - name = arg[2:].upper().replace('-', '_') - - if name == 'HELP': - _print_help() - sys.exit(0) - - if hasattr(settings, name): - curr_value = getattr(settings, name) - - if next_arg.lower() == 'debug': - next_arg = logging.DEBUG - - elif next_arg.lower() == 'info': - next_arg = logging.INFO - - elif next_arg.lower() == 'warn': - next_arg = logging.WARN - - elif next_arg.lower() == 'error': - next_arg = logging.ERROR - - elif next_arg.lower() == 'fatal': - next_arg = logging.FATAL - - elif next_arg.lower() == 'true': - next_arg = True - - elif next_arg.lower() == 'false': - next_arg = False - - elif isinstance(curr_value, int): - next_arg = int(next_arg) - - elif isinstance(curr_value, float): - next_arg = float(next_arg) - - setattr(settings, name, next_arg) - - else: - return arg[2:] - - try: - os.makedirs(settings.CONF_PATH) - - except: - pass - - try: - os.makedirs(settings.RUN_PATH) - - except: - pass - - def _print_help(): print('Usage: ' + sys.argv[0] + ' [option1 value1] ...') print('available options: ') @@ -273,10 +295,11 @@ def _start_thumbnailer(): if __name__ == '__main__': + cmd = _configure_settings() + if not _test_requirements(): sys.exit(-1) - cmd = _configure_settings() _configure_signals() _configure_logging() diff --git a/settings_default.py b/settings_default.py index c88f9f4..c753921 100644 --- a/settings_default.py +++ b/settings_default.py @@ -46,3 +46,6 @@ MJPG_CLIENT_TIMEOUT = 10 # the maximal number of entries per camera in the current pictures cache PICTURE_CACHE_SIZE = 8 + +# the number of seconds that a cached picture is valid +PICTURE_CACHE_LIFETIME = 60 diff --git a/src/mediafiles.py b/src/mediafiles.py index 4a046b1..d3b5ab0 100644 --- a/src/mediafiles.py +++ b/src/mediafiles.py @@ -36,7 +36,7 @@ _PICTURE_EXTS = ['.jpg'] _MOVIE_EXTS = ['.avi', '.mp4'] # a dictionary indexed by camera_id containing -# tuples of (sequence, width, content) +# tuples of (moment, sequence, width, content) _current_pictures_cache = {} # a cache list of paths to movies without preview @@ -387,15 +387,20 @@ def set_picture_cache(camera_id, sequence, width, content): if len(cache) >= settings.PICTURE_CACHE_SIZE: cache.pop(0) # drop the first item - cache.append((sequence, width, content)) + cache.append((datetime.datetime.utcnow(), sequence, width, content)) def get_picture_cache(camera_id, sequence, width): global _current_pictures_cache cache = _current_pictures_cache.setdefault(camera_id, []) + now = datetime.datetime.utcnow() - for (seq, w, content) in cache: + for (moment, seq, w, content) in cache: + delta = now - moment + if delta.days * 86400 + delta.seconds > settings.PICTURE_CACHE_LIFETIME: + continue + if (seq >= sequence) and ((width is w is None) or (width >= w)): return content -- 2.39.5