|
1 | | -local i = require "luci.sys" |
2 | | -local t, e, o |
3 | | -t = Map("timewol", translate("定时网络唤醒"), |
4 | | - translate("定时唤醒你的局域网设备")) |
5 | | -t.template = "timewol/index" |
6 | | -e = t:section(TypedSection, "basic", translate("Running Status")) |
7 | | -e.anonymous = true |
8 | | -o = e:option(DummyValue, "timewol_status", translate("当前状态")) |
9 | | -o.template = "timewol/timewol" |
10 | | -o.value = translate("Collecting data...") |
11 | | -e = t:section(TypedSection, "basic", translate("基本设置")) |
12 | | -e.anonymous = true |
13 | | -o = e:option(Flag, "enable", translate("开启")) |
14 | | -o.rmempty = false |
15 | | -e = t:section(TypedSection, "macclient", translate("客户端设置")) |
16 | | -e.template = "cbi/tblsection" |
17 | | -e.anonymous = true |
18 | | -e.addremove = true |
19 | | -nolimit_mac = e:option(Value, "macaddr", translate("客户端MAC")) |
20 | | -nolimit_mac.rmempty = false |
21 | | -i.net.mac_hints(function(e, t) nolimit_mac:value(e, "%s (%s)" % {e, t}) end) |
22 | | -nolimit_eth = e:option(Value, "maceth", translate("网络接口")) |
23 | | -nolimit_eth.rmempty = false |
24 | | -for t, e in ipairs(i.net.devices()) do if e ~= "lo" then nolimit_eth:value(e) end end |
25 | | -a = e:option(Value, "minute", translate("分钟")) |
26 | | -a.optional = false |
27 | | -a = e:option(Value, "hour", translate("小时")) |
28 | | -a.optional = false |
29 | | -a = e:option(Value, "day", translate("日")) |
30 | | -a.optional = false |
31 | | -a = e:option(Value, "month", translate("月")) |
32 | | -a.optional = false |
33 | | -a = e:option(Value, "weeks", translate("星期")) |
34 | | -a.optional = false |
35 | | -local e = luci.http.formvalue("cbi.apply") |
36 | | -if e then io.popen("/etc/init.d/timewol restart") end |
37 | | -return t |
| 1 | +local sys = require "luci.sys" |
| 2 | + |
| 3 | +-- Create the main map object |
| 4 | +local map = Map("timewol", translate("Timed Wake on LAN"), |
| 5 | + translate("Wake up your local area network devices on schedule")) |
| 6 | +map.template = "timewol/index" |
| 7 | + |
| 8 | +-- Running Status Section |
| 9 | +local status_section = map:section(TypedSection, "basic", translate("Running Status")) |
| 10 | +status_section.anonymous = true |
| 11 | + |
| 12 | +local status = status_section:option(DummyValue, "timewol_status", translate("Current Status")) |
| 13 | +status.template = "timewol/timewol" |
| 14 | +status.value = translate("Collecting data...") |
| 15 | + |
| 16 | +-- Basic Settings Section |
| 17 | +local basic_section = map:section(TypedSection, "basic", translate("Basic Settings")) |
| 18 | +basic_section.anonymous = true |
| 19 | + |
| 20 | +local enable = basic_section:option(Flag, "enable", translate("Enable")) |
| 21 | +enable.rmempty = false |
| 22 | + |
| 23 | +-- Client Settings Section |
| 24 | +local client_section = map:section(TypedSection, "macclient", translate("Client Settings")) |
| 25 | +client_section.template = "cbi/tblsection" |
| 26 | +client_section.anonymous = true |
| 27 | +client_section.addremove = true |
| 28 | + |
| 29 | +-- Client MAC Address |
| 30 | +local mac_addr = client_section:option(Value, "macaddr", translate("Client MAC")) |
| 31 | +mac_addr.rmempty = false |
| 32 | +sys.net.mac_hints(function(mac, hint) |
| 33 | + mac_addr:value(mac, string.format("%s (%s)", mac, hint)) |
| 34 | +end) |
| 35 | + |
| 36 | +-- Network Interface |
| 37 | +local net_iface = client_section:option(Value, "maceth", translate("Network Interface")) |
| 38 | +net_iface.rmempty = false |
| 39 | +net_iface.default = "br-lan" |
| 40 | +for _, device in ipairs(sys.net.devices()) do |
| 41 | + if device ~= "lo" then |
| 42 | + net_iface:value(device) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +-- Function to validate cron field values |
| 47 | +local function validate_cron_field(option_name, value, min, max, default) |
| 48 | + if value == "" then |
| 49 | + return default |
| 50 | + elseif value == "*" then |
| 51 | + return value |
| 52 | + end |
| 53 | + local num = tonumber(value) |
| 54 | + if num and num >= min and num <= max then |
| 55 | + return value |
| 56 | + else |
| 57 | + return nil, translatef("Invalid value for %s: %s. Must be between %d and %d or '*'", option_name, value, min, max) |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +-- Scheduling Options with Default Values and Range Checks |
| 62 | +local schedule_options = { |
| 63 | + { "minute", translate("Minute"), 0, 59, "0" }, |
| 64 | + { "hour", translate("Hour"), 0, 23, "0" }, |
| 65 | + { "day", translate("Day"), 1, 31, "*" }, |
| 66 | + { "month", translate("Month"), 1, 12, "*" }, |
| 67 | + { "weeks", translate("Week"), 0, 6, "*" } -- 0 for Sunday, 6 for Saturday |
| 68 | +} |
| 69 | + |
| 70 | +for _, opt in ipairs(schedule_options) do |
| 71 | + local field = client_section:option(Value, opt[1], opt[2]) |
| 72 | + field.default = opt[5] or opt[4] -- Use default value if present, otherwise use maximum value |
| 73 | + field.optional = false |
| 74 | + field.validate = function(self, value) |
| 75 | + return validate_cron_field(opt[2], value, opt[3], opt[4], field.default) |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +-- Apply the configuration changes |
| 80 | +map.apply_on_parse = true |
| 81 | +function map.on_apply(self) |
| 82 | + sys.exec("/etc/init.d/timewol restart") |
| 83 | +end |
| 84 | + |
| 85 | +return map |
| 86 | + |
0 commit comments