]> www.vanbest.org Git - motioneye-debian/commitdiff
added MJPG_CLIENT_IDLE_TIMEOUT setting
authorCalin Crisan <ccrisan@gmail.com>
Sun, 31 May 2015 12:44:18 +0000 (15:44 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sun, 31 May 2015 12:53:12 +0000 (15:53 +0300)
motioneye.py
settings_default.py
src/config.py
src/mjpgclient.py
src/motionctl.py

index 28dbf8a91be9b0151e7fa9b60fa34b81f676e628..13a479127a6de8c74dc0b3ced74ad7b03d4090bf 100755 (executable)
@@ -65,6 +65,7 @@ def _configure_settings():
     set_default_setting('THUMBNAILER_INTERVAL', 60)
     set_default_setting('REMOTE_REQUEST_TIMEOUT', 10)
     set_default_setting('MJPG_CLIENT_TIMEOUT', 10)
+    set_default_setting('MJPG_CLIENT_IDLE_TIMEOUT', 10)
     set_default_setting('PICTURE_CACHE_SIZE', 8)
     set_default_setting('PICTURE_CACHE_LIFETIME', 60)
     set_default_setting('SMB_SHARES', False)
@@ -341,7 +342,7 @@ def _start_motion():
         if ioloop._stopped:
             return
             
-        if not motionctl.running() and motionctl.started() and config.has_local_enabled_cameras():
+        if not motionctl.running() and motionctl.started() and config.get_enabled_local_motion_cameras():
             try:
                 logging.error('motion not running, starting it')
                 motionctl.start()
index 0c14b80b6d1c9af0c0d04f26e10dd0b4f906ff5e..3dc11d6b83172aa68ba260f930ec79ceb90245b5 100644 (file)
@@ -47,12 +47,15 @@ CLEANUP_INTERVAL = 43200
 # interval in seconds at which the thumbnail mechanism runs (set to 0 to disable) 
 THUMBNAILER_INTERVAL = 60
 
-# timeout in seconds to wait for responses when contacting a remote server
+# timeout in seconds when waiting for response from a remote motionEye server
 REMOTE_REQUEST_TIMEOUT = 10
 
-# timeout in seconds to wait for an access to a mjpg client before removing it
+# timeout in seconds when waiting for mjpg data from the motion daemon
 MJPG_CLIENT_TIMEOUT = 10
 
+# timeout in seconds after which an idle mjpg client is removed (set to 0 to disable)
+MJPG_CLIENT_IDLE_TIMEOUT = 10
+
 # the maximal number of entries per camera in the current pictures cache
 PICTURE_CACHE_SIZE = 8
 
index af8d1f20cc69c3088619fbf4c616fbe001e053f1..bbfc8680aa46707c328f2a447af219ee4b57f724 100644 (file)
@@ -205,13 +205,13 @@ def get_camera_ids():
     return filtered_camera_ids
 
 
-def has_local_enabled_cameras():
+def get_enabled_local_motion_cameras():
     if not get_main().get('@enabled'):
-        return False
+        return []
     
     camera_ids = get_camera_ids()
     cameras = [get_camera(camera_id) for camera_id in camera_ids]
-    return bool([c for c in cameras if c.get('@enabled') and utils.local_motion_camera(c)])
+    return [c for c in cameras if c.get('@enabled') and utils.local_motion_camera(c)]
 
 
 def get_network_shares():
index b103bd36ed424d92968152abfb87bc938f4afdff..d576d81b3341bc423385492902d191c1647fdce1 100644 (file)
@@ -72,8 +72,8 @@ class MjpgClient(iostream.IOStream):
                 logging.error('connection problem detected for mjpg client for camera %(camera_id)s on port %(port)s' % {
                         'port': self._port, 'camera_id': self._camera_id})
 
-                motionctl.stop() # this will close all the mjpg clients
-                motionctl.start()
+                motionctl.stop(invalidate=True) # this will close all the mjpg clients
+                motionctl.start(deferred=True)
 
             MjpgClient.last_erroneous_close_time = now
 
@@ -231,8 +231,8 @@ def _garbage_collector():
             logging.error('mjpg client timed out receiving data for camera %(camera_id)s on port %(port)s' % {
                     'camera_id': camera_id, 'port': port})
             
-            motionctl.stop() # this will close all the mjpg clients
-            motionctl.start()
+            motionctl.stop(invalidate=True) # this will close all the mjpg clients
+            motionctl.start(deferred=True)
             
             break
 
@@ -244,9 +244,9 @@ def _garbage_collector():
         delta = now - last_access
         delta = delta.days * 86400 + delta.seconds
         
-        if delta > settings.MJPG_CLIENT_TIMEOUT:
-            logging.debug('mjpg client for camera %(camera_id)s on port %(port)s timed out' % {
-                    'camera_id': camera_id, 'port': port})
+        if settings.MJPG_CLIENT_IDLE_TIMEOUT and delta > settings.MJPG_CLIENT_IDLE_TIMEOUT:
+            logging.debug('mjpg client for camera %(camera_id)s on port %(port)s has been idle for %(timeout)s seconds, removing it' % {
+                    'camera_id': camera_id, 'port': port, 'timeout': settings.MJPG_CLIENT_IDLE_TIMEOUT})
             
             client.close()
 
@@ -286,9 +286,16 @@ def get_jpg(camera_id):
     return MjpgClient.last_jpgs.get(camera_id)
 
 
-def close_all():
+def close_all(invalidate=False):
     for client in MjpgClient.clients.values():
         client.close()
+    
+    if invalidate:
+        MjpgClient.clients = {}
+        MjpgClient.last_jpgs = {}
+        MjpgClient.last_jpg_moment = {}
+        MjpgClient.last_access = {}
+        MjpgClient.last_erroneous_close_time = 0
 
 
 # schedule the garbage collector
index 1783fda68cc23ee43f59d92f8b4a29bdf67b8862..df27ae644a7ea4ac6318828b83d67d3e4663c6c3 100644 (file)
@@ -24,8 +24,10 @@ import subprocess
 import time
 
 from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest
+from tornado.ioloop import IOLoop
 
 import config
+import mjpgclient
 import powerctl
 import settings
 import utils
@@ -80,12 +82,16 @@ def _disable_initial_motion_detection():
             set_motion_detection(camera_id, False)
 
 
-def start():
+def start(deferred=False):
+    if deferred:
+        return IOLoop.instance().add_callback(start, deferred=False)
+
     global _started
     
     _started = True
     
-    if running() or not config.has_local_enabled_cameras():
+    enabled_local_motion_cameras = config.get_enabled_local_motion_cameras()
+    if running() or not enabled_local_motion_cameras:
         return
     
     logging.debug('starting motion')
@@ -131,11 +137,15 @@ def start():
         f.write(str(pid) + '\n')
     
     _disable_initial_motion_detection()
+    
+    # if mjpg client idle timeout is disabled, create mjpg clients for all cameras by default
+    if not settings.MJPG_CLIENT_IDLE_TIMEOUT:
+        logging.debug('creating default mjpg clients for local cameras')
+        for camera in enabled_local_motion_cameras:
+            mjpgclient.get_jpg(camera['@id'])
 
 
-def stop():
-    import mjpgclient
-    
+def stop(invalidate=False):
     global _started
     
     _started = False
@@ -145,7 +155,7 @@ def stop():
     
     logging.debug('stopping motion')
 
-    mjpgclient.close_all()
+    mjpgclient.close_all(invalidate=invalidate)
     
     pid = _get_pid()
     if pid is not None: