07.12.2020, 19:50
(This post was last modified: 07.12.2020, 19:56 by gjniewenhuijse.)
Thanks admin for all the work. Here my first working version
Code:
--[[ documentation
api: https://xled-docs.readthedocs.io/en/latest/rest_api.html
mqtt: https://xled-docs.readthedocs.io/en/latest/msqtt_api.html
fibaro example: https://forum.fibaro.com/topic/52767-qa-twinkly-lights-on-off/
--]]
host = "xxx.xxx.x.xx" -- enter your Twinkly ip
-- debuggen
debug = false
function debugLog(iLog)
if debug then log(iLog) end
end
-- init
if not init then
require('socket.http')
require('ltn12')
require('json')
socket.http.TIMEOUT = 5
challenceToken = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8="
authToken = nil
init = true
end
local api = {
base = "/xled/v1/",
endpoints = {
login = "login",
verify = "verify",
mode = "led/mode",
deviceName = "device_name",
reset = "led/reset",
movieConfig = "led/movie/config",
movie = "led/movie/full",
gestalt = "gestalt",
brightness = "led/out/brightness"
}
}
function getTwinklyResponseData(endPoint, body, func, content_type)
debugLog(endPoint)
content_type = content_type or "application/json"
local url = "http://" .. host .. api.base .. api.endpoints[endPoint]
local method = "GET"
local cl
if body ~= nil then
method = "POST"
if type(body) == "table" and content_type == "application/json" then
body = json.encode(body)
end
cl = #body
end
debugLog({url, method, content_type, cl, authToken, body})
local response_body = {}
local res, code, response_headers, status = socket.http.request({
url = url,
method = method,
headers =
{
["Content-Type"] = content_type,
["Content-Length"] = cl,
["X-Auth-Token"] = authToken
},
source = ltn12.source.string(body),
sink = ltn12.sink.table(response_body)
})
debugLog({res, code, response_headers, status, response_body})
if res then
if code == 200 then
func(json.decode(table.concat(response_body)))
else
func(nil, response_body)
end
else
func(nil, {error = code})
end
end
function connect(func_succ, func_err)
getTwinklyResponseData("gestalt", nil,
function(gestalt, err)
if err == nil then
getTwinklyResponseData(
"login",
{challenge = challenceToken},
function(login, err)
if err == nil then
authToken = login.authentication_token
getTwinklyResponseData(
"verify",
{},
function(verify, err)
if err == nil then
func_succ(gestalt)
else
if func_err then
func_err(err)
else
debugLog(json.encode(err))
end
end
end
)
else
if func_err then
func_err(err)
else
debugLog(json.encode(err))
end
end
end
)
else
if func_err then
func_err(err)
else
debugLog(json.encode(err))
end
end
end
)
end
function setMode(mode, func)
if authToken == nil then
debugLog("Device is not intialized!")
end
getTwinklyResponseData(
"mode",
{mode = mode},
function(response, err)
if err == nil then
if func then
func(response)
end
else
debugLog(json.encode(err))
end
end
)
end
function getMode(func)
if authToken == nil then
debugLog("Device is not intialized!")
end
getTwinklyResponseData(
"mode",
nil,
function(response, err)
if err == nil then
if func then
func(response)
end
else
debugLog(json.encode(err))
end
end
)
end
function setBrightness(value, func)
if authToken == nil then
debugLog("Device is not intialized!")
end
getTwinklyResponseData(
"brightness",
{mode = "enabled", type = "A", value = value},
function(response, err)
if err == nil then
if func then
func(response)
end
else
debugLog(json.encode(err))
end
end
)
end
function getBrightness(func)
if authToken == nil then
debugLog("Device is not intialized!")
end
getTwinklyResponseData(
"brightness",
nil,
function(response, err)
if err == nil then
if func then
func(response)
end
else
debugLog(json.encode(err))
end
end
)
end
------------------ some function tests ------------------
-- set mode
connect(
function(data)
setMode(
"off", -- off, movie
function(data)
debugLog(json.encode(data))
end
)
end
)
-- get current mode
connect(
function(data)
getMode(
function(data)
debugLog(json.encode(data))
end
)
end
)
-- set brightness
connect(
function(data)
setBrightness(
10,
function(data)
debugLog(json.encode(data))
end
)
end
)
-- get current brightness
connect(
function(data)
getBrightness(
function(data)
debugLog(json.encode(data))
end
)
end
)