]> www.vanbest.org Git - motioneye-debian/commitdiff
added a current picture cache timeout; settings are no longer mandatory
authorCalin Crisan <ccrisan@gmail.com>
Sat, 8 Mar 2014 09:20:04 +0000 (11:20 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 8 Mar 2014 09:20:04 +0000 (11:20 +0200)
motioneye.py
settings_default.py
src/mediafiles.py

index 8cb916b405a52794b33424cadb272bade19db535..746ea3b861ab21a874ef5411be43175707ed1dfa 100755 (executable)
@@ -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()
     
index c88f9f4f344a05ab48cadb9ca3c4647945353cc3..c753921ab334ced94c37307382dcf1c623d3aa7f 100644 (file)
@@ -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
index 4a046b1872dcf63b188c7dd26dd275c9084b7c8b..d3b5ab00c41dfa20bdbea19ec42e8759437c143d 100644 (file)
@@ -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