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