Пример #1
0
Файл: main.py Проект: dulton/IVR
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('uploader.yml')
        config = config_schema.validate(config)

        from pyramid.config import Configurator
        from pyramid.renderers import JSON
        from ivr.common.utils import CustomJSONEncoder
        pyramid_config = Configurator()
        pyramid_config.add_renderer(None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))

        if config.get('fs_path'):
            from fs import FSUploadService
            upload_service = FSUploadService(config['fs_path'])
            pyramid_config.registry.upload_service = upload_service
        else:
            raise Exception('No valid upload serivce given')

        pyramid_config.add_route('preview_upload', 'api/ivc/v1/projects/{project_name}/devices/{login_code}/channels/{channel}/upload_preview')
        pyramid_config.add_view(upload_view, route_name='preview_upload', request_method='POST')
        upload_server = WSGIServer(config['listen'], pyramid_config.make_wsgi_app())

        gevent.joinall(map(gevent.spawn, (upload_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception('Failed to start preview image uploader')
Пример #2
0
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('uploader.yml')
        config = config_schema.validate(config)
        log.info('config: {0}'.format(config))

        from pyramid.config import Configurator
        from pyramid.renderers import JSON
        from ivr.common.utils import CustomJSONEncoder
        pyramid_config = Configurator()
        pyramid_config.add_renderer(None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))

        from sqlalchemy import engine_from_config
        from ivr.ivc.daos.sa_dao_context_mngr import AlchemyDaoContextMngr
        from ivr.ivc.daos.sa_device_dao import SADeviceDao
        from ivr.ivc.manager.device import Device
        engine = engine_from_config(config['sqlalchemy'], prefix='')
        dao_context_mngr = AlchemyDaoContextMngr(engine)
        device_dao = SADeviceDao(dao_context_mngr, device_cls=Device)

        if config.get('fs_path'):
            from fs import FSUploadService
            upload_service = FSUploadService(config['fs_path'], device_dao)
            pyramid_config.registry.upload_service = upload_service
        else:
            raise Exception('No valid upload serivce given')

        def error_view(exc, request):
            response = request.response
            response.status_int = 500
            type, dummy, tb = sys.exc_info()
            tb_list = traceback.format_list(traceback.extract_tb(tb)[-10:])
            log.error('Error handling request\n{0}\n{1}'.format(STRING(exc), ' '.join(tb_list)))
            return {'info': STRING(exc), 'exception': STRING(type), 'traceback': tb_list}
        pyramid_config.add_view(error_view, context=Exception)

        pyramid_config.add_route('preview_upload', 'api/ivc/v1/preview_upload')
        pyramid_config.add_view(upload_view, route_name='preview_upload', request_method='POST')
        upload_server = WSGIServer(config['listen'], pyramid_config.make_wsgi_app(), log=logging.getLogger('upload server'))

        gevent.joinall(map(gevent.spawn, (upload_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception('Failed to start preview image uploader')
Пример #3
0
Файл: main.py Проект: dulton/IVR
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('ivt.yml')
        config = config_schema.validate(config)

        ivt = IVT(
            config['project'],
            config['login_code'],
            config['login_passwd'],
            config['keepalive_interval'],
            config['cameras'],
            preview_upload_server=config.get('preview_upload_server')
        )

        # start port
        port = SubProcessPort(
            port_name=config['login_code'],
            port_type=RTSP_PORT_PROGRAM_NAME,
            log_file= RTSP_PORT_PROGRAM_NAME + '.log',
        )
        port.start()

        # connect to IVC
        WSClientTransport.APP_FACTORY = ivt.ivt_session_factory
        url = config['ivc']+'?'+urllib.urlencode(
            {
                'login_code': config['login_code'],
                'login_passwd': config['login_passwd'],
                'project': config['project']
            }
        )
        while True:
            try:
                client = WSClientTransport(url)
                client.connect()
                client.wait_close()
                time.sleep(10)
            except:
                log.exception("Client session closed")
                time.sleep(10)

    except Exception:
        log.exception('Failed to start IVT')
Пример #4
0
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('ivt.yml')
        config = config_schema.validate(config)

        ivt = IVT(config['project'],
                  config['login_code'],
                  config['login_passwd'],
                  config['keepalive_interval'],
                  config['cameras'],
                  preview_upload_server=config.get('preview_upload_server'))

        # start port
        port = SubProcessPort(
            port_name=config['login_code'],
            port_type=RTSP_PORT_PROGRAM_NAME,
            log_file=RTSP_PORT_PROGRAM_NAME + '.log',
        )
        port.start()

        # connect to IVC
        WSClientTransport.APP_FACTORY = ivt.ivt_session_factory
        url = config['ivc'] + '?' + urllib.urlencode(
            {
                'login_code': config['login_code'],
                'login_passwd': config['login_passwd'],
                'project': config['project']
            })
        while True:
            try:
                client = WSClientTransport(url)
                client.connect()
                client.wait_close()
                time.sleep(10)
            except:
                log.exception("Client session closed")
                time.sleep(10)

    except Exception:
        log.exception('Failed to start IVT')
Пример #5
0
def main():
    default_log_config()
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('record_agent.yml')
        config = config_schema.validate(config)

        pyramid_config = Configurator()
        pyramid_config.add_renderer(None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))
        record_agent_mngr = RecordAgentManager(
            config['agent_id'],
            config['keepalive_endpoint'],
            config['self_request_endpoint'],
            config['max_recorder'])
        pyramid_config.registry.record_agent_mngr = record_agent_mngr

        def error_view(exc, request):
            response = request.response
            response.status_int = 500
            type, dummy, tb = sys.exc_info()
            tb_list = traceback.format_list(traceback.extract_tb(tb)[-10:])
            log.error('Error handling request\n{0}\n{1}'.format(STRING(exc), ' '.join(tb_list)))
            return {'info': STRING(exc), 'exception': STRING(type), 'traceback': tb_list}
        pyramid_config.add_view(error_view, context=Exception)
        pyramid_config.add_route('request_record', 'api/record_agent/v1/record')
        pyramid_config.add_view(request_record_view, route_name='request_record', request_method='POST')
        agent_server = WSGIServer(config['listen'], pyramid_config.make_wsgi_app(), log=logging.getLogger('agent server'))

        record_agent_mngr.start()
        gevent.joinall(map(gevent.spawn, (agent_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception('Failed to start record agent')
Пример #6
0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division

import gevent.monkey
gevent.monkey.patch_all()

import argparse
import time
import urllib

from ivr.common.ws import WSServer
from ivr.common.utils import STRING
from ivr.common.logger import default_config as default_log_config
from ivr.ivc.manager.ivtconn import IVTConn, IVTWSConnectionManager

default_log_config(debug=False)
import logging
log = logging.getLogger(__name__)


class DummyDevice(object):
    def on_device_offline(self):
        pass


class DummyDeviceManager(object):
    def get_device(self, project_name, device_id):
        return DummyDevice()


class DummyDeviceConn(IVTConn):
Пример #7
0
channel_config_schema = Schema(
    ListVal({
        'channel': IntVal(min=0),
        'type': Use(STRING),
        'ip': Use(STRING),
        'preview_upload_interval': IntVal(min=0, max=86400),
        'streams': ListVal({
            'type': Use(STRING),
            'quality': VideoQuality.schema,
            Optional('url'): Default(Use(STRING), default=''),
        }),
        DoNotCare(STRING): object
    })
)

default_log_config()
log = logging.getLogger(__name__)


class App(object):

    ivt_package_schema = Schema({
        'firmware_model': Use(STRING),
        'url': Use(STRING),
    })

    def __init__(self):
        self._quit = False
        if len(sys.argv) >= 2:
            config = parse_conf(sys.argv[1])
        else:
Пример #8
0
Файл: main.py Проект: dulton/IVR
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('ivc.yml')
        config = config_schema.validate(config)

        # prepare data backend
        if config.get('sqlalchemy'):
            from sqlalchemy import engine_from_config
            from ivr.ivc.daos.sa_dao_context_mngr import AlchemyDaoContextMngr
            from ivr.ivc.daos.sa_camera_dao import SACameraDao
            from ivr.ivc.daos.sa_device_dao import SADeviceDao
            from ivr.ivc.daos.sa_project_dao import SAProjectDao
            from ivr.ivc.daos.sa_user_dao import SAUserDao
            from ivr.ivc.daos.sa_user_session_log_dao import SAUserSessionLogDao
            from ivr.ivc.daos.sa_access_key_dao import SAAccessKeyDao
            engine = engine_from_config(config['sqlalchemy'], prefix='')
            dao_context_mngr = AlchemyDaoContextMngr(engine)
            project_dao = SAProjectDao(dao_context_mngr)
            camera_dao = SACameraDao(dao_context_mngr)
            device_dao = SADeviceDao(dao_context_mngr)
            user_dao = SAUserDao(dao_context_mngr)
            access_key_dao = SAAccessKeyDao(dao_context_mngr)
            from ivr.ivc.dummy_daos import UserSessionDAO, StreamDAO
            user_session_dao = UserSessionDAO()
            user_session_log_dao = SAUserSessionLogDao(dao_context_mngr)
            stream_dao = StreamDAO()
        else:
            from ivr.ivc.dummy_daos import CameraDAO, UserSessionDAO, StreamDAO, DeviceDAO
            camera_dao = CameraDAO()
            stream_dao = StreamDAO()
            user_session_dao = UserSessionDAO()
            device_dao = DeviceDAO()
            user_dao = None
            access_key_dao = None

        project_mngr = ProjectManager(project_dao)
        if not config['ws_listen']:
            from ivr.ivc.manager.device import DummyDeviceManager
            device_mngr = DummyDeviceManager(device_dao)
        else:
            device_mngr = DeviceWSConnectionManager(device_dao,
                                                    config['device_ttl'])
        camera_mngr = CameraManager(camera_dao, device_mngr)
        device_mngr.set_camera_mngr(camera_mngr)
        stream_mngr = StreamManager(stream_dao,
                                    camera_mngr,
                                    config['rtmp_publish_url_prefix'],
                                    config['rtmp_url_prefix'],
                                    config['hls_url_prefix'],
                                    stream_ttl=config['stream_ttl'])
        user_session_log_mngr = UserSessionLogManager(user_session_log_dao)
        user_session_mngr = UserSessionManager(user_session_dao, stream_mngr,
                                               user_session_log_mngr,
                                               config['user_session_ttl'])
        user_mngr = UserManager(user_dao, project_dao, dao_context_mngr)
        access_key_mngr = AccessKeyManager(access_key_dao, user_dao,
                                           dao_context_mngr)

        # prepare REST API
        from pyramid.config import Configurator
        from pyramid.renderers import JSON
        from pyramid.authorization import ACLAuthorizationPolicy
        from ivr.common.utils import CustomJSONEncoder
        from ivr.ivc.rest.security import IvcJwtAuthenticationPolicy
        authn_policy = IvcJwtAuthenticationPolicy()
        authz_policy = ACLAuthorizationPolicy()

        pyramid_config = Configurator()
        pyramid_config.add_renderer(
            None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))
        # pyramid_config.set_authentication_policy(authn_policy)
        # pyramid_config.set_authorization_policy(authz_policy)
        pyramid_config.include('ivr.ivc.rest', route_prefix='api/ivc/v1')
        pyramid_config.registry.project_mngr = project_mngr
        pyramid_config.registry.device_mngr = device_mngr
        pyramid_config.registry.camera_mngr = camera_mngr
        pyramid_config.registry.stream_mngr = stream_mngr
        pyramid_config.registry.user_session_mngr = user_session_mngr
        pyramid_config.registry.user_session_log_mngr = user_session_log_mngr
        pyramid_config.registry.user_mngr = user_mngr
        pyramid_config.registry.access_key_mngr = access_key_mngr
        if config.get('debug'):
            pyramid_config.add_settings({
                'debugtoolbar.hosts': ['0.0.0.0/0', '::1'],
                'debugtoolbar.enabled':
                True,
                'pyramid.debug_authorization':
                False,
            })
            pyramid_config.include('pyramid_debugtoolbar')
            pyramid_config.include('pyramid_chameleon')
        rest_server = WSGIServer(config['rest_listen'],
                                 pyramid_config.make_wsgi_app())

        # start server and wait
        if config['ws_listen']:
            ws_server = WSServer(config['ws_listen'],
                                 device_mngr.device_online)
            gevent.joinall(
                map(gevent.spawn,
                    (ws_server.server_forever, rest_server.serve_forever)))
        else:
            gevent.joinall(map(gevent.spawn, (rest_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception("Failed to start IVC")
Пример #9
0
Файл: main.py Проект: dulton/IVR
def main():
    default_log_config(debug=True)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('ivc.yml')
        config = config_schema.validate(config)

        # prepare data backend
        if config.get('sqlalchemy'):
            from sqlalchemy import engine_from_config
            from ivr.ivc.daos.sa_dao_context_mngr import AlchemyDaoContextMngr
            from ivr.ivc.daos.sa_camera_dao import SACameraDao
            from ivr.ivc.daos.sa_device_dao import SADeviceDao
            from ivr.ivc.daos.sa_project_dao import SAProjectDao
            from ivr.ivc.daos.sa_user_dao import SAUserDao
            from ivr.ivc.daos.sa_user_session_log_dao import SAUserSessionLogDao
            from ivr.ivc.daos.sa_access_key_dao import SAAccessKeyDao
            engine = engine_from_config(config['sqlalchemy'], prefix='')
            dao_context_mngr = AlchemyDaoContextMngr(engine)
            project_dao = SAProjectDao(dao_context_mngr)
            camera_dao = SACameraDao(dao_context_mngr)
            device_dao = SADeviceDao(dao_context_mngr)
            user_dao = SAUserDao(dao_context_mngr)
            access_key_dao = SAAccessKeyDao(dao_context_mngr)
            from ivr.ivc.dummy_daos import UserSessionDAO, StreamDAO
            user_session_dao = UserSessionDAO()
            user_session_log_dao = SAUserSessionLogDao(dao_context_mngr)
            stream_dao = StreamDAO()
        else:
            from ivr.ivc.dummy_daos import CameraDAO, UserSessionDAO, StreamDAO, DeviceDAO
            camera_dao = CameraDAO()
            stream_dao = StreamDAO()
            user_session_dao = UserSessionDAO()
            device_dao = DeviceDAO()
            user_dao = None
            access_key_dao = None

        project_mngr = ProjectManager(project_dao)
        if not config['ws_listen']:
            from ivr.ivc.manager.device import DummyDeviceManager
            device_mngr = DummyDeviceManager(device_dao)
        else:
            device_mngr = DeviceWSConnectionManager(device_dao, config['device_ttl'])
        camera_mngr = CameraManager(camera_dao, device_mngr)
        device_mngr.set_camera_mngr(camera_mngr)
        stream_mngr = StreamManager(stream_dao,
                                    camera_mngr,
                                    config['rtmp_publish_url_prefix'],
                                    config['rtmp_url_prefix'],
                                    config['hls_url_prefix'],
                                    stream_ttl=config['stream_ttl'])
        user_session_log_mngr = UserSessionLogManager(user_session_log_dao)
        user_session_mngr = UserSessionManager(user_session_dao,
                                               stream_mngr,
                                               user_session_log_mngr,
                                               config['user_session_ttl'])
        user_mngr = UserManager(user_dao, project_dao, dao_context_mngr)
        access_key_mngr = AccessKeyManager(access_key_dao, user_dao, dao_context_mngr)

        # prepare REST API
        from pyramid.config import Configurator
        from pyramid.renderers import JSON
        from pyramid.authorization import ACLAuthorizationPolicy
        from ivr.common.utils import CustomJSONEncoder
        from ivr.ivc.rest.security import IvcJwtAuthenticationPolicy
        authn_policy = IvcJwtAuthenticationPolicy()
        authz_policy = ACLAuthorizationPolicy()

        pyramid_config = Configurator()
        pyramid_config.add_renderer(None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))
        # pyramid_config.set_authentication_policy(authn_policy)
        # pyramid_config.set_authorization_policy(authz_policy)
        pyramid_config.include('ivr.ivc.rest', route_prefix='api/ivc/v1')
        pyramid_config.registry.project_mngr = project_mngr
        pyramid_config.registry.device_mngr = device_mngr
        pyramid_config.registry.camera_mngr = camera_mngr
        pyramid_config.registry.stream_mngr = stream_mngr
        pyramid_config.registry.user_session_mngr = user_session_mngr
        pyramid_config.registry.user_session_log_mngr = user_session_log_mngr
        pyramid_config.registry.user_mngr = user_mngr
        pyramid_config.registry.access_key_mngr = access_key_mngr
        if config.get('debug'):
            pyramid_config.add_settings({'debugtoolbar.hosts': ['0.0.0.0/0', '::1'],
                                 'debugtoolbar.enabled': True,
                                 'pyramid.debug_authorization': False,})
            pyramid_config.include('pyramid_debugtoolbar')
            pyramid_config.include('pyramid_chameleon')
        rest_server = WSGIServer(config['rest_listen'], pyramid_config.make_wsgi_app())

        # start server and wait
        if config['ws_listen']:
            ws_server = WSServer(config['ws_listen'], device_mngr.device_online)
            gevent.joinall(map(gevent.spawn, (ws_server.server_forever, rest_server.serve_forever)))
        else:
            gevent.joinall(map(gevent.spawn, (rest_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception("Failed to start IVC")
Пример #10
0
def main():
    default_log_config(debug=False)
    import logging
    log = logging.getLogger(__name__)

    try:
        if len(sys.argv) == 2:
            config = parse_conf(sys.argv[1])
        else:
            config = parse_conf('ivc.yml')
        config = config_schema.validate(config)

        # prepare data backend
        if config.get('sqlalchemy'):
            from sqlalchemy import engine_from_config
            from ivr.ivc.daos.sa_dao_context_mngr import AlchemyDaoContextMngr
            from ivr.ivc.daos.sa_camera_dao import SACameraDao
            from ivr.ivc.daos.sa_device_dao import SADeviceDao
            from ivr.ivc.daos.sa_project_dao import SAProjectDao
            from ivr.ivc.daos.sa_user_dao import SAUserDao
            from ivr.ivc.daos.sa_user_session_log_dao import SAUserSessionLogDao
            from ivr.ivc.daos.sa_access_key_dao import SAAccessKeyDao
            from ivr.ivc.daos.sa_firmware_dao import SAFirmwareDao
            from ivr.ivc.daos.sa_account_dao import SAAccountDao
            from ivr.ivc.daos.sa_bill_dao import SABillDao
            from ivr.ivc.daos.sa_record_schedule_dao import SARecordScheduleDao
            from ivr.ivc.daos.sa_record_config_dao import SARecordConfigDao
            from ivr.ivc.daos.sa_record_agent_dao import SARecordAgentDao
            from ivr.ivc.daos.sa_wechat_binding_dao import SAWechatBindingDao
            from ivr.ivc.daos.sa_stream_dao import SAStreamDao
            from ivr.ivc.daos.sa_session_dao import SASessionDao
            from ivr.ivc.daos.sa_live_show_dao import SALiveShowDao
            from ivr.ivc.daos.sa_user_vod_session_log_dao import SAUserVODSessionLogDao
            from ivr.ivc.daos.sa_move_detect_dao import SAMoveDetectConfigDao
            engine = engine_from_config(config['sqlalchemy'], prefix='')
            dao_context_mngr = AlchemyDaoContextMngr(engine)
            project_dao = SAProjectDao(dao_context_mngr)
            camera_dao = SACameraDao(dao_context_mngr)
            device_dao = SADeviceDao(dao_context_mngr)
            user_dao = SAUserDao(dao_context_mngr)
            access_key_dao = SAAccessKeyDao(dao_context_mngr)
            user_session_log_dao = SAUserSessionLogDao(dao_context_mngr)
            #stream_dao = SAStreamDao(dao_context_mngr)
            #user_session_dao = SASessionDao(dao_context_mngr)
            firmware_dao = SAFirmwareDao(dao_context_mngr)
            account_dao = SAAccountDao(dao_context_mngr)
            bill_dao = SABillDao(dao_context_mngr)
            record_schedule_dao = SARecordScheduleDao(dao_context_mngr)
            record_config_dao = SARecordConfigDao(dao_context_mngr)
            record_agent_dao = SARecordAgentDao(dao_context_mngr)
            wechat_binding_dao = SAWechatBindingDao(dao_context_mngr)
            live_show_dao = SALiveShowDao(dao_context_mngr)
            vod_sessionlog_dao = SAUserVODSessionLogDao(dao_context_mngr)
            move_detect_config_dao = SAMoveDetectConfigDao(dao_context_mngr)
        else:
            raise Exception('No sqlalchemy or other DAO specfied')

        redis_host, _, redis_port = config['redis'].partition(':')
        redis_port = int(redis_port) if redis_port else None
        redis_client = redis.StrictRedis(
            host=redis_host,
            port=redis_port,
            max_connections=32
        )

        from ivr.ivc.daos.rd_stream_dao import RDStreamDao
        from ivr.ivc.daos.rd_session_dao import RDSessionDao
        stream_dao = RDStreamDao(redis_client)
        user_session_dao = RDSessionDao(redis_client)

        from pymongo import MongoClient
        from ivr.ivc.daos.mg_record_file_dao import MGRecordFileDAO
        from ivr.ivc.daos.mg_record_statistic_dao import MGRecordStatisticDAO
        from ivr.ivc.daos.mg_event_record_dao import MGEventRecordDAO, MGEventRecordOpDAO
        mongo_db = MongoClient(config['mongo']['url'])[config['mongo']['database']]
        record_file_dao = MGRecordFileDAO(mongo_db)
        record_statistic_dao = MGRecordStatisticDAO(mongo_db)
        event_record_dao = MGEventRecordDAO(mongo_db)
        event_record_op_dao = MGEventRecordOpDAO(mongo_db)

        project_mngr = ProjectManager(project_dao)
        firmware_mngr = FirmwareManager(firmware_dao)
        camera_mngr = CameraManager(
            camera_dao,
            config['camera_preview_image_path_prefix'],
            config['camera_preview_image_url_prefix'],
            config['camera_preview_image_server']
        )
        from ivr.ivc.stream_vendor.nginx import StreamVendorNginx
        from ivr.ivc.stream_vendor.aliyun import StreamVendorAliyun
        stream_vendor_nginx = StreamVendorNginx(config['rtmp_publish_url_prefix'])
        stream_vendor_aliyun = StreamVendorAliyun(**config['aliyun_live'])
        stream_mngr = StreamManager(stream_dao,
                                    camera_mngr,
                                    project_mngr,
                                    stream_vendor_nginx,
                                    stream_vendor_aliyun,
                                    stream_ttl=config['stream_ttl'])
        camera_mngr.set_stream_mngr(stream_mngr)
        ivtconn_mngr = IVTWSConnectionManager(config['device_ttl'])
        device_mngr = DeviceManager(device_dao, firmware_mngr, camera_mngr, stream_mngr, ivtconn_mngr)
        camera_mngr.set_device_mngr(device_mngr)
        ivtconn_mngr.set_device_mngr(device_mngr)
        ivtconn_mngr.set_camera_mngr(camera_mngr)
        user_session_log_mngr = UserSessionLogManager(user_session_log_dao)
        user_session_mngr = UserSessionManager(user_session_dao,
                                               user_session_log_mngr,
                                               project_mngr,
                                               camera_mngr,
                                               config['user_session_ttl'])
        camera_mngr.set_user_session_mngr(user_session_mngr)
        user_mngr = UserManager(user_dao, project_dao, dao_context_mngr)
        access_key_mngr = AccessKeyManager(access_key_dao,
                                           user_dao,
                                           dao_context_mngr,
                                           config['default_access_key'])
        bill_mngr = BillManager(account_dao, bill_dao, project_dao, dao_context_mngr)
        record_schedule_mngr = RecordScheduleManager(record_schedule_dao)
        record_agent_mngr = RecordAgentManager(record_agent_dao)
        record_file_mngr = RecordFileManager(record_file_dao)
        record_statistic_mngr = RecordStatisticManager(record_statistic_dao, project_mngr)
        user_vod_session_log_mngr = VodSessionLogManager(vod_sessionlog_dao)
        storage_client = None
        if 'oss' in config:
            from ivr.ivc.manager.oss import OSS
            storage_client = OSS(**config['oss'])
        else:
            raise Exception('No record file storage specified')
        record_storage_mngr = RecordStorageManager(
            config['ivc_host'],
            config['record_file_token_key'],
            camera_mngr,
            record_file_mngr,
            record_statistic_mngr,
            user_vod_session_log_mngr,
            storage_client
        )

        record_config_mngr = RecordConfigManager(
            record_config_dao,
            config['ivc_host'],
            camera_mngr,
            record_schedule_mngr,
            record_agent_mngr,
            record_storage_mngr,
            60
        )
        record_agent_mngr.set_record_config_mngr(record_config_mngr)
        ivtconn_mngr.set_record_config_mngr(record_config_mngr)
        camera_mngr.set_record_config_mngr(record_config_mngr)
        event_record_op_mngr = EventRecordOpManager(event_record_op_dao)
        event_record_mngr = EventRecordManager(
            event_record_dao,
            camera_mngr,
            record_storage_mngr,
            event_record_op_mngr
        )
        record_storage_mngr.set_event_record_mngr(event_record_mngr)
        wechat_mngr = WechatManager(user_dao=user_dao,
                                    wechat_binding_dao=wechat_binding_dao,
                                    dao_context_mngr=dao_context_mngr,
                                    access_key_manager=access_key_mngr,
                                    wechat_conf=config.get('wechat_conf', {}))
        live_show_mngr = LiveShowManager(
            live_show_dao,
            camera_mngr,
            record_config_mngr,
            event_record_mngr,
            config['live_show_default_wechat_url_prefix'],
            config['live_show_default_cover_url']
        )
        move_detect_config_mngr = MoveDetectConfigManager(move_detect_config_dao, camera_mngr)


        # prepare REST API
        from pyramid.config import Configurator
        from pyramid.renderers import JSON
        from pyramid.authorization import ACLAuthorizationPolicy
        from ivr.common.utils import CustomJSONEncoder
        from ivr.ivc.rest.security import IvcJwtAuthenticationPolicy
        authn_policy = IvcJwtAuthenticationPolicy()
        authz_policy = ACLAuthorizationPolicy()

        pyramid_config = Configurator()
        pyramid_config.add_renderer(None, JSON(indent=4, check_circular=True, cls=CustomJSONEncoder))
        pyramid_config.set_authentication_policy(authn_policy)
        pyramid_config.set_authorization_policy(authz_policy)
        pyramid_config.include('ivr.ivc.rest', route_prefix='api/ivc/v1')
        pyramid_config.registry.debug = config['debug']
        pyramid_config.registry.project_mngr = project_mngr
        pyramid_config.registry.device_mngr = device_mngr
        pyramid_config.registry.ivtconn_mngr = ivtconn_mngr
        pyramid_config.registry.camera_mngr = camera_mngr
        pyramid_config.registry.stream_mngr = stream_mngr
        pyramid_config.registry.user_session_mngr = user_session_mngr
        pyramid_config.registry.user_session_log_mngr = user_session_log_mngr
        pyramid_config.registry.user_mngr = user_mngr
        pyramid_config.registry.access_key_mngr = access_key_mngr
        pyramid_config.registry.firmware_mngr = firmware_mngr
        pyramid_config.registry.bill_mngr = bill_mngr
        pyramid_config.registry.record_storage_mngr = record_storage_mngr
        pyramid_config.registry.record_agent_mngr = record_agent_mngr
        pyramid_config.registry.record_schedule_mngr = record_schedule_mngr
        pyramid_config.registry.record_config_mngr = record_config_mngr
        pyramid_config.registry.record_statistic_mngr = record_statistic_mngr
        pyramid_config.registry.event_record_mngr = event_record_mngr
        pyramid_config.registry.wechat_mngr = wechat_mngr
        pyramid_config.registry.live_show_mngr = live_show_mngr
        pyramid_config.registry.user_vod_session_log_mngr = user_vod_session_log_mngr
        pyramid_config.registry.move_detect_config_mngr = move_detect_config_mngr
        if config.get('debug'):
            pyramid_config.add_settings({'debugtoolbar.hosts': ['0.0.0.0/0', '::1'],
                                 'debugtoolbar.enabled': True,
                                 'pyramid.debug_authorization': False,})
            pyramid_config.include('pyramid_debugtoolbar')
            pyramid_config.include('pyramid_chameleon')
        rest_server = WSGIServer(config['rest_listen'], pyramid_config.make_wsgi_app(), log=logging.getLogger('rest server'))

        # start server and wait
        if config['ws_listen']:
            ws_server = WSServer(config['ws_listen'], ivtconn_mngr.ivt_online)
            gevent.joinall(map(gevent.spawn, (ws_server.server_forever, rest_server.serve_forever)))
        else:
            gevent.joinall(map(gevent.spawn, (rest_server.serve_forever, )))
        log.info("Quit")
    except Exception:
        log.exception("Failed to start IVC")