4.1. Etäasiakas – API:n käyttöopas
Yleiskatsaus
Adax API on toteutettu REST-palveluna, ja sen avulla voit:
- Hakea huoneiden tavoite- ja nykyiset lämpötilat,
- Muuttaa huoneiden tavoitelämpötiloja.
Aloitusopas
Adax API:n turvallista käyttöä varten tarvitset autentikointitunnuksen sekä perustiedot REST-palvelujen käytöstä. API on saatavilla osoitteessa: https://api-1.adax.no/client-api/
Palvelujen kuvaus on saatavilla OpenAPI-muodossa osoitteessa: https://api-1.adax.no/client-api/
Voit käyttää myös tarjottuja Python- ja Java-esimerkkejä aloituspohjina.
Autentikointitunnuksen hankkiminen Adax API:ta varten
Adax WiFi -sovelluksen avulla voit luoda ja saada autentikointitunnuksen, joka tarvitaan Adax API:n käyttöön. Käytä vähintään sovellusversiota 3.5.1. Autentikointitunnuksen saat seuraamalla seuraavia vaiheita:
- Siirry Account -välilehdelle,
- Valitse ”Remote user client API”,
- Valitse ”Add Credential”,
- Anna tunnukselle nimi, kopioi ja ota talteen luotu salasana.
Adax API:n käyttö
API tarjoaa kolme päätepistettä:
- /auth/ – autentikointia varten REST-palveluun
- /rest/v1/control/ – huoneiden ohjaukseen
- /rest/v1/content/ – huoneiden tilatietojen hakemiseen
Ohjaus- ja tilatietopisteet vastaanottavat ja palauttavat JSON-muotoisia asiakirjoja.
Koodiesimerkkejä
Python-esimerkki
# install dependencies with: pip install requests sanction # install dependencies with: pip install requestssanction
import requests
import sanction
import time
import datetime
CLIENT_ID = "300288"
# replace with your client ID (see Adax WiFi app,Account Section)
CLIENT_SECRET = "xd8TdSCendw2cb6h"
# replace with your client secret (see Adax WiFi app,Account Section)
API_URL = "https://api-1.adax.no/client-api"
oauthClinet = sanction.Client(token_endpoint = API_URL + '/auth/token')
def get_token():
# Authenticate and obtain JWT token
oauthClinet.request_token(grant_type = 'password', username = CLIENT_ID, password = CLIENT_SECRET)
return oauthClinet.access_token
def refresh_token():
oauthClinet.request_token(grant_type='refresh_token', refresh_token = oauthClinet.refresh_token, username =
CLIENT_ID, password = CLIENT_SECRET)
return oauthClinet.access_token
def set_room_target_temperature(roomId, temperature, token):
# Sets target temperature of the room
headers = { "Authorization": "Bearer " + token }
json = { 'rooms': [{ 'id': roomId, 'targetTemperature': str(temperature) }] }
response = requests.post(API_URL + '/rest/v1/control/', json = json, headers = headers)
def get_energy_info(token, roomId):
headers = { "Authorization": "Bearer " + token }
response = requests.get(API_URL + "/rest/v1/energy_log/" + str(roomId), headers = headers)
json = response.json()
for log in json['points']:
fromTime = datetime.datetime.utcfromtimestamp(int(log['fromTime']) / 1000).strftime('%Y-%m-%d %H:%M:%S')
toTime = datetime.datetime.utcfromtimestamp(int(log['toTime']) / 1000).strftime('%Y-%m-%d %H:%M:%S')
energy = log['energyWh'];
print("From: %15s, To: %15s, %5dwh" % (fromTime, toTime, energy))
def get_homes_info(token):
headers = { "Authorization": "Bearer " + token }
response = requests.get(API_URL + "/rest/v1/content/?withEnergy=1", headers = headers)
print(response)
json = response.json()
for room in json['rooms']:
roomName = room['name']
if ('targetTemperature' in room):
targetTemperature = room['targetTemperature'] / 100.0
else:
targetTemperature = 0
if ('temperature' in room):
currentTemperature = room['temperature'] / 100.0
else
currentTemperature = 0
print("Room: %15s, Target: %5.2fC, Temperature: %5.2fC, id: %5d" % (roomName, targetTemperature, currentTemperature, room['id']))
if ('devices' in json):
for device in json['devices']:
deviceName = device['name']
energy = device['energyWh'];
energyTime = datetime.datetime.utcfromtimestamp(int(device['energyTime']) / 1000).strftime('%Y-%m-% d %H:%M:%S')
print("Device: %15s, Time: %15s, Energy: %5dwh, id: %5d" % (deviceName, energyTime, energy, device ['id']))
token = get_token()
while True:
time.sleep(10)
# Change the temperature to 24 C in the room with an Id of 196342
set_room_target_temperature(735362, 2400, token)
# Replace the 196342 with the room id from the
get_homes_info output
time.sleep(10)
set_room_target_temperature(750269, 2400, token)
# Replace the 196342 with the room id from the
get_homes_info output
time.sleep(10)
get_homes_info(token)
time.sleep(10)
get_energy_info(token, 735362)
token = refresh_token()