pastebin/hue.lua
Michael 22d331e644 hue lua app
Signed-off-by: Michael <michael.lindman@gmail.com>
2020-10-25 04:04:05 +00:00

60 lines
1.4 KiB
Lua
Executable File

#!/usr/bin/env lua
local http = require("socket.http")
local ltn12 = require"ltn12"
local lunajson = require 'lunajson'
local addr = "http://10.0.30.24/api/"
local apikey = ""
function Request(path, req)
local resp = {}
local body, statusCode, respheaders, statusText = http.request {
method = "PUT",
url = path,
source = ltn12.source.string(req),
headers = {
["content-type"] = "application/json",
["content-length"] = tostring(#req)
},
sink = ltn12.sink.table(resp)
}
resp = table.concat(resp)
print(resp)
end
function Get(path)
local resp = {}
local body, statusCode, respheaders, statusText = http.request {
method = "GET",
url = path,
headers = {
["content-type"] = "application/json",
},
sink = ltn12.sink.table(resp)
}
resp = table.concat(resp)
return lunajson.decode(resp)
end
local js = Get(addr..apikey.."/lights/4/")
print(js["state"])
function LightsOn(on)
Request(addr..apikey.."/lights/4/state", "{\"on\":"..on.."}")
end
function LightsBri(bri)
Request(addr..apikey.."/lights/4/state", "{\"on\": true, \"bri\":"..bri.."}")
end
function Round(x)
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
if arg[1] == "on" then
LightsOn(arg[2])
elseif arg[1] == "bri" then
LightsBri(Round(arg[2]/100*255))
end