Querying portia - Data fetching with PHP

Making HTTP requests using Python - Checking credentials

  • ### Unsucessfull request

In [1]:
// Portia service URL for token authorization checking
$url = "http://io.portia.supe.solutions/api/v1/accesstoken/check";

// Creates a stream for HTTP request
$options = [
    "http" => [
        "method" => "GET"
    ]
];
$context = stream_context_create($options);

// Makes the request
$response = file_get_contents($url, false, $context);


Out[1]:
PHP warning:  file_get_contents(http://io.portia.supe.solutions/api/v1/accesstoken/check): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
 in /opt/jupyter-php/pkgs/vendor/litipk/jupyter-php/src/Actions/ExecuteAction.php(115) : eval()'d code on line 6
  • ### Sucessfull request

In [2]:
// Portia service URL for token authorization checking
$url = "http://io.portia.supe.solutions/api/v1/accesstoken/check";

// Creates a stream for HTTP request
$options = [
    "http" => [
        "method" => "GET",
        "header" => "Authorization: Bearer bdb6e780b43011e7af0b67cba486057b\r\n" // Setting the header with a token for successful authorization
    ]
];
$context = stream_context_create($options);

// Makes the request
$response = file_get_contents($url, false, $context);

// Shows response
if($http_response_header[0] == "HTTP/1.1 200 OK") {
    echo("Success accessing Portia Service - Status: " . $http_response_header[0] . "\n" . $response);
} else {
    echo("Couldn't access Portia service - Status: " . $http_response_header[0]);
}


Out[2]:
Success accessing Portia Service - Status: HTTP/1.1 200 OK
{"user":"teste","isLoggedIn":true}

Obtaining data from a specific time frame

Now that we have learned how to authenticate with the service, let's see how to get the data


In [8]:
// Example for getting the last 5 minutes of data
$fiveMinutes = 1000 * 60 * 5;
$toTimestamp = time() * 1000; // The time function only gives us the UTC time as seconds since January 1, 1970, so, we multiply by 1000 to get the milliseconds
$fromTimestamp = $toTimestamp - $fiveMinutes;

// Portia service URL for specific time frame
$url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1";

// Adding the calculated timestamps as GET parameters
$url = $url . "?from_timestamp=$fromTimestamp&?to_timestamp=$toTimestamp"; // If no parameters, the service default response is for the last 24 hours

// Creates a stream for HTTP request
$options = [
    "http" => [
        "method" => "GET",
        "header" => "Authorization: Bearer bdb6e780b43011e7af0b67cba486057b\r\n" // Setting the header with a token for successful authorization
    ]
];
$context = stream_context_create($options);

// Makes the request
$response = file_get_contents($url, false, $context);

// Shows response
if($http_response_header[0] == "HTTP/1.1 200 OK") {
    // Parses dimensions
    $dimensions = json_decode($response);

    echo("Success! For each received dimension: ");
    foreach($dimensions as $dimension) {
        echo("Accessing dimension package:");
        echo("\tDimension Code: " . $dimension -> dimension_code);
        echo("\tUnity Code: " . $dimension -> dimension_unity_code);
        echo("\tThing Code: " . $dimension -> dimension_thing_code);
        echo("\tDimension Value: " . $dimension -> dimension_value);
        echo("\tServer Timestamp: " . $dimension -> server_timestamp);
    }

} else {
    echo("Couldn't access Portia service - Status: " . $http_response_header[0]);
}


Out[8]:
Success! For each received dimension: 
Out[8]:
Accessing dimension package:
Out[8]:
	Dimension Code: 1
Out[8]:
	Unity Code: 1
Out[8]:
	Thing Code: 1
Out[8]:
	Dimension Value: 20.8
Out[8]:
	Server Timestamp: 1508785157578
Out[8]:
Accessing dimension package:
Out[8]:
	Dimension Code: 1
Out[8]:
	Unity Code: 1
Out[8]:
	Thing Code: 1
Out[8]:
	Dimension Value: 20.9
Out[8]:
	Server Timestamp: 1508785094581
Out[8]:
Accessing dimension package:
Out[8]:
	Dimension Code: 1
Out[8]:
	Unity Code: 1
Out[8]:
	Thing Code: 1
Out[8]:
	Dimension Value: 20.9
Out[8]:
	Server Timestamp: 1508785034294
Out[8]:
Accessing dimension package:
Out[8]:
	Dimension Code: 1
Out[8]:
	Unity Code: 1
Out[8]:
	Thing Code: 1
Out[8]:
	Dimension Value: 21
Out[8]:
	Server Timestamp: 1508784973303
Out[8]:
Accessing dimension package:
Out[8]:
	Dimension Code: 1
Out[8]:
	Unity Code: 1
Out[8]:
	Thing Code: 1
Out[8]:
	Dimension Value: 21.1
Out[8]:
	Server Timestamp: 1508784913444

Obtaining the latest data

For the next example, we are requesting only the last data sent by the equipments

  • ### Last dimension

In [11]:
// Portia service URL for getting the latest data
$url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1/last";

// Creates a stream for HTTP request
$options = [
    "http" => [
        "method" => "GET",
        "header" => "Authorization: Bearer bdb6e780b43011e7af0b67cba486057b\r\n" // Setting the header with a token for successful authorization
    ]
];
$context = stream_context_create($options);

// Makes the request
$response = file_get_contents($url, false, $context);

// Shows response
if($http_response_header[0] == "HTTP/1.1 200 OK") {
    // Parses dimensions
    $dimension = json_decode($response)[0];
    echo("Success! Accessing dimension package:");
    echo("\tDimension Code: " . $dimension -> dimension_code);
    echo("\tUnity Code: " . $dimension -> dimension_unity_code);
    echo("\tThing Code: " . $dimension -> dimension_thing_code);
    echo("\tDimension Value: " . $dimension -> dimension_value);
    echo("\tServer Timestamp: " . $dimension -> server_timestamp);

} else {
    echo("Couldn't access Portia service - Status: " . $http_response_header[0]);
}


Out[11]:
Success! Accessing dimension package:
Out[11]:
	Dimension Code: 1
Out[11]:
	Unity Code: 1
Out[11]:
	Thing Code: 1
Out[11]:
	Dimension Value: 20.7
Out[11]:
	Server Timestamp: 1508785396842
  • ### Last three dimensions

In [12]:
// Portia service URL for getting the latest data
$url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1/last";

# Adding GET parameter for specifying that we want the last 3 dimension packages
$url = $url . "?limit=3";

// Creates a stream for HTTP request
$options = [
    "http" => [
        "method" => "GET",
        "header" => "Authorization: Bearer bdb6e780b43011e7af0b67cba486057b\r\n" // Setting the header with a token for successful authorization
    ]
];
$context = stream_context_create($options);

// Makes the request
$response = file_get_contents($url, false, $context);

// Shows response
if($http_response_header[0] == "HTTP/1.1 200 OK") {
    // Parses dimensions
    $dimensions = json_decode($response);

    echo("Success! For each received dimension: ");
    foreach($dimensions as $dimension) {
        echo("Accessing dimension package:");
        echo("\tDimension Code: " . $dimension -> dimension_code);
        echo("\tUnity Code: " . $dimension -> dimension_unity_code);
        echo("\tThing Code: " . $dimension -> dimension_thing_code);
        echo("\tDimension Value: " . $dimension -> dimension_value);
        echo("\tServer Timestamp: " . $dimension -> server_timestamp);
    }

} else {
    echo("Couldn't access Portia service - Status: " . $http_response_header[0]);
}


Out[12]:
Success! For each received dimension: 
Out[12]:
Accessing dimension package:
Out[12]:
	Dimension Code: 1
Out[12]:
	Unity Code: 1
Out[12]:
	Thing Code: 1
Out[12]:
	Dimension Value: 20.9
Out[12]:
	Server Timestamp: 1508785518063
Out[12]:
Accessing dimension package:
Out[12]:
	Dimension Code: 1
Out[12]:
	Unity Code: 1
Out[12]:
	Thing Code: 1
Out[12]:
	Dimension Value: 20.8
Out[12]:
	Server Timestamp: 1508785457421
Out[12]:
Accessing dimension package:
Out[12]:
	Dimension Code: 1
Out[12]:
	Unity Code: 1
Out[12]:
	Thing Code: 1
Out[12]:
	Dimension Value: 20.7
Out[12]:
	Server Timestamp: 1508785396842