Exemplo n.º 1
0
def device_ports(
    portia_config: dict,
    edge_id: str,
    last: bool=False,
    params: dict={
        'from': None,
        'to': None,
        'sort': True,
        'precision': 'ms',
        'timezone': 'Etc/UTC'
}) -> object:
    """Lists a device's ports.
    
    Arguments:
        portia_config {dict} -- Portia's configuration arguments 
        edge_id {str} -- Edge ID that identifies the device
    
    Keyword Arguments:
        last {bool} -- if the last package of each port should be returned or
                       not (default: {False})
        params {dict} -- params to send to the service (default:
                         {{ 'from', 'to', 'sort', 'precision': 'ms',
                         'timezone': 'Etc/UTC' }})
    
    Returns:
        object -- object with the list of ports
    
    Raises:
        Exception -- when the request goes wrong
    """
    if last == False:
        endpoint = '/describe/device/{0}/ports'.format(edge_id)
    else:
        endpoint = '/describe/device/{0}/ports/last'.format(edge_id)

    response = utils.http_get_request(portia_config, endpoint, params)

    if response.status_code == 200:

        d = json.loads(response.text).get('ports')

        if portia_config.get('debug'):
            print('[portia-debug]: {0}'.format(d))

        if last == True:
            d = pd.DataFrame(
                d,
                columns=['header_timestamp', 'port', 'dimension_thing_code']
            )

            d['port'] = d['port'].map(int)
            d['dimension_thing_code'] = d['dimension_thing_code'].map(int)
        else:
            d = list(map(int, d))

        return d

    else:
        raise Exception("couldn't retrieve data")
Exemplo n.º 2
0
def query_by_port_sensor_dimension_event(
    portia_config: dict,
    edge_id: str,
    port: int,
    sensor: int,
    dimension_code: int,
    event_code: int,
    last: bool = False,
    params: dict = {
        'from': None,
        'to': None,
        'order': None,
        'limit': None,
        'precision': 'ms',
        'timezone': 'Etc/UTC'
    }
) -> object:
    """Retrieves a device's events by its port, sensor and dimension code.
    
    Arguments:
        portia_config {dict} -- Portia's configuration arguments
        edge_id {str} -- Edge ID that identifies the device
        port {int} -- port of the device
        sensor {int} -- sensor of the device
        dimension_code {int} -- dimension code of the device
        event_code {int} -- event code of the device
    
    Keyword Arguments:
        last {bool} -- if the last event should be returned or not
                       (default: {False})
        params {dict} -- params to send to the service (default: {{ 'from',
                         'to', 'order', 'lower_bound', 'upper_bound', 'order',
                         'limit', 'precision': 'ms', 'timezone': 'Etc/UTC' }})

    Returns:
        object -- object with the device's events
    """
    accept_header = portia_config.get('Accept')

    if accept_header is None:
        accept_header = 'text/csv'

    header = {'Accept': accept_header}

    if last == False:
        endpoint = ('/events/device/{0}/port/{1}/sensor/{2}/dimension/{3}'
                    '/event/{4}'.format(edge_id, port, sensor, dimension_code,
                                        event_code))
    else:
        endpoint = ('/events/device/{0}/port/{1}/sensor/{2}/dimension/{3}'
                    '/event/{4}/last'.format(edge_id, port, sensor,
                                             dimension_code, event_code))

    response = utils.http_get_request(portia_config,
                                      endpoint,
                                      params=params,
                                      optional_headers=header)

    return utils.convert(accept_header, portia_config, response)
Exemplo n.º 3
0
def sensor_profile(
    portia_config: dict,
    edge_id: str,
    port: int,
    sensor: int,
    strategy: ProfileStrategies = ProfileStrategies.BY_ZERO_PORT,
    interval: int = 30,
    params: dict = {
        'sort': True,
        'precision': 'ms',
        'timezone': 'Etc/UTC'
    }
) -> dict:
    """Retrieves a sensor's profile.

    Arguments:
        portia_config {dict} -- Portia's configuration arguments
        edge_id {str} -- Edge ID that identifies the device
        port {int} -- port of the device
        sensor {int} -- sensor of the device
    
    Keyword Arguments:
        strategy {ProfileStrategies} -- strategy to use when building the
                                        profile (default: 
                                        {ProfileStrategies.BY_ZERO_PORT})
        interval {int} -- interval of time in minutes to build the profile
                          (default: {30})
        params {dict} -- params to send to the service (default: 
                         {{ 'sort', 'precision': 'ms',
                         'timezone': 'Etc/UTC' }})

    Returns:
        dict -- dictionary with the sensor's profile
    """
    endpoint = '/profile/device/{0}/port/{1}/sensor/{2}/{3}/{4}'.format(
        edge_id, port, sensor, strategy.endpoint, interval)

    response = utils.http_get_request(portia_config, endpoint, params)

    if response.status_code == 200:

        d = json.loads(response.text)
        if portia_config['debug']:
            print('[portia-debug]: {0}'.format(d.get('ports')))

        return d

    else:
        raise Exception("couldn't retrieve data")
Exemplo n.º 4
0
def query_by_port_sensor_dimension(
    portia_config: dict,
    edge_id: str,
    port: int,
    sensor: int,
    dimension_code: int,
    strategy: SummaryStrategies = SummaryStrategies.PER_HOUR,
    interval: int = 1,
    params: dict = {
        'from': None,
        'to': None,
        'lower_bound': None,
        'upper_bound': None,
        'offset': 0,
        'fill': None,
        'order': None,
        'limit': None,
        'avg': True,
        'min': True,
        'max': True,
        'sum': False,
        'median': False,
        'mode': False,
        'stddev': False,
        'spread': False,
        'last_timestamp': False,
        'precision': 'ms',
        'timezone': 'Etc/UTC'
    }
) -> object:
    """Summarizes a device by port, sensor and dimension code.
    
    Arguments:
        portia_config {dict} -- Portia's configuration arguments
        edge_id {str} -- Edge ID that identifies the device
        port {int} -- port of the device
        sensor {int} -- sensor of the device
        dimension_code {int} -- dimension code of the device
    
    Keyword Arguments:
        strategy {SummaryStrategies} -- strategy to use when summarizing
                                        (default: {SummaryStrategies.PER_HOUR})
        interval {int} -- interval of time to summarize (default: {1})
        params {dict} -- params to send to the service (default: {{ 'from',
                         'to', 'lower_bound', 'upper_bound', 'offset': 0,
                         'fill', 'order', 'limit', 'min', 'max', 'sum', 'avg',
                         'median', 'mode', 'stddev', 'spread',
                         'last_timestamp', precision': 'ms',
                         'timezone': 'Etc/UTC'}})

    Returns:
        object -- object with the device's summarized dimensions
    """
    accept_header = portia_config.get('Accept')

    if accept_header is None:
        accept_header = 'text/csv'

    header = {'Accept': accept_header}

    endpoint = ('/summary/device/{0}/port/{1}/sensor/{2}/dimension/{3}/{4}'
                '/{5}'.format(edge_id, port, sensor, dimension_code,
                              strategy.endpoint, interval))

    response = utils.http_get_request(portia_config,
                                      endpoint,
                                      params=params,
                                      optional_headers=header)

    return utils.convert(accept_header, portia_config, response)