aboutsummaryrefslogtreecommitdiff
path: root/alarm.lua
diff options
context:
space:
mode:
Diffstat (limited to 'alarm.lua')
-rw-r--r--alarm.lua74
1 files changed, 74 insertions, 0 deletions
diff --git a/alarm.lua b/alarm.lua
new file mode 100644
index 0000000..afbd406
--- /dev/null
+++ b/alarm.lua
@@ -0,0 +1,74 @@
+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 = [[
+<html>
+<body>
+<form method="post">
+<table>
+%s</table>
+<input type="submit">
+</form>
+</body>
+</html>
+]]
+ local row = [[
+ <tr><td>%s</td>
+ <td><input type="checkbox" name="en%d"%s></td>
+ <td><input type="text" name="time%d" value="%02d:%02d"></td>
+ </tr>
+]]
+ 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