예제 #1
0
 def _update_resource(self, active_resource_ids=[]):
     for res in resource.list_resource(status=True,
                                       gateway_id=self.gateway_id):
         res_id = res.get('id')
         # res_uuid = res.get('uuid')
         if res_id and res_id not in active_resource_ids:
             resource.update_resource(res_id, status=False)
예제 #2
0
def get_historical_data(message):
    """
    Get average sensor data in a period of time
    """
    sensor = message['sensor']
    date_range = message['date_range']
    gateway_id = session['gateway_id']
    print sensor, date_range, gateway_id
    res_list = resource.list_resource(gateway_id=gateway_id)
    res_id = [res['id'] for res in res_list if res['sensor_type']['mapping_class'] == sensor]

    data = []
    if res_id:
        start_time = date_range[0]
        end_time = date_range[1]
        data = _get_historical_data(sensor, start_time, end_time, res_id)

    print res_id, data
    emit('my ' + sensor, {'data': data})
예제 #3
0
def _get_sensor_data(token_dict):
    res = resource.list_resource(status=1, gateway_id=session['gateway_id'])
    ret = {'alert': {}, 'status': {}, 'data': {}}
    for sensor in res:
        typ = sensor.get('sensor_type').get('type')
        if typ in ALERT_GRP:
            latest_data = util.get_class(
                "DB.api.{}.get_latest_alert_by_gateway_uuid".format(typ))(
                    gateway_id=session['gateway_id'],
                    uuid=sensor.get('uuid'),
                    token=token_dict[typ + '-token'])
        elif typ == 'power':
            latest_data = util.get_class(
                "DB.api.energy.get_latest_by_gateway_uuid")(
                    gateway_id=session['gateway_id'], uuid=sensor.get('uuid'))
        else:
            latest_data = util.get_class(
                "DB.api.{}.get_latest_by_gateway_uuid".format(typ))(
                    gateway_id=session['gateway_id'], uuid=sensor.get('uuid'))
        if latest_data is None:
            continue
        if typ in ALERT_GRP:
            ret['alert'].update({typ: latest_data.get('created_at')})
        elif typ in STATUS_GRP:
            if typ == "rgbled":
                val = True if latest_data.get(
                    'rgbvalue') == "255,0,0" else False
                ret['status'].update({typ: val})
            else:
                ret['status'].update({typ: latest_data.get('status')})
        elif typ in DATA_GRP:
            data = None
            if typ == 'temperature':
                data = str(latest_data.get('temperature'))
            elif typ == 'solar':
                data = str(latest_data.get('tiltpercentage'))
            elif typ == 'illuminance':
                data = str(latest_data.get('illuminance'))
            elif typ == 'power':
                data = str(latest_data.get('value'))
            ret['data'].update({typ: data})
    return ret
예제 #4
0
def _get_sensor_data(token_dict):
    res = resource.list_resource(status=1, gateway_id=session['gateway_id'])
    ret = {
        'alert': {},
        'status': {},
        'data': {}
    }
    for sensor in res:
        typ = sensor.get('sensor_type').get('type')
        if typ in ALERT_GRP:
            latest_data = util.get_class("DB.api.{}.get_latest_alert_by_gateway_uuid".format(typ))(gateway_id=session['gateway_id'],
                                                                                                   uuid=sensor.get('uuid'),
                                                                                                   token=token_dict[typ+'-token'])
        elif typ == 'power':
            latest_data = util.get_class("DB.api.energy.get_latest_by_gateway_uuid")(gateway_id=session['gateway_id'],
                                                                                     uuid=sensor.get('uuid'))
        else:
            latest_data = util.get_class("DB.api.{}.get_latest_by_gateway_uuid".format(typ))(gateway_id=session['gateway_id'],
                                                                                             uuid=sensor.get('uuid'))
        if latest_data is None:
            continue
        if typ in ALERT_GRP:
            ret['alert'].update({typ: latest_data.get('created_at')})
        elif typ in STATUS_GRP:
            if typ == "rgbled":
                val = True if latest_data.get('rgbvalue') == "255,0,0" else False
                ret['status'].update({typ: val})
            else:
                ret['status'].update({typ: latest_data.get('status')})
        elif typ in DATA_GRP:
            data = None
            if typ == 'temperature':
                data = str(latest_data.get('temperature'))
            elif typ == 'solar':
                data = str(latest_data.get('tiltpercentage'))
            elif typ == 'illuminance':
                data = str(latest_data.get('illuminance'))
            elif typ == 'power':
                data = str(latest_data.get('value'))
            ret['data'].update({typ: data})
    return ret
예제 #5
0
def get_historical_data(message):
    """
    Get average sensor data in a period of time
    """
    sensor = message['sensor']
    date_range = message['date_range']
    gateway_id = session['gateway_id']
    print sensor, date_range, gateway_id
    res_list = resource.list_resource(gateway_id=gateway_id)
    res_id = [
        res['id'] for res in res_list
        if res['sensor_type']['mapping_class'] == sensor
    ]

    data = []
    if res_id:
        start_time = date_range[0]
        end_time = date_range[1]
        data = _get_historical_data(sensor, start_time, end_time, res_id)

    print res_id, data
    emit('my ' + sensor, {'data': data})
예제 #6
0
def _get_sensor_data(token_dict):
    res = resource.list_resource(status=1, gateway_id=session['gateway_id'])
    default_token = util.format_datetime(util.get_utc_now() -
                                         datetime.timedelta(minutes=1))
    ret = {'alert': {}, 'status': {}, 'data': {}, 'brillo': {}, 'generic': {}}
    for sensor in res:
        typ = sensor.get('sensor_type').get('mapping_class')
        href = sensor.get('path')
        resource_id = sensor.get("id")
        if href.startswith("/a/"):
            if typ in ALERT_GRP:
                token = token_dict.get(str(resource_id)) if str(resource_id) in token_dict.keys() \
                                                            and token_dict.get(str(resource_id)) else default_token
                latest_data = util.get_class(
                    "DB.api.{}.get_latest_alert_by_gateway_uuid".format(typ))(
                        resource_id=resource_id, token=token)
                latest_data = latest_data if latest_data else {
                    "resource_id": resource_id
                }
            elif typ == 'power':
                latest_data = util.get_class(
                    "DB.api.energy.get_latest_by_gateway_uuid".format(typ))(
                        resource_id=resource_id)
            else:
                latest_data = util.get_class(
                    "DB.api.{}.get_latest_by_gateway_uuid".format(typ))(
                        resource_id=resource_id)
            if typ == 'buzzer':
                status_data = util.get_class(
                    "DB.api.{}.get_latest_by_gateway_uuid".format(typ))(
                        resource_id=resource_id)
            if latest_data is None:
                continue
            # print latest_data
            if typ in ALERT_GRP:
                _compose_sensor_data(typ, latest_data, 'created_at', 'alert',
                                     ret)

            if typ in STATUS_GRP:
                if typ == "rgbled":
                    val = True if latest_data.get(
                        'rgbvalue') == "[255, 0, 0]" else False
                    _compose_sensor_data(typ, latest_data, {'value': val},
                                         'status', ret)
                elif typ == 'buzzer':
                    _compose_sensor_data(typ, status_data, 'status', 'status',
                                         ret)
                else:
                    _compose_sensor_data(typ, latest_data, 'status', 'status',
                                         ret)
            elif typ in DATA_GRP:
                # extract values from the db query result
                if typ in ['temperature', 'illuminance']:
                    key_words = [typ]
                elif typ == 'solar':
                    key_words = ['tiltpercentage']
                elif typ == 'power':
                    key_words = ['value']
                elif typ == 'environment':
                    key_words = [
                        'temperature', 'humidity', 'pressure', 'uv_index'
                    ]

                for key in key_words:
                    sensor_type = typ if typ != "environment" else key
                    _compose_sensor_data(sensor_type, latest_data, key, 'data',
                                         ret)
            elif typ == "generic":
                _compose_sensor_data(typ, latest_data, 'json_data', 'generic',
                                     ret)
        elif href.startswith("/brillo/"):
            latest_data = util.get_class(
                "DB.api.{}.get_latest_by_gateway_uuid".format(typ))(
                    resource_id=resource_id)
            # print latest_data
            if latest_data is None:
                uuid = sensor.get('uuid')
                if ret['brillo'].get(uuid) is None:
                    ret['brillo'].update(
                        {uuid: {
                            typ: {
                                'resource_id': resource_id
                            }
                        }})
                else:
                    ret['brillo'][uuid].update(
                        {typ: {
                            'resource_id': resource_id
                        }})
                continue
            if typ in BRILLO_GRP:
                if typ in ['brightness']:
                    keys = [typ]
                elif typ == 'rgbled':
                    keys = ['rgbvalue']
                elif typ == 'audio':
                    keys = ['volume', 'mute']
                elif typ == 'mp3player':
                    keys = ['media_states', 'playlist', 'state', 'title']

                _compose_sensor_data(typ, latest_data, keys, 'brillo', ret)
    return ret
예제 #7
0
 def _update_resource(self, active_resource_ids=[]):
     for res in resource.list_resource(status=True, gateway_id=self.gateway_id):
         res_id = res.get('id')
         # res_uuid = res.get('uuid')
         if res_id and res_id not in active_resource_ids:
             resource.update_resource(res_id, status=False)
예제 #8
0
def _get_sensor_data(token_dict):
    res = resource.list_resource(status=1, gateway_id=session['gateway_id'])
    default_token = util.format_datetime(util.get_utc_now() - datetime.timedelta(minutes=1))
    ret = {
        'alert': {},
        'status': {},
        'data': {},
        'brillo': {},
        'generic': {}
    }
    for sensor in res:
        typ = sensor.get('sensor_type').get('mapping_class')
        href = sensor.get('path')
        resource_id = sensor.get("id")
        if href.startswith("/a/"):
            if typ in ALERT_GRP:
                token = token_dict.get(str(resource_id)) if str(resource_id) in token_dict.keys() \
                                                            and token_dict.get(str(resource_id)) else default_token
                latest_data = util.get_class("DB.api.{}.get_latest_alert_by_gateway_uuid"
                                             .format(typ))(resource_id=resource_id,
                                                           token=token)
                latest_data = latest_data if latest_data else {"resource_id": resource_id}
            elif typ == 'power':
                latest_data = util.get_class("DB.api.energy.get_latest_by_gateway_uuid".format(typ))(resource_id=resource_id)
            else:
                latest_data = util.get_class("DB.api.{}.get_latest_by_gateway_uuid".format(typ))(resource_id=resource_id)
            if latest_data is None:
                continue
            # print latest_data
            if typ in ALERT_GRP:
                _compose_sensor_data(typ, latest_data, 'created_at', 'alert', ret)
            elif typ in STATUS_GRP:
                if typ == "rgbled":
                    val = True if latest_data.get('rgbvalue') == "[255, 0, 0]" else False
                    _compose_sensor_data(typ, latest_data, {'value': val}, 'status', ret)
                else:
                    _compose_sensor_data(typ, latest_data, 'status', 'status', ret)
            elif typ in DATA_GRP:
                # extract values from the db query result
                if typ in ['temperature', 'illuminance']:
                    key_words = [typ]
                elif typ == 'solar':
                    key_words = ['tiltpercentage']
                elif typ == 'power':
                    key_words = ['value']
                elif typ == 'environment':
                    key_words = ['temperature', 'humidity', 'pressure', 'uv_index']

                for key in key_words:
                    sensor_type = typ if typ != "environment" else key
                    _compose_sensor_data(sensor_type, latest_data, key, 'data', ret)
            elif typ == "generic":
                _compose_sensor_data(typ, latest_data, 'json_data', 'generic', ret)
        elif href.startswith("/brillo/"):
            latest_data = util.get_class("DB.api.{}.get_latest_by_gateway_uuid".format(typ))(
                resource_id=resource_id)
            # print latest_data
            if latest_data is None:
                continue
            if typ in BRILLO_GRP:
                if typ in ['brightness']:
                    keys = [typ]
                elif typ == 'rgbled':
                    keys = ['rgbvalue']
                elif typ == 'audio':
                    keys = ['volume', 'mute']
                elif typ == 'mp3player':
                    keys = ['media_states', 'playlist', 'state', 'title']

                _compose_sensor_data(typ, latest_data, keys, 'brillo', ret)
    return ret