This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Glamox / Adax Cloud
#1
Hello. I've written this code to access cloud token and read content from cloud, and also to write target value to the heater.
Feel free to use it, and post any updates in this thread. Also thanks to ADMIN for help with this.


Code:
--Authentication script for Adax and Glamox heaters
--
--Put this script as scheduled


require('json')
require('ssl.https')
require('socket.http')
require('ltn12')

-- Set credentials for Adax API
clientID ='xxxxxxx' --- You find this ID in your app.
clientSecret ='xxxxxxx'    -- Activate this in the app, under third party integration




function GetToken()
  request_token_url = 'https://api-1.adax.no/client-api/auth/token'
  local response_body = {}
  local request_body = 'grant_type=password&username=' .. clientID .. '&password=' .. clientSecret
  local body, code, hdrs, stat = socket.http.request{
    url = request_token_url;
    method = "POST";
    headers =
    {
     ["Content-Type"] = "application/x-www-form-urlencoded";
     ["Content-Length"] = #request_body;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret.access_token
  else
    log('request failed', body, code, hdrs, stat)
  end
end


Token = GetToken()
storage.set('AdaxToken', Token) --Writes token to storage for use in other scripts

Code:
--Script for reading contant from API. You get Homes, Rooms and Devices. You can read the actual temperature and current target value among other things.
-- Load modules


require('json')
require('ssl.https')
require('socket.http')
require('ltn12')

-- Get token from storage
Token = storage.get('GlamoxToken')




function RequestFromAdax(request)
  request_url = 'https://api-1.adax.no/client-api' .. request
  local response_body = {}
  local request_body = ""
  local body, code, hdrs, stat = socket.http.request{
    url = request_url;
    method = "GET";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Content-Length"] = #request_body;
      ["Authorization"] = "Bearer " .. Token;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    log('request failed', body, code, hdrs, stat)
  end
end



getContent = RequestFromAdax('/rest/v1/content/')


Code:
--Example for writing new target value to a room. You get roomID from /content/, use this to write to correct room. Script returns a status table.
--Adax/Glamox
--
require('json')
require('ssl.https')
require('socket.http')
require('ltn12')

--Get token from storage
Token = storage.get('GlamoxToken')


function WriteToAdax(request, request_body)
  request_url = 'https://api-1.adax.no/client-api' .. request
  local response_body = {}
  local content_length
  if type(request_body) == "string" then
    content_length = #request_body
  else
    request_body = ""
  end
 
  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
     ["Content-Type"] = "application/json";
     ["Authorization"] = "Bearer " .. Token;
     ["Content-Length"] = #request_body;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  --log(code)
      if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
     log('request failed', body, code, hdrs, stat)
  end
end


room_id = 123456
temperature = 25

body = json.encode({
    rooms = {
    { id = room_id, targetTemperature = (tostring(temperature))*100 }
  }
})



data = WriteToAdax('/rest/v1/control/', body)

The API is documented here: https://adax.no/wi-fi/api-development/
It does not have all the functions as the app does, because Adax did not want to much trafic on their servers. There will be a more functional update on the local API some months from now they say.
I have not made a script for local access, maybe someone will complete this task and post here?
Reply


Forum Jump: