164451416639cd10b48a8fe86e3bcb12bb8cbc7c
[project/luci.git] / applications / luci-app-wifischedule / luasrc / model / cbi / wifischedule / wifi_schedule.lua
1 -- Copyright (c) 2016, prpl Foundation
2 --
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
5 -- in all copies.
6 --
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.
12 --
13 -- Author: Nils Koenig <openwrt@newk.it>
14
15 local fs = require "nixio.fs"
16 local sys = require "luci.sys"
17 local uci = require("luci.model.uci").cursor()
18
19 function time_validator(self, value, desc)
20 if value ~= nil then
21 h_str, m_str = string.match(value, "^(%d%d?):(%d%d?)$")
22 h = tonumber(h_str)
23 m = tonumber(m_str)
24 if ( h ~= nil and
25 h >= 0 and
26 h <= 23 and
27 m ~= nil and
28 m >= 0 and
29 m <= 59) then
30 return value
31 end
32 end
33 return nil, translatef("The value %s is invalid", desc)
34 end
35
36 -- -------------------------------------------------------------------------------------------------
37
38 -- BEGIN Map
39 m = Map("wifi_schedule", translate("Wifi Schedule"), translate("Defines a schedule when to turn on and off wifi."))
40 m.apply_on_parse = true
41
42 function m.on_apply(self)
43 sys.exec("/usr/bin/wifi_schedule.sh cron")
44 end
45 -- END Map
46
47 -- BEGIN Global Section
48 global_section = m:section(TypedSection, "global", translate("Global Settings"))
49 global_section.optional = false
50 global_section.rmempty = false
51 global_section.anonymous = true
52 -- END Section
53
54 -- BEGIN Global Enable Checkbox
55 global_enable = global_section:option(Flag, "enabled", translate("Enable Wifi Schedule"))
56 global_enable.optional = false
57 global_enable.rmempty = false
58
59 function global_enable.validate(self, value, global_section)
60 if value == "1" then
61 if ( fs.access("/sbin/wifi") and
62 fs.access("/usr/bin/wifi_schedule.sh") )then
63 return value
64 else
65 return nil, translate("Could not find required /usr/bin/wifi_schedule.sh or /sbin/wifi")
66 end
67 else
68 return "0"
69 end
70 end
71 -- END Global Enable Checkbox
72
73 -- BEGIN Global Logging Checkbox
74 global_logging = global_section:option(Flag, "logging", translate("Enable logging"))
75 global_logging.optional = false
76 global_logging.rmempty = false
77 global_logging.default = 0
78 -- END Global Enable Checkbox
79
80 -- BEGIN Global Activate WiFi Button
81 enable_wifi = global_section:option(Button, "enable_wifi", translate("Activate wifi"))
82 function enable_wifi.write()
83 sys.exec("/usr/bin/wifi_schedule.sh start manual")
84 end
85 -- END Global Activate Wifi Button
86
87 -- BEGIN Global Disable WiFi Gracefully Button
88 disable_wifi_gracefully = global_section:option(Button, "disable_wifi_gracefully", translate("Disable wifi gracefully"))
89 function disable_wifi_gracefully.write()
90 sys.exec("/usr/bin/wifi_schedule.sh stop manual")
91 end
92 -- END Global Disable Wifi Gracefully Button
93
94 -- BEGIN Disable WiFi Forced Button
95 disable_wifi_forced = global_section:option(Button, "disable_wifi_forced", translate("Disabled wifi forced"))
96 function disable_wifi_forced.write()
97 sys.exec("/usr/bin/wifi_schedule.sh forcestop manual")
98 end
99 -- END Global Disable WiFi Forced Button
100
101 -- BEGIN Global Unload Modules Checkbox
102 global_unload_modules = global_section:option(Flag, "unload_modules", translate("Unload Modules (experimental; saves more power)"))
103 global_unload_modules.optional = false
104 global_unload_modules.rmempty = false
105 global_unload_modules.default = 0
106 -- END Global Unload Modules Checkbox
107
108
109 -- BEGIN Modules
110 modules = global_section:option(TextValue, "modules", "")
111 modules:depends("unload_modules", global_unload_modules.enabled);
112 modules.wrap = "off"
113 modules.rows = 10
114
115 function modules.cfgvalue(self, section)
116 mod = uci:get("wifi_schedule", section, "modules")
117 if mod == nil then
118 mod = ""
119 end
120 return mod:gsub(" ", "\r\n")
121 end
122
123 function modules.write(self, section, value)
124 if value then
125 value_list = value:gsub("\r\n", " ")
126 ListValue.write(self, section, value_list)
127 uci:set("wifi_schedule", section, "modules", value_list)
128 end
129 end
130 -- END Modules
131
132 -- BEGIN Determine Modules
133 determine_modules = global_section:option(Button, "determine_modules", translate("Determine Modules Automatically"))
134 determine_modules:depends("unload_modules", global_unload_modules.enabled);
135 function determine_modules.write(self, section)
136 output = sys.exec("/usr/bin/wifi_schedule.sh getmodules")
137 modules:write(section, output)
138 end
139 -- END Determine Modules
140
141 -- BEGIN Section
142 d = m:section(TypedSection, "entry", translate("Schedule events"))
143 d.addremove = true
144 --d.anonymous = true
145 -- END Section
146
147 -- BEGIN Enable Checkbox
148 c = d:option(Flag, "enabled", translate("Enable"))
149 c.optional = false
150 c.rmempty = false
151 -- END Enable Checkbox
152
153 -- BEGIN Day(s) of Week
154 dow = d:option(MultiValue, "daysofweek", translate("Day(s) of Week"))
155 dow.optional = false
156 dow.rmempty = false
157 dow:value("Monday", translate("Monday"))
158 dow:value("Tuesday", translate("Tuesday"))
159 dow:value("Wednesday", translate("Wednesday"))
160 dow:value("Thursday", translate("Thursday"))
161 dow:value("Friday", translate("Friday"))
162 dow:value("Saturday", translate("Saturday"))
163 dow:value("Sunday", translate("Sunday"))
164 -- END Day(s) of Weel
165
166 -- BEGIN Start Wifi Dropdown
167 starttime = d:option(Value, "starttime", translate("Start WiFi"))
168 starttime.optional = false
169 starttime.rmempty = false
170 starttime:value("00:00")
171 starttime:value("01:00")
172 starttime:value("02:00")
173 starttime:value("03:00")
174 starttime:value("04:00")
175 starttime:value("05:00")
176 starttime:value("06:00")
177 starttime:value("07:00")
178 starttime:value("08:00")
179 starttime:value("09:00")
180 starttime:value("10:00")
181 starttime:value("11:00")
182 starttime:value("12:00")
183 starttime:value("13:00")
184 starttime:value("14:00")
185 starttime:value("15:00")
186 starttime:value("16:00")
187 starttime:value("17:00")
188 starttime:value("18:00")
189 starttime:value("19:00")
190 starttime:value("20:00")
191 starttime:value("21:00")
192 starttime:value("22:00")
193 starttime:value("23:00")
194
195 function starttime.validate(self, value, d)
196 return time_validator(self, value, translate("Start Time"))
197 end
198 -- END Start Wifi Dropdown
199
200 -- BEGIN Stop Wifi Dropdown
201 stoptime = d:option(Value, "stoptime", translate("Stop WiFi"))
202 stoptime.optional = false
203 stoptime.rmempty = false
204 stoptime:value("00:00")
205 stoptime:value("01:00")
206 stoptime:value("02:00")
207 stoptime:value("03:00")
208 stoptime:value("04:00")
209 stoptime:value("05:00")
210 stoptime:value("06:00")
211 stoptime:value("07:00")
212 stoptime:value("08:00")
213 stoptime:value("09:00")
214 stoptime:value("10:00")
215 stoptime:value("11:00")
216 stoptime:value("12:00")
217 stoptime:value("13:00")
218 stoptime:value("14:00")
219 stoptime:value("15:00")
220 stoptime:value("16:00")
221 stoptime:value("17:00")
222 stoptime:value("18:00")
223 stoptime:value("19:00")
224 stoptime:value("20:00")
225 stoptime:value("21:00")
226 stoptime:value("22:00")
227 stoptime:value("23:00")
228
229 function stoptime.validate(self, value, d)
230 return time_validator(self, value, translate("Stop Time"))
231 end
232 -- END Stop Wifi Dropdown
233
234 -- BEGIN Force Wifi Stop Checkbox
235 force_wifi = d:option(Flag, "forcewifidown", translate("Force disabling wifi even if stations associated"))
236 force_wifi.default = false
237 force_wifi.rmempty = false
238
239 function force_wifi.validate(self, value, d)
240 if value == "0" then
241 if fs.access("/usr/bin/iwinfo") then
242 return value
243 else
244 return nil, translate("Could not find required program /usr/bin/iwinfo")
245 end
246 else
247 return "1"
248 end
249 end
250 -- END Force Wifi Checkbox
251
252 return m