def cleanup_media(media_type): logging.debug('cleaning up %(media_type)ss...' % {'media_type': media_type}) if media_type == 'picture': exts = _PICTURE_EXTS elif media_type == 'movie': exts = _MOVIE_EXTS + ['.thumb'] for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue preserve_media = camera_config.get('@preserve_%(media_type)ss' % {'media_type': media_type}, 0) if preserve_media == 0: return # preserve forever still_images_enabled = bool( ((camera_config['emulate_motion'] or camera_config['output_pictures']) and camera_config['picture_filename']) or (camera_config['snapshot_interval'] and camera_config['snapshot_filename'])) movies_enabled = camera_config['ffmpeg_output_movies'] if media_type == 'picture' and not still_images_enabled: continue # only cleanup pictures for cameras with still images enabled elif media_type == 'movie' and not movies_enabled: continue # only cleanup movies for cameras with movies enabled preserve_moment = datetime.datetime.now() - datetime.timedelta(days=preserve_media) target_dir = camera_config.get('target_dir') _remove_older_files(target_dir, preserve_moment, exts=exts)
def get_jpg(camera_id): if not motionctl.running(): return None if camera_id not in MjpgClient.clients: # mjpg client not started yet for this camera logging.debug('creating mjpg client for camera %(camera_id)s' % { 'camera_id': camera_id}) camera_config = config.get_camera(camera_id) if not camera_config['@enabled'] or not utils.local_motion_camera(camera_config): logging.error('could not start mjpg client for camera id %(camera_id)s: not enabled or not local' % { 'camera_id': camera_id}) return None port = camera_config['stream_port'] username, password = None, None if camera_config.get('stream_auth_method') > 0: username, password = camera_config.get('stream_authentication', ':').split(':') client = MjpgClient(camera_id, port, username, password) client.connect() MjpgClient.last_access[camera_id] = datetime.datetime.utcnow() return MjpgClient.last_jpgs.get(camera_id)
def get_jpg(camera_id): if camera_id not in MjpgClient.clients: # mjpg client not started yet for this camera logging.debug('creating mjpg client for camera %(camera_id)s' % {'camera_id': camera_id}) camera_config = config.get_camera(camera_id) if not camera_config['@enabled'] or not utils.local_motion_camera( camera_config): logging.error( 'could not start mjpg client for camera id %(camera_id)s: not enabled or not local' % {'camera_id': camera_id}) return None port = camera_config['stream_port'] username, password = None, None if camera_config.get('stream_auth_method') > 0: username, password = camera_config.get('stream_authentication', ':').split(':') client = MjpgClient(camera_id, port, username, password) client.connect() MjpgClient.last_access[camera_id] = datetime.datetime.utcnow() return MjpgClient.last_jpgs.get(camera_id)
def get_jpg(camera_id): if camera_id not in MjpgClient.clients: # mjpg client not started yet for this camera logging.debug("creating mjpg client for camera %(camera_id)s" % {"camera_id": camera_id}) camera_config = config.get_camera(camera_id) if not camera_config["@enabled"] or not utils.local_motion_camera(camera_config): logging.error( "could not start mjpg client for camera id %(camera_id)s: not enabled or not local" % {"camera_id": camera_id} ) return None port = camera_config["stream_port"] username, password = None, None if camera_config.get("stream_auth_method") > 0: username, password = camera_config.get("stream_authentication", ":").split(":") client = MjpgClient(camera_id, port, username, password) client.connect() MjpgClient.last_access[camera_id] = datetime.datetime.utcnow() return MjpgClient.last_jpgs.get(camera_id)
def get_jpg(camera_id): if camera_id not in MjpgClient.clients: # mjpg client not started yet for this camera logging.debug('creating mjpg client for camera %(camera_id)s' % { 'camera_id': camera_id}) camera_config = config.get_camera(camera_id) if not camera_config['@enabled'] or not utils.local_motion_camera(camera_config): logging.error('could not start mjpg client for camera id %(camera_id)s: not enabled or not local' % { 'camera_id': camera_id}) return None port = camera_config['stream_port'] username, password = None, None auth_mode = None if camera_config.get('stream_auth_method') > 0: username, password = camera_config.get('stream_authentication', ':').split(':') auth_mode = 'digest' if camera_config.get('stream_auth_method') > 1 else 'basic' client = MjpgClient(camera_id, port, username, password, auth_mode) client.connect() MjpgClient.clients[camera_id] = client client = MjpgClient.clients[camera_id] client._last_access = time.time() return client._last_jpg
def make_next_movie_preview(): global _previewless_movie_files logging.debug('making preview for the next movie...') if _previewless_movie_files: (camera_config, path) = _previewless_movie_files.pop(0) make_movie_preview(camera_config, path) else: logging.debug('gathering movies without preview...') count = 0 for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue target_dir = camera_config['target_dir'] for (full_path, st) in _list_media_files(target_dir, _MOVIE_EXTS): # @UnusedVariable if os.path.exists(full_path + '.thumb'): continue logging.debug('found a movie without preview: %(path)s' % { 'path': full_path}) _previewless_movie_files.append((camera_config, full_path)) count += 1 logging.debug('found %(count)d movies without preview' % {'count': count}) if count: make_next_movie_preview()
def _disable_initial_motion_detection(): for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue if not camera_config['@motion_detection']: logging.debug('motion detection disabled by config for camera with id %s' % camera_id) set_motion_detection(camera_id, False)
def _check_ws(): # schedule the next call ioloop = tornado.ioloop.IOLoop.instance() ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws) if not motionctl.running(): return now = datetime.datetime.now() for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue working_schedule = camera_config.get('@working_schedule') motion_detection = camera_config.get('@motion_detection') working_schedule_type = camera_config.get( '@working_schedule_type') or 'outside' if not working_schedule: # working schedule disabled, motion detection left untouched continue if not motion_detection: # motion detection explicitly disabled continue now_during = _during_working_schedule(now, working_schedule) must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside') currently_enabled = motionctl.get_motion_detection(camera_id) if currently_enabled is None: # could not detect current status logging.warn( 'skipping motion detection status update for camera with id %(id)s' % {'id': camera_id}) continue if currently_enabled and not must_be_enabled: logging.debug( 'must disable motion detection for camera with id %(id)s (%(what)s working schedule)' % { 'id': camera_id, 'what': working_schedule_type }) motionctl.set_motion_detection(camera_id, False) elif not currently_enabled and must_be_enabled: logging.debug( 'must enable motion detection for camera with id %(id)s (%(what)s working schedule)' % { 'id': camera_id, 'what': working_schedule_type }) motionctl.set_motion_detection(camera_id, True)
def _check_ws(): # schedule the next call io_loop = IOLoop.instance() io_loop.add_timeout(datetime.timedelta(seconds=10), _check_ws) if not motionctl.running(): return def on_motion_detection_status(camera_id, must_be_enabled, working_schedule_type, enabled=None, error=None): if error: # could not detect current status return logging.warn("skipping motion detection status update for camera with id %(id)s" % {"id": camera_id}) if enabled and not must_be_enabled: logging.debug( "must disable motion detection for camera with id %(id)s (%(what)s working schedule)" % {"id": camera_id, "what": working_schedule_type} ) motionctl.set_motion_detection(camera_id, False) elif not enabled and must_be_enabled: logging.debug( "must enable motion detection for camera with id %(id)s (%(what)s working schedule)" % {"id": camera_id, "what": working_schedule_type} ) motionctl.set_motion_detection(camera_id, True) now = datetime.datetime.now() for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue working_schedule = camera_config.get("@working_schedule") motion_detection = camera_config.get("@motion_detection") working_schedule_type = camera_config.get("@working_schedule_type") or "outside" if not working_schedule: # working schedule disabled, motion detection left untouched continue if not motion_detection: # motion detection explicitly disabled continue now_during = _during_working_schedule(now, working_schedule) must_be_enabled = (now_during and working_schedule_type == "during") or ( not now_during and working_schedule_type == "outside" ) motionctl.get_motion_detection( camera_id, functools.partial(on_motion_detection_status, camera_id, must_be_enabled, working_schedule_type) )
def thread_id_to_camera_id(thread_id): # find the corresponding camera_id # (which can be different from thread_id) camera_ids = config.get_camera_ids() tid = 0 for cid in camera_ids: camera_config = config.get_camera(cid) if utils.local_motion_camera(camera_config): tid += 1 if tid == thread_id: return cid return None
def camera_id_to_thread_id(camera_id): # find the corresponding thread_id # (which can be different from camera_id) camera_ids = config.get_camera_ids() thread_id = 0 for cid in camera_ids: camera_config = config.get_camera(cid) if utils.local_motion_camera(camera_config): thread_id += 1 if cid == camera_id: return thread_id or None return None
def _check_ws(): # schedule the next call ioloop = tornado.ioloop.IOLoop.instance() ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws) if not motionctl.running(): return now = datetime.datetime.now() for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue working_schedule = camera_config.get('@working_schedule') motion_detection = camera_config.get('@motion_detection') working_schedule_type = camera_config.get('@working_schedule_type') or 'outside' if not working_schedule: # working schedule disabled, motion detection left untouched continue if not motion_detection: # motion detection explicitly disabled continue now_during = _during_working_schedule(now, working_schedule) must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside') currently_enabled = motionctl.get_motion_detection(camera_id) if currently_enabled is None: # could not detect current status logging.warn('skipping motion detection status update for camera with id %(id)s' % {'id': camera_id}) continue if currently_enabled and not must_be_enabled: logging.debug('must disable motion detection for camera with id %(id)s (%(what)s working schedule)' % { 'id': camera_id, 'what': working_schedule_type}) motionctl.set_motion_detection(camera_id, False) elif not currently_enabled and must_be_enabled: logging.debug('must enable motion detection for camera with id %(id)s (%(what)s working schedule)' % { 'id': camera_id, 'what': working_schedule_type}) motionctl.set_motion_detection(camera_id, True)
def cleanup_media(media_type): logging.debug('cleaning up %(media_type)ss...' % {'media_type': media_type}) if media_type == 'picture': exts = _PICTURE_EXTS elif media_type == 'movie': exts = _MOVIE_EXTS + ['.thumb'] for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue preserve_media = camera_config.get( '@preserve_%(media_type)ss' % {'media_type': media_type}, 0) if preserve_media == 0: return # preserve forever still_images_enabled = bool( ((camera_config['emulate_motion'] or camera_config['output_pictures']) and camera_config['picture_filename']) or (camera_config['snapshot_interval'] and camera_config['snapshot_filename'])) movies_enabled = camera_config['ffmpeg_output_movies'] if media_type == 'picture' and not still_images_enabled: continue # only cleanup pictures for cameras with still images enabled elif media_type == 'movie' and not movies_enabled: continue # only cleanup movies for cameras with movies enabled preserve_moment = datetime.datetime.now() - datetime.timedelta( days=preserve_media) target_dir = camera_config.get('target_dir') if os.path.exists(target_dir): # create a sentinel file to make sure the target dir is never removed open(os.path.join(target_dir, '.keep'), 'w').close() _remove_older_files(target_dir, preserve_moment, exts=exts)
def cleanup_media(media_type): logging.debug("cleaning up %(media_type)ss..." % {"media_type": media_type}) if media_type == "picture": exts = _PICTURE_EXTS elif media_type == "movie": exts = _MOVIE_EXTS + [".thumb"] for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue preserve_media = camera_config.get("@preserve_%(media_type)ss" % {"media_type": media_type}, 0) if preserve_media == 0: return # preserve forever still_images_enabled = bool( ( (camera_config["emulate_motion"] or camera_config["output_pictures"]) and camera_config["picture_filename"] ) or (camera_config["snapshot_interval"] and camera_config["snapshot_filename"]) ) movies_enabled = camera_config["ffmpeg_output_movies"] if media_type == "picture" and not still_images_enabled: continue # only cleanup pictures for cameras with still images enabled elif media_type == "movie" and not movies_enabled: continue # only cleanup movies for cameras with movies enabled preserve_moment = datetime.datetime.now() - datetime.timedelta(days=preserve_media) target_dir = camera_config.get("target_dir") if os.path.exists(target_dir): # create a sentinel file to make sure the target dir is never removed open(os.path.join(target_dir, ".keep"), "w").close() _remove_older_files(target_dir, preserve_moment, exts=exts)
def make_next_movie_preview(): global _previewless_movie_files logging.debug('making preview for the next movie...') if _previewless_movie_files: (camera_config, path) = _previewless_movie_files.pop(0) make_movie_preview(camera_config, path) else: logging.debug('gathering movies without preview...') count = 0 for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) if not utils.local_motion_camera(camera_config): continue target_dir = camera_config['target_dir'] for (full_path, st) in _list_media_files(target_dir, _MOVIE_EXTS): # @UnusedVariable if os.path.exists(full_path + '.thumb'): continue logging.debug('found a movie without preview: %(path)s' % {'path': full_path}) _previewless_movie_files.append((camera_config, full_path)) count += 1 logging.debug('found %(count)d movies without preview' % {'count': count}) if count: make_next_movie_preview()