wifischedule.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // JS version: Ramon van Gorkom
  15. 'use strict';
  16. 'require view';
  17. 'require form';
  18. 'require fs';
  19. 'require uci';
  20. function timeValidator(value, desc) {
  21. if (value !== null) {
  22. const matches = value.match(/^(\d\d?):(\d\d?)$/);
  23. if (matches) {
  24. const h = parseInt(matches[1], 10);
  25. const m = parseInt(matches[2], 10);
  26. if (h >= 0 && h <= 23 && m >= 0 && m <= 59) {
  27. return true;
  28. }
  29. }
  30. }
  31. return _('The value %s is invalid'.format(desc));
  32. }
  33. return view.extend({
  34. load: function() {
  35. return Promise.all([
  36. L.resolveDefault(fs.stat('/sbin/wifi'), null),
  37. L.resolveDefault(fs.stat('/usr/bin/wifi_schedule.sh'), null),
  38. L.resolveDefault(fs.exec_direct('/usr/bin/wifi_schedule.sh', [ 'getmodules' ]), null),
  39. L.resolveDefault(fs.stat('/usr/bin/iwinfo'), null)
  40. ]);
  41. },
  42. render: function (data) {
  43. var m, s, o, oUnloadmodules, oModules;
  44. m = new form.Map('wifi_schedule', _('Wifi Schedule'),_('Defines a schedule when to turn on and off wifi.'));
  45. s = m.section(form.TypedSection, 'global', _('Manual control'));
  46. s.optional = false;
  47. s.rmempty = false;
  48. s.anonymous = true;
  49. o = s.option(form.Button, '', _('Activate wifi'));
  50. o.onclick = function (section, value) {
  51. fs.exec('/usr/bin/wifi_schedule.sh', ['start', 'manual']);
  52. }
  53. o = s.option(form.Button, '', _('Disable wifi gracefully'));
  54. o.onclick = function (section, value) {
  55. fs.exec('/usr/bin/wifi_schedule.sh', ['stop', 'manual']);
  56. }
  57. o = s.option(form.Button, '', _('Disable wifi forced'));
  58. o.onclick = function (section, value) {
  59. fs.exec('/usr/bin/wifi_schedule.sh', ['forcestop', 'manual']);
  60. }
  61. s = m.section(form.TypedSection, 'global', _('Global Settings'));
  62. s.optional = false;
  63. s.rmempty = false;
  64. s.anonymous = true;
  65. o = s.option(form.Flag, 'enabled', _('Enable Wifi Schedule'));
  66. o.optional = false;
  67. o.rmempty = false;
  68. o.validate = function(section_id, value) {
  69. return value !== '1' || (data[0] && data[1]) ? true : _('Could not find required /usr/bin/wifi_schedule.sh or /sbin/wifi');
  70. };
  71. o = s.option(form.Flag, 'logging', _('Enable logging'));
  72. o.optional = false;
  73. o.rmempty = false;
  74. o.default = 0;
  75. o = s.option(form.Flag, 'unload_modules', _('Unload Modules (experimental; saves more power)'));
  76. o.optional = false;
  77. o.rmempty = false;
  78. o.default = 0;
  79. o = s.option(form.TextValue, 'modules', 'Modules to unload')
  80. o.depends('unload_modules', '1');
  81. o.wrap = 'off';
  82. o.rows = 10;
  83. o.cfgvalue = function (section) {
  84. let mod = uci.get('wifi_schedule', section, 'modules');
  85. if (!mod) mod = "";
  86. return mod.replace(/ /g, "\r\n");
  87. }
  88. o.write = function (section, value) {
  89. var valueList = value.replace(/[\r\n]+/g, " ").replace(/\s+$/, '');
  90. return uci.set('wifi_schedule', section, 'modules', valueList);
  91. }
  92. o = s.option(form.DummyValue, 'detectedmodules', _('Modules detected'));
  93. o.depends('unload_modules', '1');
  94. o.default = data[2];
  95. s = m.section(form.TypedSection, 'entry', _('Schedule events'));
  96. s.addremove = true;
  97. o = s.option(form.Flag, 'enabled', _('Enable mode'));
  98. o.rmempty = false;
  99. o.optional = false;
  100. o = s.option(form.MultiValue, 'daysofweek', _('Day(s) of Week'));
  101. o.rmempty = false;
  102. o.optional = false;
  103. o.modalonly = true;
  104. o.multiple = true;
  105. o.size = 7;
  106. o.value('Monday',_('Monday'));
  107. o.value('Tuesday',_('Tuesday'));
  108. o.value('Wednesday',_('Wednesday'));
  109. o.value('Thursday',_('Thursday'));
  110. o.value('Friday',_('Friday'));
  111. o.value('Saturday',_('Saturday'));
  112. o.value('Sunday',_('Sunday'));
  113. o.write = function(section_id, value) {
  114. return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
  115. };
  116. o = s.option (form.Value, 'starttime', _('Start WiFi'));
  117. o.rmempty = false;
  118. o.optional = false;
  119. for (let hour = 0; hour <= 23; hour++) {
  120. o.value(`${hour.toString().padStart(2, '0')}:00`);
  121. }
  122. o.validate = function(section_id, value) {
  123. return timeValidator(value, _('Start Time'))
  124. };
  125. o = s.option (form.Value, 'stoptime', _('Stop WiFi'));
  126. o.rmempty = false;
  127. o.optional = false;
  128. for (let hour = 0; hour <= 23; hour++) {
  129. o.value(`${hour.toString().padStart(2, '0')}:00`);
  130. }
  131. o.validate = function(section_id, value) {
  132. return timeValidator(value, _('Stop Time'))
  133. };
  134. o = s.option(form.Flag, 'forcewifidown', _('Force disabling wifi even if stations associated'));
  135. o.default = false;
  136. o.rmempty = false;
  137. o.validate = function(section_id, value) {
  138. return value !== '0' || data[3] ? true : _("Could not find required program /usr/bin/iwinfo");
  139. };
  140. return m.render()
  141. },
  142. handleSaveApply: function (ev, mode) {
  143. var Fn = L.bind(function() {
  144. fs.exec('/usr/bin/wifi_schedule.sh', ['cron']);
  145. document.removeEventListener('uci-applied',Fn);
  146. });
  147. document.addEventListener('uci-applied', Fn);
  148. this.super('handleSaveApply', [ev, mode]);
  149. },
  150. });