From 22d331e644f47789cccf2ffa10c7598c53a02cf6 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 25 Oct 2020 04:04:05 +0000 Subject: [PATCH] hue lua app Signed-off-by: Michael --- hue.lua | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 hue.lua diff --git a/hue.lua b/hue.lua new file mode 100755 index 0000000..7c7ed68 --- /dev/null +++ b/hue.lua @@ -0,0 +1,59 @@ +#!/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