Example #1
0
 def setUp(self):
     self.camera_mngr = CameraManager(CameraDAO(), DummyDeviceManager())
Example #2
0
class TestCameraMngr(unittest.TestCase):
    def setUp(self):
        self.camera_mngr = CameraManager(CameraDAO(), DummyDeviceManager())

    def tearDown(self):
        self.camera_mngr = None

    def test_add_delete_cameras(self):
        camera_list = [
            {
                'camera': Camera('p1', 'c1', device_uuid='d1',
                                 channel_index=0),
                'cameras_create': {
                    'p1': ['c1'],
                    'p2': []
                },
                'cameras_delete': {
                    'p1': ['c2', 'c4'],
                    'p2': ['c3']
                },
            },
            {
                'camera': Camera('p1', 'c2', device_uuid='d1',
                                 channel_index=1),
                'cameras_create': {
                    'p1': ['c1', 'c2'],
                    'p2': []
                },
                'cameras_delete': {
                    'p1': ['c4'],
                    'p2': ['c3']
                },
            },
            {
                'camera': Camera('p2', 'c3', device_uuid='d2',
                                 channel_index=0),
                'cameras_create': {
                    'p1': ['c1', 'c2'],
                    'p2': ['c3']
                },
                'cameras_delete': {
                    'p1': ['c4'],
                    'p2': []
                },
            },
            {
                'camera': Camera('p1', 'c4', device_uuid='d1',
                                 channel_index=2),
                'cameras_create': {
                    'p1': ['c1', 'c2', 'c4'],
                    'p2': ['c3']
                },
                'cameras_delete': {
                    'p1': [],
                    'p2': []
                },
            },
        ]
        # add cameras
        for data in camera_list:
            self.assertIsNone(
                self.camera_mngr.get_camera(data['camera'].project_name,
                                            data['camera'].uuid))
            self.camera_mngr.add_camera(data['camera'])
            for p, cs in data['cameras_create'].iteritems():
                self.assertEqual(self.camera_mngr.get_camera_count(p), len(cs))
                dao_cameras = self.camera_mngr.get_camera_list(p,
                                                               start=0,
                                                               limit=100)
                self.assertEqual(len(dao_cameras), len(cs))
                for dao_c in dao_cameras:
                    self.assertIn(dao_c.uuid, cs)
        # delete cameras
        for data in camera_list:
            self.camera_mngr.delete_camera(data['camera'].project_name,
                                           data['camera'])
            for p, cs in data['cameras_delete'].iteritems():
                self.assertEqual(self.camera_mngr.get_camera_count(p), len(cs))
                dao_cameras = self.camera_mngr.get_camera_list(p,
                                                               start=0,
                                                               limit=100)
                self.assertEqual(len(dao_cameras), len(cs))
                for dao_c in dao_cameras:
                    self.assertIn(dao_c.uuid, cs)

    def test_not_allow_get_camera_with_wrong_project(self):
        self.camera_mngr.add_camera(
            Camera('p1', 'c1', device_uuid='d1', channel_index=0))
        self.assertIsNone(self.camera_mngr.get_camera('p2', 'c1'))

    def test_not_allow_delete_camera_with_wrong_project(self):
        self.camera_mngr.add_camera(
            Camera('p1', 'c1', device_uuid='d1', channel_index=0))
        self.camera_mngr.delete_camera(
            'p1', Camera('p2', 'c1', device_uuid='d1', channel_index=0))
        camera = self.camera_mngr.get_camera('p1', 'c1')
        self.assertIsNotNone(camera)
        self.assertEqual(camera.project_name, 'p1')
        self.assertEqual(camera.uuid, 'c1')
Example #3
0
File: main.py Project: 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")
Example #4
0
 def setUp(self):
     self.camera_mngr = CameraManager(CameraDAO(), DummyDeviceManager())
Example #5
0
class TestCameraMngr(unittest.TestCase):

    def setUp(self):
        self.camera_mngr = CameraManager(CameraDAO(), DummyDeviceManager())

    def tearDown(self):
        self.camera_mngr = None

    def test_add_delete_cameras(self):
        camera_list = [
            {
                'camera': Camera('p1', 'c1', device_uuid='d1', channel_index=0),
                'cameras_create': {'p1': ['c1'], 'p2': []},
                'cameras_delete': {'p1': ['c2', 'c4'], 'p2': ['c3']},
            },
            {
                'camera': Camera('p1', 'c2', device_uuid='d1', channel_index=1),
                'cameras_create': {'p1': ['c1', 'c2'], 'p2': []},
                'cameras_delete': {'p1': ['c4'], 'p2': ['c3']},
            },
            {
                'camera': Camera('p2', 'c3', device_uuid='d2', channel_index=0),
                'cameras_create': {'p1': ['c1', 'c2'], 'p2': ['c3']},
                'cameras_delete': {'p1': ['c4'], 'p2': []},
            },
            {
                'camera': Camera('p1', 'c4', device_uuid='d1', channel_index=2),
                'cameras_create': {'p1': ['c1', 'c2', 'c4'], 'p2': ['c3']},
                'cameras_delete': {'p1': [], 'p2': []},
            },
        ]
        # add cameras
        for data in camera_list:
            self.assertIsNone(self.camera_mngr.get_camera(data['camera'].project_name, data['camera'].uuid))
            self.camera_mngr.add_camera(data['camera'])
            for p, cs in data['cameras_create'].iteritems():
                self.assertEqual(self.camera_mngr.get_camera_count(p), len(cs))
                dao_cameras = self.camera_mngr.get_camera_list(p, start=0, limit=100)
                self.assertEqual(len(dao_cameras), len(cs))
                for dao_c in dao_cameras:
                    self.assertIn(dao_c.uuid, cs)
        # delete cameras
        for data in camera_list:
            self.camera_mngr.delete_camera(data['camera'].project_name, data['camera'])
            for p, cs in data['cameras_delete'].iteritems():
                self.assertEqual(self.camera_mngr.get_camera_count(p), len(cs))
                dao_cameras = self.camera_mngr.get_camera_list(p, start=0, limit=100)
                self.assertEqual(len(dao_cameras), len(cs))
                for dao_c in dao_cameras:
                    self.assertIn(dao_c.uuid, cs)

    def test_not_allow_get_camera_with_wrong_project(self):
        self.camera_mngr.add_camera(Camera('p1', 'c1', device_uuid='d1', channel_index=0))
        self.assertIsNone(self.camera_mngr.get_camera('p2', 'c1'))

    def test_not_allow_delete_camera_with_wrong_project(self):
        self.camera_mngr.add_camera(Camera('p1', 'c1', device_uuid='d1', channel_index=0))
        self.camera_mngr.delete_camera('p1', Camera('p2', 'c1', device_uuid='d1', channel_index=0))
        camera = self.camera_mngr.get_camera('p1', 'c1')
        self.assertIsNotNone(camera)
        self.assertEqual(camera.project_name, 'p1')
        self.assertEqual(camera.uuid, 'c1')
Example #6
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")