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