Skip to content

Commit 496eaf6

Browse files
committed
luci-app-timewol: Script improvement and multilingualization
Rewrite some scripts to make it work better and change to multilingual
1 parent f20b66a commit 496eaf6

File tree

5 files changed

+202
-87
lines changed

5 files changed

+202
-87
lines changed

applications/luci-app-timewol/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (C) 2016 Openwrt.org
2+
# Copyright (C) 2024 iv7777 <hongba@rocketmail.com>
23
#
34
# This is free software, licensed under the Apache License, Version 2.0 .
45
#
@@ -7,8 +8,8 @@ include $(TOPDIR)/rules.mk
78

89
LUCI_TITLE:=LuCI support for Timewol
910
LUCI_PKGARCH:=all
10-
PKG_VERSION:=1.0
11-
PKG_RELEASE:=3-20190309
11+
PKG_VERSION:=1.1
12+
PKG_RELEASE:=1-20240520
1213

1314
include ../../luci.mk
1415

applications/luci-app-timewol/luasrc/controller/timewol.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function index()
44
if not nixio.fs.access("/etc/config/timewol") then return end
55

66
entry({"admin", "control"}, firstchild(), "Control", 44).dependent = false
7-
local page = entry({"admin", "control", "timewol"}, cbi("timewol"), _("定时唤醒"))
7+
local page = entry({"admin", "control", "timewol"}, cbi("timewol"), _("Timed WOL"))
88
page.order = 95
99
page.dependent = true
1010
page.acl_depends = { "luci-app-timewol" }
Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,86 @@
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+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
11
msgid "Control"
22
msgstr "管控"
3+
4+
msgid "Timed WOL"
5+
msgstr "定时网络唤醒"
6+
7+
msgid "Timed Wake on LAN"
8+
msgstr "定时网络唤醒"
9+
10+
msgid "Wake up your local area network devices on schedule"
11+
msgstr "定时唤醒你的局域网设备"
12+
13+
msgid "Timed WOL"
14+
msgstr "定时网络唤醒"
15+
16+
msgid "Running Status"
17+
msgstr "运行状态"
18+
19+
msgid "Current Status"
20+
msgstr "当前状态"
21+
22+
msgid "Basic Settings"
23+
msgstr "基本设置"
24+
25+
msgid "Enable"
26+
msgstr "开启"
27+
28+
msgid "Client Settings"
29+
msgstr "客户端设置"
30+
31+
msgid "Client MAC"
32+
msgstr "客户端MAC"
33+
34+
msgid "Network Interface"
35+
msgstr "网络接口"
36+
37+
msgid "Minute"
38+
msgstr "分钟"
39+
40+
msgid "Hour"
41+
msgstr "小时"
42+
43+
msgid "Day"
44+
msgstr "日"
45+
46+
msgid "Month"
47+
msgstr "月"
48+
49+
msgid "Week"
50+
msgstr "星期"
51+
52+
msgid "Invalid value for %s: %s. Must be between %d and %d or '*'"
53+
msgstr "%s: %s 的值无效. 必须在 %d 和 %d 之间,或为 '*'"
54+

applications/luci-app-timewol/root/etc/init.d/timewol

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,86 @@
22
#
33
# Copyright (C) 2015 OpenWrt-dist
44
# Copyright (C) 2016 fw867 <ffkykzs@gmail.com>
5+
# Copyright (C) 2024 iv7777 <hongba@rocketmail.com>
56
#
67
# This is free software, licensed under the GNU General Public License v3.
78
# See /LICENSE for more information.
89
#
910

1011
START=99
11-
1212
CONFIG=timewol
13+
CRONTAB_FILE="/etc/crontabs/root"
14+
ETHERWAKE_CMD="/usr/bin/etherwake"
1315

16+
# Function to get UCI configuration values with defaults
1417
uci_get_by_type() {
15-
local index=0
16-
if [ -n $4 ]; then
17-
index=$4
18-
fi
19-
local ret=$(uci get $CONFIG.@$1[$index].$2 2>/dev/null)
20-
echo ${ret:=$3}
18+
local type=$1
19+
local option=$2
20+
local default=$3
21+
local index=${4:-0} # Use 0 if $4 is not provided
22+
23+
local value
24+
value=$(uci get "$CONFIG.@$type[$index].$option" 2>/dev/null) || value=$default
25+
echo "$value"
2126
}
2227

28+
# Function to check if a value represents a true boolean
2329
is_true() {
24-
case $1 in
25-
1|on|true|yes|enabled) echo 0;;
26-
*) echo 1;;
27-
esac
30+
case "$1" in
31+
1|on|true|yes|enabled) return 0 ;;
32+
*) return 1 ;;
33+
esac
2834
}
2935

36+
# Function to load configuration and check if enabled
3037
load_config() {
31-
ENABLED=$(uci_get_by_type basic enable)
32-
return $(is_true $ENABLED)
38+
local enabled
39+
enabled=$(uci_get_by_type basic enable "0")
40+
is_true "$enabled"
3341
}
3442

35-
add_rule(){
36-
sed -i '/etherwake/d' /etc/crontabs/root >/dev/null 2>&1
37-
for i in $(seq 0 100)
38-
do
39-
local macaddr=$(uci_get_by_type macclient macaddr '' $i)
40-
local maceth=$(uci_get_by_type macclient maceth '' $i)
41-
local minute=$(uci_get_by_type macclient minute '' $i)
42-
local hour=$(uci_get_by_type macclient hour '' $i)
43-
local day=$(uci_get_by_type macclient day '' $i)
44-
local month=$(uci_get_by_type macclient month '' $i)
45-
local weeks=$(uci_get_by_type macclient weeks '' $i)
46-
if [ -z $macaddr ] || [ -z $maceth ]; then
47-
break
48-
fi
49-
if [ -z $minute ] ; then
50-
minute="0"
51-
fi
52-
if [ -z $hour ] ; then
53-
hour="*"
54-
fi
55-
if [ -z $day ] ; then
56-
day="*"
57-
fi
58-
if [ -z $month ] ; then
59-
month="*"
60-
fi
61-
if [ -z $weeks ] ; then
62-
weeks="*"
63-
fi
64-
echo "$minute $hour $day $month $weeks /usr/bin/etherwake -D -i $maceth $macaddr" >> /etc/crontabs/root
65-
done
43+
# Function to add WoL rules to the crontab
44+
add_rule() {
45+
# Remove existing etherwake entries
46+
sed -i '/etherwake/d' "$CRONTAB_FILE"
47+
48+
for i in $(seq 0 100); do
49+
local macaddr
50+
local maceth
51+
local minute
52+
local hour
53+
local day
54+
local month
55+
local weeks
56+
57+
macaddr=$(uci_get_by_type macclient macaddr "" "$i")
58+
maceth=$(uci_get_by_type macclient maceth "" "$i")
59+
60+
# Stop if no more macaddr entries
61+
[ -z "$macaddr" ] && break
62+
[ -z "$maceth" ] && break
63+
64+
minute=$(uci_get_by_type macclient minute "0" "$i")
65+
hour=$(uci_get_by_type macclient hour "*" "$i")
66+
day=$(uci_get_by_type macclient day "*" "$i")
67+
month=$(uci_get_by_type macclient month "*" "$i")
68+
weeks=$(uci_get_by_type macclient weeks "*" "$i")
69+
70+
echo "$minute $hour $day $month $weeks $ETHERWAKE_CMD -D -i $maceth $macaddr" >> "$CRONTAB_FILE"
71+
done
6672
}
6773

74+
# Function to start the service
6875
start() {
69-
! load_config && exit 0
70-
add_rule
76+
if load_config; then
77+
add_rule
78+
else
79+
exit 0
80+
fi
7181
}
82+
83+
# Function to stop the service
7284
stop() {
73-
sed -i '/etherwake/d' /etc/crontabs/root >/dev/null 2>&1
85+
# Remove etherwake entries from crontab
86+
sed -i '/etherwake/d' "$CRONTAB_FILE"
7487
}

0 commit comments

Comments
 (0)