_MAIN_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, 'motion.conf')
_CAMERA_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, _CAMERA_CONFIG_FILE_NAME)
+_main_config_cache = None
+_camera_config_cache = None
+_camera_ids_cache = None
+
def get_main(as_lines=False):
- # TODO use a cache
+ global _main_config_cache
+
+ if not as_lines and _main_config_cache:
+ return _main_config_cache
config_file_path = os.path.join(settings.PROJECT_PATH, _MAIN_CONFIG_FILE_PATH)
data = _conf_to_dict(lines, list_names=['thread'])
_set_default_motion(data)
+ _main_config_cache = data
+
return data
def set_main(data):
- # TODO use a cache
+ global _main_config_cache
_set_default_motion(data)
finally:
file.close()
-
+
+ _main_config_cache = data
+
return data
def get_camera_ids():
+ global _camera_ids_cache
+
+ if _camera_ids_cache:
+ return _camera_ids_cache
+
config_path = settings.CONF_PATH
logging.debug('listing config dir %(path)s...' % {'path': config_path})
camera_ids.sort()
+ _camera_ids_cache = camera_ids
+
return camera_ids
def get_camera(camera_id, as_lines=False):
- # TODO use a cache
+ global _camera_config_cache
+
+ if not as_lines and _camera_config_cache and camera_id in _camera_config_cache:
+ return _camera_config_cache[camera_id]
camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id}
_set_default_motion_camera(data)
+ if _camera_config_cache is None:
+ _camera_config_cache = {}
+
+ _camera_config_cache[camera_id] = data
+
return data
def set_camera(camera_id, data):
- # TODO use a cache
+ global _camera_config_cache
if data['@proto'] == 'v4l2':
_set_default_motion_camera(data)
finally:
file.close()
+
+ if _camera_config_cache is None:
+ _camera_config_cache = {}
+
+ _camera_config_cache[camera_id] = data
return data
def add_camera(device_details):
- # TODO use a cache
+ global _camera_ids
+
+ _camera_ids = None
# determine the last camera id
camera_ids = get_camera_ids()
def rem_camera(camera_id):
- # TODO use a cache
-
+ global _camera_ids
+
+ _camera_ids = None
+
camera_config_name = _CAMERA_CONFIG_FILE_NAME % {'id': camera_id}
camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id}
import template
+def log_request(handler):
+ if handler.get_status() < 400:
+ log_method = logging.debug
+
+ elif handler.get_status() < 500:
+ log_method = logging.warning
+
+ else:
+ log_method = logging.error
+
+ request_time = 1000.0 * handler.request.request_time()
+ log_method("%d %s %.2fms", handler.get_status(),
+ handler._request_summary(), request_time)
+
+
application = Application(
[
(r'^/$', handlers.MainHandler),
(r'^/movie/(?P<camera_id>\d+)/(?P<op>download)/(?P<filename>.+)/?$', handlers.MovieHandler),
],
debug=settings.DEBUG,
- log_function=logging.debug,
+ log_function=log_request,
static_path=settings.STATIC_PATH,
static_url_prefix=settings.STATIC_URL
)