1 -- Copyright (c) 2016, prpl Foundation
3 -- Permission to use, copy, modify, and/or distribute this software for any purpose with or without
4 -- fee is hereby granted, provided that the above copyright notice and this permission notice appear
7 -- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
8 -- INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
9 -- FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
10 -- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
11 -- ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13 -- Author: Nils Koenig <openwrt@newk.it>
15 local fs = require "nixio.fs"
16 local sys = require "luci.sys"
17 local uci = require("luci.model.uci").cursor()
19 function time_validator(self, value, desc)
21 h_str, m_str = string.match(value, "^(%d%d?):(%d%d?)$")
33 return nil, translatef("The value %s is invalid", desc)
36 -- -------------------------------------------------------------------------------------------------
39 m = Map("wifi_schedule", translate("Wifi Schedule"), translate("Defines a schedule when to turn on and off wifi."))
40 function m.on_commit(self)
41 sys.exec("/usr/bin/wifi_schedule.sh cron")
45 -- BEGIN Global Section
46 global_section = m:section(TypedSection, "global", translate("Global Settings"))
47 global_section.optional = false
48 global_section.rmempty = false
49 global_section.anonymous = true
52 -- BEGIN Global Enable Checkbox
53 global_enable = global_section:option(Flag, "enabled", translate("Enable Wifi Schedule"))
54 global_enable.optional = false
55 global_enable.rmempty = false
57 function global_enable.validate(self, value, global_section)
59 if ( fs.access("/sbin/wifi") and
60 fs.access("/usr/bin/wifi_schedule.sh") )then
63 return nil, translate("Could not find required /usr/bin/wifi_schedule.sh or /sbin/wifi")
69 -- END Global Enable Checkbox
71 -- BEGIN Global Logging Checkbox
72 global_logging = global_section:option(Flag, "logging", translate("Enable logging"))
73 global_logging.optional = false
74 global_logging.rmempty = false
75 global_logging.default = 0
76 -- END Global Enable Checkbox
78 -- BEGIN Global Activate WiFi Button
79 enable_wifi = global_section:option(Button, "enable_wifi", translate("Activate wifi"))
80 function enable_wifi.write()
81 sys.exec("/usr/bin/wifi_schedule.sh start manual")
83 -- END Global Activate Wifi Button
85 -- BEGIN Global Disable WiFi Gracefully Button
86 disable_wifi_gracefully = global_section:option(Button, "disable_wifi_gracefully", translate("Disable wifi gracefully"))
87 function disable_wifi_gracefully.write()
88 sys.exec("/usr/bin/wifi_schedule.sh stop manual")
90 -- END Global Disable Wifi Gracefully Button
92 -- BEGIN Disable WiFi Forced Button
93 disable_wifi_forced = global_section:option(Button, "disable_wifi_forced", translate("Disabled wifi forced"))
94 function disable_wifi_forced.write()
95 sys.exec("/usr/bin/wifi_schedule.sh forcestop manual")
97 -- END Global Disable WiFi Forced Button
99 -- BEGIN Global Unload Modules Checkbox
100 global_unload_modules = global_section:option(Flag, "unload_modules", translate("Unload Modules (experimental; saves more power)"))
101 global_unload_modules.optional = false
102 global_unload_modules.rmempty = false
103 global_unload_modules.default = 0
104 -- END Global Unload Modules Checkbox
108 modules = global_section:option(TextValue, "modules", "")
109 modules:depends("unload_modules", global_unload_modules.enabled);
113 function modules.cfgvalue(self, section)
114 mod = uci:get("wifi_schedule", section, "modules")
118 return mod:gsub(" ", "\r\n")
121 function modules.write(self, section, value)
123 value_list = value:gsub("\r\n", " ")
124 ListValue.write(self, section, value_list)
125 uci:set("wifi_schedule", section, "modules", value_list)
130 -- BEGIN Determine Modules
131 determine_modules = global_section:option(Button, "determine_modules", translate("Determine Modules Automatically"))
132 determine_modules:depends("unload_modules", global_unload_modules.enabled);
133 function determine_modules.write(self, section)
134 output = sys.exec("/usr/bin/wifi_schedule.sh getmodules")
135 modules:write(section, output)
137 -- END Determine Modules
140 d = m:section(TypedSection, "entry", translate("Schedule events"))
145 -- BEGIN Enable Checkbox
146 c = d:option(Flag, "enabled", translate("Enable"))
149 -- END Enable Checkbox
151 -- BEGIN Day(s) of Week
152 dow = d:option(MultiValue, "daysofweek", translate("Day(s) of Week"))
155 dow:value("Monday", translate("Monday"))
156 dow:value("Tuesday", translate("Tuesday"))
157 dow:value("Wednesday", translate("Wednesday"))
158 dow:value("Thursday", translate("Thursday"))
159 dow:value("Friday", translate("Friday"))
160 dow:value("Saturday", translate("Saturday"))
161 dow:value("Sunday", translate("Sunday"))
162 -- END Day(s) of Weel
164 -- BEGIN Start Wifi Dropdown
165 starttime = d:option(Value, "starttime", translate("Start WiFi"))
166 starttime.optional = false
167 starttime.rmempty = false
168 starttime:value("00:00")
169 starttime:value("01:00")
170 starttime:value("02:00")
171 starttime:value("03:00")
172 starttime:value("04:00")
173 starttime:value("05:00")
174 starttime:value("06:00")
175 starttime:value("07:00")
176 starttime:value("08:00")
177 starttime:value("09:00")
178 starttime:value("10:00")
179 starttime:value("11:00")
180 starttime:value("12:00")
181 starttime:value("13:00")
182 starttime:value("14:00")
183 starttime:value("15:00")
184 starttime:value("16:00")
185 starttime:value("17:00")
186 starttime:value("18:00")
187 starttime:value("19:00")
188 starttime:value("20:00")
189 starttime:value("21:00")
190 starttime:value("22:00")
191 starttime:value("23:00")
193 function starttime.validate(self, value, d)
194 return time_validator(self, value, translate("Start Time"))
196 -- END Start Wifi Dropdown
198 -- BEGIN Stop Wifi Dropdown
199 stoptime = d:option(Value, "stoptime", translate("Stop WiFi"))
200 stoptime.optional = false
201 stoptime.rmempty = false
202 stoptime:value("00:00")
203 stoptime:value("01:00")
204 stoptime:value("02:00")
205 stoptime:value("03:00")
206 stoptime:value("04:00")
207 stoptime:value("05:00")
208 stoptime:value("06:00")
209 stoptime:value("07:00")
210 stoptime:value("08:00")
211 stoptime:value("09:00")
212 stoptime:value("10:00")
213 stoptime:value("11:00")
214 stoptime:value("12:00")
215 stoptime:value("13:00")
216 stoptime:value("14:00")
217 stoptime:value("15:00")
218 stoptime:value("16:00")
219 stoptime:value("17:00")
220 stoptime:value("18:00")
221 stoptime:value("19:00")
222 stoptime:value("20:00")
223 stoptime:value("21:00")
224 stoptime:value("22:00")
225 stoptime:value("23:00")
227 function stoptime.validate(self, value, d)
228 return time_validator(self, value, translate("Stop Time"))
230 -- END Stop Wifi Dropdown
232 -- BEGIN Force Wifi Stop Checkbox
233 force_wifi = d:option(Flag, "forcewifidown", translate("Force disabling wifi even if stations associated"))
234 force_wifi.default = false
235 force_wifi.rmempty = false
237 function force_wifi.validate(self, value, d)
239 if fs.access("/usr/bin/iwinfo") then
242 return nil, translate("Could not find required program /usr/bin/iwinfo")
248 -- END Force Wifi Checkbox