1
0

wifi_schedule.lua 7.7 KB

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