# Library for HTTP requests
import requests

# Portia service URL for token authorization checking
url = "http://io.portia.supe.solutions/api/v1/accesstoken/check"

# Makes the request
response = requests.get(url)

# Shows response
if response.status_code == 200:
    print("Success accessing Portia Service - Status Code: {0}\n{1}".format(response.status_code, response.text))
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


"""
Explanation: Querying portia - Data fetching with Python
Making HTTP requests using Python - Checking credentials


Unsucessfull request
End of explanation
"""


# Library for HTTP requests
import requests

# Portia service URL for token authorization checking
url = "http://io.portia.supe.solutions/api/v1/accesstoken/check"

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    print("Success accessing Portia Service - Status Code: {0}\n{1}".format(response.status_code, response.text))
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


"""
Explanation: Sucessfull request
End of explanation
"""


import requests      # Library for HTTP requests
import time as epoch # Library for timing functions
import json          # Library for JSON usage

# Example for getting the last 5 minutes of data
fiveMinutes = 1000 * 60 * 5
toTimestamp = int(epoch.time()) * 1000 # The time lib 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 += "?from_timestamp={0}&?to_timestamp={1}".format(fromTimestamp, toTimestamp) # If no parameters, the service default response is for the last 24 hours

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimensions
    dimensions = json.loads(response.text)
    
    print("Success! For each received dimension:")
    for dimension in dimensions:
        print("Accessing dimension package:")
        print("\tDimension Code: {0}".format(dimension["dimension_code"]))
        print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
        print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
        print("\tDimension Value: {0}".format(dimension["dimension_value"]))
        print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


"""
Explanation: 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
End of explanation
"""


import requests      # Library for HTTP requests
import json          # Library for JSON usage

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

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimension
    dimension = json.loads(response.text)[0]
    print("Success! Accessing dimension package:")
    print("\tDimension Code: {0}".format(dimension["dimension_code"]))
    print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
    print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
    print("\tDimension Value: {0}".format(dimension["dimension_value"]))
    print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


"""
Explanation: Obtaining the latest data
For the next example, we are requesting only the last data sent by the equipments


Last dimension
End of explanation
"""


import requests      # Library for HTTP requests
import json          # Library for JSON usage

# 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 += "?limit={0}".format(3)

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimensions
    dimensions = json.loads(response.text)
    
    print("Success! For each received dimension:")
    for dimension in dimensions:
        print("Accessing dimension package:")
        print("\tDimension Code: {0}".format(dimension["dimension_code"]))
        print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
        print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
        print("\tDimension Value: {0}".format(dimension["dimension_value"]))
        print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


"""
Explanation: Last three dimensions
End of explanation
"""


