LUA script to access TOON all data on demand

For heating/cooling related questions in Domoticz

Moderator: leecollings

Post Reply
MatthijsPH
Posts: 15
Joined: Friday 01 May 2015 19:27
Target OS: Linux
Domoticz version: 3.5305
Location: The Netherlands
Contact:

LUA script to access TOON all data on demand

Post by MatthijsPH »

I am trying to build a lua-script that can access ENECO TOON and retrieve data on demand. This way for instance the actual TOON state can be retrieved from Toon. Offcourse more is possible. Having access to TOON can be handy if you want to update your selector switch without having to guess the current state by the current setpoint or get some other data that is not available in Domoticz.
There is a already a python script wich I forked and documented before.
However I would like to be able to use this functionality from within my lua-scripts and on demand.
I've come a long way in rewriting the python script to LUA and I am now able to receive "handshake" details, log-on to Toon and log-out again. The problem is the middle part in wich TOON State should be received. I'm pretty sure I'm doing something wrong (probably the cURL/get part), but I can't figure out what it is by myself. Hopefully somebody can help me.
The current "test" version of the script can also be found here

Code: Select all

-- LUA modules
json = require('dkjson')

local user = 'xxxxxxxxxx'
local pass = 'xxxxxxxxxx'

local debug = 1

-- UUID1
--random = math.random
--function uuid1()
--   local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
--   return string.gsub(template, '[xy]', function (c)
--      local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
--      return string.format('%x', v)
--   end)
--end

-- UUID4
function uuid()
   tmp = assert(io.popen('curl GET https://www.uuidgenerator.net/api/version4'))
   uuid4 = tostring(tmp:read('*all'))
   tmp:close()
   if debug == 1 then 
      print('uuid: '..uuid4)
   end
   return uuid4
end

-- LOGIN
function login()
   -- step 1: get agreement-details from Toon api
   local d1 = '\"username=' .. user .. '&password=' .. pass .. '\"'
   login = assert(io.popen('curl -X POST -d ' .. d1 .. ' https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/login'))
      local token = login:read('*all')
      login:close()
      jsonLogin, pos, err = json.decode(token, 1, nil)
      if debug == 1 then
         if err then print('err: '..err)
         else print('contents: '..token)
         end
      end
   clientId = tostring(jsonLogin.clientId)
   clientIdChecksum = tostring(jsonLogin.clientIdChecksum)
   agreementId = tostring(jsonLogin.agreements[1].agreementId)
   agreementIdChecksum = tostring(jsonLogin.agreements[1].agreementIdChecksum)
   if debug == 1 and jsonLogin.success == true then print('Eneco Toon - sleutel opgehaald') end
   -- step 2: log on to Toon api
   local tmp1 = uuid()
   if debug == 1 then print('login - uuid: '..tostring(uuid4)) end
   local d2 = '\"clientId='..clientId..'&clientIdChecksum='..clientIdChecksum..'&agreementId='..agreementId..'&agreementIdChecksum='..agreementIdChecksum..'&random='..uuid4..'\"'
   if debug == 1 then print('body login: '..d2) end
   auth = assert(io.popen('curl -X POST -d ' .. d2 .. ' https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/start'))
      local check = auth:read('*all')
      auth:close()
      jsonAuth, pos, err = json.decode(check, 1, nil)
      if debug == 1 then
         if err then print('err: '..err)
         else print('auth: '..check)
         end
      end
   --uuId = tostring(jsonAuth.displayUuidKpi.uuid)
   --if debug == 1 then print('uuId: '..uuId) end
   if debug == 1 and jsonAuth.success == true then print('Eneco Toon - ingelogd') end
   return clientId, clientIdChecksum
end

-- LOGOUT
function logout()
   local tmp2 = uuid()
   if debug == 1 then print('logout - uuid: '..uuid4) end
   local d3 = '\"random='..uuid4..'&clientId='..clientId..'&clientIdChecksum='..clientIdChecksum..'\"' 
   if debug == 1 then print('body logout: '..d3) end
   logout = assert(io.popen('curl -X POST -d ' .. d3 .. ' https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/logout'))
      local check = logout:read('*all')
      logout:close()
      jsonLogout, pos, err = json.decode(check, 1, nil)
      if debug == 1 then
         if err then print('err: '..err)
         else print('logout: '..check)
         end
         if jsonLogout.success == true then print('Eneco Toon - uitgelogd') end
      end
end

-- GETSTATE
function getState()
   if debug == 1 then print('start getState') end
   i = 1 
   repeat
      local tmp3 = uuid()
      --if debug == 1 then print('getstate - uuid: '..uuid4) end
      local d4 = '\"random='..uuid4..'&clientId='..clientId..'&clientIdChecksum='..clientIdChecksum..'\"'  
      if debug == 1 then print('body getState: '..d4) end
      getState = assert(io.popen('curl -X GET -d '..d4..' https://toonopafstand.eneco.nl/toonMobileBackendWeb/client/auth/retrieveToonState'))
      local state = getState:read('*all')
      print('getstate: '..state)
         getState:close()
         jsonState, pos, err = json.decode(state, 1, nil)
	 if jsonState.success == true then 
	    print("SUCCESS!")
	    stop = true
	 else 
	    print("FAIL!")
	    i = i + 1
	 end
   until (stop or i == 4)
end

commandArray = {}
if devicechanged['testSwitch'] then
   local test1 = login()
   local test2 = getState()
   local test3 = logout()
   --local test = uuid()
end
return commandArray
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest