aptimer 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/lua
  2. local timestamp = os.time()
  3. local uci = require('luci.model.uci').cursor()
  4. function compare(list, value)
  5. if not list then
  6. return nil
  7. end
  8. for _, v in ipairs(list) do
  9. if v == value then
  10. return true
  11. end
  12. end
  13. return false
  14. end
  15. function getDay()
  16. local type = uci:get('aptimer','settings','type')
  17. if type == 'day' then return 'all'
  18. elseif type == 'week' then return os.date('%a', timestamp)
  19. elseif type == 'month' then return os.date('%d', timestamp)
  20. else return nil
  21. end
  22. end
  23. function apSet(value)
  24. local execWifi = false
  25. local radios = {'client_radio0', 'client_radio1'}
  26. for _, radio in ipairs(radios) do
  27. if uci:get('wireless', radio) then
  28. uci:set('wireless', radio, 'disabled', value and 0 or 1)
  29. execWifi = true
  30. end
  31. end
  32. if execWifi then
  33. uci:save('wireless')
  34. os.execute('wifi')
  35. end
  36. end
  37. if uci:get_bool('aptimer','settings','enabled') then
  38. local day = getDay()
  39. local current = os.date("%H:%M", timestamp)
  40. local off = compare(uci:get_list('aptimer', day, 'off'), current)
  41. local on = compare(uci:get_list('aptimer', day, 'on'), current)
  42. if on and not off then
  43. apSet(true)
  44. end
  45. if off and not on then
  46. apSet(false)
  47. end
  48. end