Exemplo n.º 1
0
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.is_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.do_connect()

        MjpgClient.clients[camera_id] = client

    client = MjpgClient.clients[camera_id]

    return client.get_last_jpg()
Exemplo n.º 2
0
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.is_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]

    return client.get_last_jpg()
Exemplo n.º 3
0
def cleanup_media(media_type):
    logging.debug('cleaning up %(media_type)ss...' % {'media_type': media_type})
    
    if media_type == 'picture':
        exts = _PICTURE_EXTS
        
    else:  # 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.is_local_motion_camera(camera_config):
            continue
        
        preserve_media = camera_config.get('@preserve_%(media_type)ss' % {'media_type': media_type}, 0)
        if preserve_media == 0:
            continue  # preserve forever
        
        still_images_enabled = bool(camera_config['picture_filename']) or bool(camera_config['snapshot_filename'])
        movies_enabled = bool(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)
Exemplo n.º 4
0
def _set_all_motion_detection(enable):
    import motionctl

    def on_set_config_response(error=None):
        if error is None:
            logging.debug("Saving motion detection failed")
        else:
            logging.debug("Motion detection saved successfully")

    def on_get_config_response(remote_ui_config=None, error=None):
        remote_ui_config["motion_detection"] = enable
        remote.set_config(local_config, remote_ui_config,
                          on_set_config_response)

    for camera_id in config.get_camera_ids():
        camera_config = config.get_camera(camera_id)
        if not utils.is_local_motion_camera(camera_config):
            local_config = config.get_camera(camera_id)
            remote.get_config(local_config, on_get_config_response)
            logging.debug(
                'motion detection %s by config for remote camera with id %s' %
                (str(enable), camera_id))
        elif not camera_config['@motion_detection']:
            logging.debug(
                'motion detection %s by config for local camera with id %s' %
                (str(enable), camera_id))
            motionctl.set_motion_detection(camera_id, enable)
        else:
            logging.error("Couldn't categorize camera with id %s" % camera_id)
Exemplo n.º 5
0
def cleanup_media(media_type):
    logging.debug('cleaning up %(media_type)ss...' % {'media_type': media_type})
    
    if media_type == 'picture':
        exts = _PICTURE_EXTS
        
    else:  # 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.is_local_motion_camera(camera_config):
            continue
        
        preserve_media = camera_config.get('@preserve_%(media_type)ss' % {'media_type': media_type}, 0)
        if preserve_media == 0:
            continue  # preserve forever
        
        still_images_enabled = bool(camera_config['picture_filename']) or bool(camera_config['snapshot_filename'])
        movies_enabled = bool(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)
Exemplo n.º 6
0
def _disable_initial_motion_detection():
    import config

    for camera_id in config.get_camera_ids():
        camera_config = config.get_camera(camera_id)
        if not utils.is_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)
Exemplo n.º 7
0
def _disable_initial_motion_detection():
    import config

    for camera_id in config.get_camera_ids():
        camera_config = config.get_camera(camera_id)
        if not utils.is_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)
Exemplo n.º 8
0
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: %(error)s' % {
                    'id': camera_id, 'error': error})
            
        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.is_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))
Exemplo n.º 9
0
def camera_id_to_thread_id(camera_id):
    import config

    # find the corresponding thread_id
    # (which can be different from camera_id)
    camera_ids = config.get_camera_ids()
    thread_id = 0
    camera_id = int(camera_id)
    for cid in camera_ids:
        camera_config = config.get_camera(cid)
        if utils.is_local_motion_camera(camera_config):
            thread_id += 1

        if cid == camera_id:
            return thread_id or None

    return None
Exemplo n.º 10
0
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: %(error)s'
                % {
                    'id': camera_id,
                    'error': error
                })

        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.is_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))
Exemplo n.º 11
0
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: %(error)s'
                % {
                    'id': camera_id,
                    'error': error
                })

        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.is_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'

        geofence_enabled = camera_config.get('@geofence_enabled')
        ips = camera_config.get('@geofence_ips')

        if not working_schedule:  # working schedule disabled, motion detection left untouched
            continue

        if not motion_detection:  # motion detection explicitly disabled
            continue

        must_be_enabled = True

        now_during = True
        if working_schedule:
            now_during = _during_working_schedule(now, working_schedule)

        if geofence_enabled and (now_during
                                 or working_schedule_type != 'outside'):
            for ip in ips.split(','):
                try:
                    ip = ip.strip()
                    socket.inet_aton(ip)
                except socket.error as e:
                    logging.warning('geofence ip error: \'%s\' msg: %s' %
                                    (ip, e.message))
                    continue

                try:
                    subprocess.check_call(['ping', '-c1', ip],
                                          stdout=open(os.devnull, 'w'))
                    must_be_enabled = False
                    break
                except:
                    pass

        motionctl.get_motion_detection(
            camera_id,
            functools.partial(on_motion_detection_status, camera_id,
                              must_be_enabled, working_schedule_type))