Exemplo n.º 1
0
def make_media_folders():
    import config
    
    config.get_main() # just to have main config already loaded
    
    camera_ids = config.get_camera_ids()
    for camera_id in camera_ids:
        camera_config = config.get_camera(camera_id)
        if 'target_dir' in camera_config:
            if not os.path.exists(camera_config['target_dir']):
                try:
                    os.makedirs(camera_config['target_dir'])
                
                except Exception as e:
                    logging.error('failed to create root media folder "%s" for camera with id %s: %s' % (
                            camera_config['target_dir'], camera_id, e))
Exemplo n.º 2
0
def make_media_folders():
    import config
    
    config.get_main()  # just to have main config already loaded
    
    camera_ids = config.get_camera_ids()
    for camera_id in camera_ids:
        camera_config = config.get_camera(camera_id)
        if 'target_dir' in camera_config:
            if not os.path.exists(camera_config['target_dir']):
                try:
                    os.makedirs(camera_config['target_dir'])
                
                except Exception as e:
                    logging.error('failed to create root media folder "%s" for camera with id %s: %s' % (
                            camera_config['target_dir'], camera_id, e))
Exemplo n.º 3
0
def thread_id_to_camera_id(thread_id):
    main_config = config.get_main()
    threads = main_config.get('thread', '')

    try:
        return int(re.search('thread-(\d+).conf', threads[int(thread_id) - 1]).group(1))
    
    except IndexError:
        return None
Exemplo n.º 4
0
def motion_camera_id_to_camera_id(motion_camera_id):
    import config

    main_config = config.get_main()
    cameras = main_config.get('camera', [])

    try:
        return int(re.search(r'camera-(\d+).conf', cameras[int(motion_camera_id) - 1]).group(1))

    except IndexError:
        return None
Exemplo n.º 5
0
def _set_streameye_settings(camera_id, s):
    s = dict(s)
    s.setdefault('sePort', 8081)
    s.setdefault('seAuthMode', 'disabled')

    main_config = config.get_main()
    username = main_config['@normal_username']
    password = main_config['@normal_password']
    realm = 'motionEyeOS'

    logging.debug('writing streameye settings to %s' % STREAMEYE_CONF)

    lines = [
        'PORT="%s"' % s['sePort'],
        'AUTH="%s"' % s['seAuthMode'],
        'CREDENTIALS="%s:%s:%s"' % (username, password, realm)
    ]

    with open(STREAMEYE_CONF, 'w') as f:
        for line in lines:
            f.write(line + '\n')

    if 1 in config.get_camera_ids():
        # a workaround to update the camera username and password
        # since we cannot call set_camera() from here
        if s['seAuthMode'] == 'basic':
            url = 'http://%s:%[email protected]:%s/' % (username, password,
                                                  s['sePort'])

        else:
            url = 'http://127.0.0.1:%s/' % s['sePort']

        if 1 in config._camera_config_cache:
            logging.debug('updating streaming authentication in config cache')
            config._camera_config_cache[1]['@url'] = url

        lines = config.get_camera(1, as_lines=True)
        for i, line in enumerate(lines):
            if line.startswith('# @url'):
                lines[i] = '# @url %s' % url

        config_file = os.path.join(settings.CONF_PATH,
                                   config._CAMERA_CONFIG_FILE_NAME % {'id': 1})
        logging.debug(
            'updating streaming authentication in camera config file %s' %
            config_file)
        with open(config_file, 'w') as f:
            for line in lines:
                f.write(line + '\n')

    logging.debug('restarting streameye')
    if os.system('streameye.sh restart'):
        logging.error('streameye restart failed')
def _set_streameye_settings(camera_id, s):
    s = dict(s)
    s.setdefault('sePort', 8081)
    s.setdefault('seAuthMode', 'disabled')
    
    main_config = config.get_main()
    username = main_config['@normal_username']
    password = main_config['@normal_password']
    realm = 'motionEyeOS'

    logging.debug('writing streameye settings to %s' % STREAMEYE_CONF)
    
    lines = [
        'PORT="%s"' % s['sePort'],
        'AUTH="%s"' % s['seAuthMode'],
        'CREDENTIALS="%s:%s:%s"' % (username, password, realm)
    ]

    with open(STREAMEYE_CONF, 'w') as f:
        for line in lines:
            f.write(line + '\n')

    if 1 in config.get_camera_ids():
        # a workaround to update the camera username and password
        # since we cannot call set_camera() from here
        if s['seAuthMode'] == 'basic':
            url = 'http://%s:%[email protected]:%s/' % (username, password, s['sePort'])
        
        else:
            url = 'http://127.0.0.1:%s/' % s['sePort']
        
        if 1 in config._camera_config_cache:
            logging.debug('updating streaming authentication in config cache')
            config._camera_config_cache[1]['@url'] = url

        lines = config.get_camera(1, as_lines=True)
        for i, line in enumerate(lines):
            if line.startswith('# @url'):
                lines[i] = '# @url %s' % url

        config_file = os.path.join(settings.CONF_PATH, config._CAMERA_CONFIG_FILE_NAME % {'id': 1})
        logging.debug('updating streaming authentication in camera config file %s' % config_file)
        with open(config_file, 'w') as f:
            for line in lines:
                f.write(line + '\n')

    logging.debug('restarting streameye')
    if os.system('streameye.sh restart'):
        logging.error('streameye restart failed')
Exemplo n.º 7
0
def camera_id_to_thread_id(camera_id):
    import config

    # find the corresponding thread_id
    # (which can be different from camera_id)

    main_config = config.get_main()
    threads = main_config.get('thread', [])

    thread_filename = 'thread-%d.conf' % camera_id
    for i, thread in enumerate(threads):
        if thread != thread_filename:
            continue

        return i + 1

    return None
Exemplo n.º 8
0
def camera_id_to_motion_camera_id(camera_id):
    import config

    # find the corresponding motion camera_id
    # (which can be different from camera_id)

    main_config = config.get_main()
    cameras = main_config.get('camera', [])

    camera_filename = 'camera-%d.conf' % camera_id
    for i, camera in enumerate(cameras):
        if camera != camera_filename:
            continue

        return i + 1

    return None
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)
        
    main_config = config.get_main()
    threads = main_config.get('thread', [])
    
    thread_filename = 'thread-%d.conf' % camera_id
    for i, thread in enumerate(threads):
        if thread != thread_filename:
            continue
        
        return i + 1

    return None
Exemplo n.º 10
0
def _set_streameye_enabled(enabled):
    global _streameye_enabled
    
    if enabled:
        logging.debug('removing all cameras from cache')
        config._camera_config_cache = {}
        config._camera_ids_cache = []
        
        logging.debug('disabling all cameras in motion.conf')
        cmd = 'sed -r -i "s/^thread (.*)/#thread \1/" /data/etc/motion.conf &>/dev/null'
        if os.system(cmd):
            logging.error('failed to disable cameras in motion.conf')
        
        logging.debug('renaming thread files')
        for name in os.listdir(settings.CONF_PATH):
            if re.match('^thread-\d+.conf$', name):
                os.rename(os.path.join(settings.CONF_PATH, name), os.path.join(settings.CONF_PATH, name + '.bak'))

        logging.debug('adding simple mjpeg camera')
        
        streameye_settings = _get_streameye_settings(1)
        main_config = config.get_main()
        
        device_details = {
            'proto': 'mjpeg',
            'host': '127.0.0.1',
            'port': streameye_settings['sePort'],
            'username': '',
            'password': '',
            'scheme': 'http',
            'uri': '/'
        }

        if streameye_settings['seAuthMode'] == 'basic':
            device_details['username'] = main_config['@normal_username']
            device_details['password'] = main_config['@normal_password']

        _streameye_enabled = True
        config._additional_structure_cache = {}
        camera_config = config.add_camera(device_details)
        
        # call set_camera again so that the streamEye-related defaults are saved
        config.set_camera(camera_config['@id'], camera_config)
        
        _set_motioneye_add_remove_cameras(False)

    else: # disabled
        logging.debug('removing simple mjpeg camera')
        for camera_id in config.get_camera_ids():
            camera_config = config.get_camera(camera_id)
            if camera_config.get('@proto') == 'mjpeg':
                config.rem_camera(camera_id)

        logging.debug('renaming thread files')
        for name in os.listdir(settings.CONF_PATH):
            if re.match('^thread-\d+.conf.bak$', name):
                os.rename(os.path.join(settings.CONF_PATH, name), os.path.join(settings.CONF_PATH, name[:-4]))
        
        _streameye_enabled = False
        config.invalidate()

        logging.debug('enabling all cameras')
        for camera_id in config.get_camera_ids():
            camera_config = config.get_camera(camera_id)
            camera_config['@enabled'] = True
            config.set_camera(camera_id, camera_config)
            
        _set_motioneye_add_remove_cameras(True)
Exemplo n.º 11
0
def _set_streameye_enabled(enabled):
    global _streameye_enabled
    
    if enabled:
        logging.debug('removing all cameras from cache')
        config._camera_config_cache = {}
        config._camera_ids_cache = []
        
        logging.debug('disabling all cameras in motion.conf')
        cmd = 'sed -r -i "s/^camera (.*)/#camera \\1/" /data/etc/motion.conf &>/dev/null'
        if os.system(cmd):
            logging.error('failed to disable cameras in motion.conf')
        
        logging.debug('renaming camera files')
        for name in os.listdir(settings.CONF_PATH):
            if re.match('^camera-\d+.conf$', name):
                os.rename(os.path.join(settings.CONF_PATH, name), os.path.join(settings.CONF_PATH, name + '.bak'))

        logging.debug('adding simple mjpeg camera')
        
        streameye_settings = _get_streameye_settings(1)
        main_config = config.get_main()
        
        device_details = {
            'proto': 'mjpeg',
            'host': '127.0.0.1',
            'port': streameye_settings['sePort'],
            'username': '',
            'password': '',
            'scheme': 'http',
            'path': '/'
        }

        if streameye_settings['seAuthMode'] == 'basic':
            device_details['username'] = main_config['@normal_username']
            device_details['password'] = main_config['@normal_password']

        _streameye_enabled = True
        config._additional_structure_cache = {}
        camera_config = config.add_camera(device_details)
        
        # call set_camera again so that the streamEye-related defaults are saved
        config.set_camera(camera_config['@id'], camera_config)
        
        _set_motioneye_add_remove_cameras(False)

    else: # disabled
        logging.debug('removing simple mjpeg camera')
        for camera_id in config.get_camera_ids():
            camera_config = config.get_camera(camera_id)
            if camera_config.get('@proto') == 'mjpeg':
                config.rem_camera(camera_id)

        logging.debug('renaming camera files')
        for name in os.listdir(settings.CONF_PATH):
            if re.match('^camera-\d+.conf.bak$', name):
                os.rename(os.path.join(settings.CONF_PATH, name), os.path.join(settings.CONF_PATH, name[:-4]))
        
        _streameye_enabled = False
        config.invalidate()

        logging.debug('enabling all cameras')
        for camera_id in config.get_camera_ids():
            camera_config = config.get_camera(camera_id)
            camera_config['@enabled'] = True
            config.set_camera(camera_id, camera_config)
            
        _set_motioneye_add_remove_cameras(True)
Exemplo n.º 12
0
 def index(self):
     return config.get_main()
Exemplo n.º 13
0
def thread_id_to_camera_id(thread_id):
    main_config = config.get_main()
    threads = main_config.get('thread', '')

    return int(re.search('thread-(\d+).conf', threads[thread_id - 1]).group(1))
Exemplo n.º 14
0
 def index(self):
     return config.get_main()