local config = require 'config' local httpd = require 'httpd' local hue = { } function hue.req(conf, t, cb) conf = conf or config.loaded.hue local conn = net.createConnection() conn:on('connection', function(conn) conn:on('receive', function(conn, pl) if cb then cb(pl:find('200') and true) end end) req = { string.format('PUT /api/%s/lights/%d/state HTTP/1.1', conf.user, conf.light), string.format('Host: %s', conf.addr), 'Connection: close', string.format('Content-Length: %d', #t + 2), '', t, '' } conn:send(table.concat(req, '\r\n')) end) conn:connect(80, conf.addr) end function hue.on(conf, bri, cb) hue.req(conf, string.format( '{"on":true,"bri":%d}', bri or 255), cb) end function hue.off(conf, cb) hue.req(conf, '{"on":false}', cb) end function hue.httpd_process(method, path, headers, content) if path == '/hue' then if method == 'GET' then local page = [[
]] return 200, 'OK', { ['Content-Type'] = 'text/html' }, page elseif method == 'POST' then local form = httpd.parse_form(content) local on = form['on'] and true or false if form['on'] then hue.on(nil, 128) else hue.off() end return 200, 'OK', {}, 'OK!\n' end end end return hue