local httpd = require 'httpd' local hue = require 'hue' local config = require 'config' local alarm = { } local days = { 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' } function alarm.httpd_process(method, path, headers, content) local alarms = config.loaded.alarms if path == '/alarm' then if method == 'GET' then local page = [[
%s
]] local row = [[ %s ]] local rows = { } for di, d in ipairs(days) do local checked = alarms[di] and alarms[di].active and ' checked' or '' local h = alarms[di] and alarms[di].hour or 0 local m = alarms[di] and alarms[di].min or 0 rows[#rows + 1] = string.format(row, d, di, checked, di, h, m) end page = string.format(page, table.concat(rows)) return 200, 'OK', { ['Content-Type'] = 'text/html' }, page elseif method == 'POST' then local form = httpd.parse_form(content) for di, d in ipairs(days) do active = form['en'..di] and true or false h, m = form['time'..di]:match('^(%d+):(%d+)$') h = h and tonumber(h) m = m and tonumber(m) if h and m then alarms[di] = { active = active, hour = h, min = m } else return 200, 'OK', {}, 'Bad parameters\n' end end config.save() return 200, 'OK', {}, 'OK!\n' end end end function alarm.test(tm) local di = tm.wday local a = config.loaded.alarms[di] if a and a.active and a.hour == tm.hour and a.min == tm.min then alarm.ring() end end function alarm.ring() hue.on(nil, 64) end return alarm