Beispiel #1
0
def read_channels(username):
    logger.debug('user: request in read_channels: %s' % username)
    channels = []
    try:
        if username not in rediscache.read_from_redis(
                rediscache.USERNAME_LIST_KEY):
            channels_cache.sadd(rediscache.USERNAME_LIST_KEY, username)
            OutSysToRedis().render_channels_rcms(username)
        channel_redis = rediscache.read_from_redis(rediscache.CHANNELS_PREFIX,
                                                   username)
        if not channel_redis:
            # channel_maps={}
            logger.debug('readChannels not from redis instead from rcms')
            if OutSysToRedis().render_channels_rcms(username):
                channels_cache.sadd(rediscache.USERNAME_LIST_KEY, username)
            channel_redis = rediscache.read_from_redis(
                rediscache.CHANNELS_PREFIX, username)
        else:
            logger.debug('read_channels get from redis')
        if channel_redis:
            for val in channel_redis:
                channels.append(sjson.JSONDecoder().decode(val))
            channels.sort(reverse=True, key=lambda x: x['name'].split('//')[1])
    except Exception, e:
        logger.error(
            'redis api read_channels failed.username== %s ;exception==%s' %
            (username, traceback.format_exc(e)))
        logger.error(traceback.format_exc(e))
Beispiel #2
0
def isValidChannel(channel_name, username):
    logger.info('call valid channel, user:%s, channel:%s' %
                (username, channel_name))
    rediscache.append_to_channel_cache(rediscache.USERNAME_LIST_KEY, username)
    channel = rediscache.read_from_redis(rediscache.CHANNELS_PREFIX, username,
                                         channel_name)
    if not channel:
        channel = rediscache.read_from_redis(
            rediscache.CHANNELS_PREFIX, username,
            getExtensiveDomainName(channel_name))
        logger.warn('username:%s, DomainName:%s' %
                    (username, getExtensiveDomainName(channel_name)))
    channel_obj = {}
    if not channel:
        if not isInvalids(username, channel_name):
            channels_user = OutSysToRedis().render_channels_rcms(username)
            for c in channels_user:
                if is_matched(c, channel_name):
                    channel_obj = sjson.loads(channels_user.get(c))
                    break
            if not channel_obj.get('is_valid', False):
                setInvalids(username, channel_name)
    else:
        channel_obj = sjson.loads(channel)
    try:
        rediscache.append_to_channel_redis(rediscache.CHANNEL_LIST_KEY,
                                           channel_obj.get('code', 0))
    except Exception, e:
        pass
Beispiel #3
0
def read_grayDevices(channel_code):
    logger.debug('user: request in read_grayDevices from channel code = %s' %
                 channel_code)
    devices = []
    devices_redis = rediscache.read_from_redis(rediscache.GARY_DEVICES_PREFIX,
                                               channel_code)
    try:
        if not devices_redis:
            logger.debug('gray device missed in redis. for %s' % channel_code)
            # device_maps={}
            logger.debug('read Gray Devices not from redis instead from rcms')
            device_maps = OutSysToRedis().render_gary_devices_rcms(
                channel_code)
            devices_redis = rediscache.read_from_redis(
                rediscache.GARY_DEVICES_PREFIX, channel_code)
        else:
            logger.debug('success:api of read_grayDevices get from redis')
        if devices_redis:
            logger.debug('gray devices data is:{}'.format(devices_redis))
            if devices_redis[0].lower() == "true":
                return []
            for val in devices_redis:
                devices.append(sjson.JSONDecoder().decode(val))
    except Exception, e:
        logger.error(
            'fail: get firstdevice from redisapi failed. read_firstLayerDevices of channel: %r'
            % channel_code)
        logger.error(traceback.format_exc(e))
Beispiel #4
0
def read_firstLayerDevices(channel_code):
    logger.debug(
        'user: request in read_firstLayerDevices from channel code = %s' %
        channel_code)
    devices = []
    devices_redis = rediscache.read_from_redis(
        rediscache.FIRSTLAYER_DEVICES_PREFIX, channel_code)
    try:
        if not devices_redis:
            logger.debug('device missed in redis. for %s' % channel_code)
            # device_maps={}
            logger.debug('readDevices not from redis instead from rcms')
            device_maps = OutSysToRedis().render_devices_rcms(
                channel_code, False)
            devices_redis = rediscache.read_from_redis(
                rediscache.FIRSTLAYER_DEVICES_PREFIX, channel_code)
        else:
            logger.debug(
                'success:api of read_firstLayerDevices get from redis')
        if devices_redis:
            for val in devices_redis:
                devices.append(sjson.JSONDecoder().decode(val))
    except Exception, e:
        logger.error(
            'fail: get firstdevice from redisapi failed. read_firstLayerDevices of channel: %r'
            % channel_code)
        logger.error(traceback.format_exc(e))
Beispiel #5
0
def read_devices(channel_code):
    logger.debug('user: request in read_devices from channel code = %s' %
                 str(channel_code))
    devices = []
    devices_redis = rediscache.read_from_redis(rediscache.DEVICES_PREFIX,
                                               channel_code)
    try:
        if not devices_redis:
            logger.debug('device miss in redis. for %s' % channel_code)
            # device_maps={}
            logger.debug('readDevices not from redis instead from rcms')
            OutSysToRedis().render_devices_rcms(channel_code)
            devices_redis = rediscache.read_from_redis(
                rediscache.DEVICES_PREFIX, channel_code)
        else:
            logger.debug(
                'read_devices of channel : %s get device is  from redis ' %
                str(channel_code))
        if devices_redis:
            for val in devices_redis:
                devices.append(sjson.JSONDecoder().decode(val))
    except Exception, e:
        logger.error(
            'get devices from redisapi failed. read_devices of channel: %s' %
            str(channel_code))
        logger.error(traceback.format_exc(e))
Beispiel #6
0
def read_channels_by_portal(username):
    logger.debug('user: request in read_channels: %s' % username)
    channels = []
    try:
        channel_redis = rediscache.read_from_redis(
            rediscache.CHANNELS_PORTAL_PREFIX, username)
        if not channel_redis:
            # channel_maps={}
            logger.debug('readChannels not from redis instead from portal')
            api_mongo.sync_user_channel_portals_by_username(username)
            channel_redis = rediscache.read_from_redis(
                rediscache.CHANNELS_PORTAL_PREFIX, username)
        else:
            logger.debug('read_channels get from redis')
        if channel_redis:
            for val in channel_redis:
                channels.append(sjson.JSONDecoder().decode(val))
            channels.sort(reverse=True, key=lambda x: x['name'].split('//')[1])
    except Exception, e:
        logger.error(
            'redis api read_channels failed.username== %s ;exception==%s' %
            (username, traceback.format_exc(e)))
        logger.error(traceback.format_exc(e))