Skip to content

Commit

Permalink
luci-app-wifischedule: Convert to JS
Browse files Browse the repository at this point in the history
luci-app-wifischedule: Convert to JS

Signed-off-by: Ramon Van Gorkom Ramon00c00@gmail.com
  • Loading branch information
Ramon00 committed Oct 31, 2024
1 parent fb8ce8f commit ac36148
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
// Copyright (c) 2016, prpl Foundation
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without
// fee is hereby granted, provided that the above copyright notice and this permission notice appear
// in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
// FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Author: Nils Koenig <openwrt@newk.it>
// JS version: Ramon van Gorkom


'use strict';
'require view';
'require form';
'require fs';
'require uci';



var determineModulesButton = form.DummyValue.extend({
load: function() {
var Button = E('button', {
'class': 'cbi-button cbi-button-neutral',
}, _('Determine Modules Automatically')
);
Button.addEventListener('click', function() {
let output = fs.exec('/usr/bin/wifi_schedule.sh', ['getmodules']);
uci.set('wifi_schedule', 'global', 'modules', 'abc');
});
this.default= E([Button]);
}
});


function timeValidator(value, desc) {
if (value !== null) {
const matches = value.match(/^(\d\d?):(\d\d?)$/);
if (matches) {
const h = parseInt(matches[1], 10);
const m = parseInt(matches[2], 10);
if (h >= 0 && h <= 23 && m >= 0 && m <= 59) {
return true;
}
}
}
return _('The value %s is invalid'.format(desc));
}

function checkFile(path) {
try {
if (fs.stat(path)) {
return true;
}
} catch (err) {
return false;
}
}

return view.extend({

render: function () {
var m, s, o, oUnloadmodules, oModules;

m = new form.Map('wifi_schedule', _('Wifi Schedule'),_('Defines a schedule when to turn on and off wifi.'));

s = m.section(form.TypedSection, 'global', _('Manual control'));
s.optional = false;
s.rmempty = false;
s.anonymous = true;

o = s.option(form.Button, '', _('Activate wifi'));
o.write = function (section, value) {
fs.exec('/usr/bin/wifi_schedule.sh', ['start', 'manual']);
}


o = s.option(form.Button, '', _('Disable wifi gracefully'));
o.write = function (section, value) {
fs.exec('/usr/bin/wifi_schedule.sh', ['stop', 'manual']);
}

o = s.option(form.Button, '', _('Disable wifi forced'));
o.write = function (section, value) {
fs.exec('/usr/bin/wifi_schedule.sh', ['forcestop', 'manual']);
}


s = m.section(form.TypedSection, 'global', _('Global Settings'));
s.optional = false;
s.rmempty = false;
s.anonymous = true;



o = s.option(form.Flag, 'enabled', _('Enable Wifi Schedule'));
o.optional = false;
o.rmempty = false;
o.validate = function(section_id, value) {
if (value == '1') {
if ((checkFile('/sbin/wifi')) &&
(checkFile('/usr/bin/wifi_schedule.sh'))) {
return true;
} else {
return _('Could not find required /usr/bin/wifi_schedule.sh or /sbin/wifi');
}
} else {
return true;
}
};

o = s.option(form.Flag, 'logging', _('Enable logging'));
o.optional = false;
o.rmempty = false;
o.default = 0;

oUnloadmodules = s.option(form.Flag, 'unload_modules', _('Unload Modules (experimental; saves more power)'));
oUnloadmodules.optional = false;
oUnloadmodules.rmempty = false;
oUnloadmodules.default = 0;

oModules = s.option(form.TextValue, 'modules', '')
oModules.depends('unload_modules', '1');
oModules.wrap = 'off';
oModules.rows = 10;
oModules.cfgvalue = function (section) {
let mod = uci.get('wifi_schedule', section, 'modules');
if (mod === undefined || mod === null) {
mod = "";
}
return mod.replace(/ /g, "\r\n");
}
oModules.write = function (section, value) {
if (value !== undefined || value !== null) {
var valueList = value.replace(/[\r\n]+/g, " ").replace(/\s+$/, '');
return uci.set('wifi_schedule', section, 'modules', valueList);
}
}

o = s.option(form.Button, 'determine_modules', _('Determine Modules Automatically'));
o.depends('unload_modules', '1');
o.write = function (section) {
let output = fs.exec('/usr/bin/wifi_schedule.sh', ['getmodules']);
oModules.write(section, output);
}



s = m.section(form.TypedSection, 'entry', _('Schedule events'));
s.addremove = true;

o = s.option(form.Flag, 'enabled', _('Enable mode'));
o.rmempty = false;
o.optional = false;

o = s.option(form.MultiValue, 'daysofweek', _('Day(s) of Week'));
o.rmempty = false;
o.optional = false;
o.modalonly = true;
o.multiple = true;
o.size = 7;
o.value('Monday',_('Monday'));
o.value('Tuesday',_('Tuesday'));
o.value('Wednesday',_('Wednesday'));
o.value('Thursday',_('Thursday'));
o.value('Friday',_('Friday'));
o.value('Saturday',_('Saturday'));
o.value('Sunday',_('Sunday'));
o.write = function(section_id, value) {
return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
};


o = s.option (form.Value, 'starttime', _('Start WiFi'));
o.rmempty = false;
o.optional = false;
o.value('00:00');
o.value('01:00');
o.value('02:00');
o.value('03:00');
o.value('04:00');
o.value('05:00');
o.value('06:00');
o.value('07:00');
o.value('08:00');
o.value('09:00');
o.value('10:00');
o.value('11:00');
o.value('12:00');
o.value('13:00');
o.value('14:00');
o.value('15:00');
o.value('16:00');
o.value('17:00');
o.value('18:00');
o.value('19:00');
o.value('20:00');
o.value('21:00');
o.value('22:00');
o.value('23:00');
o.validate = function(section_id, value) {
return timeValidator(value, _('Start Time'))
};

o = s.option (form.Value, 'stoptime', _('Stop WiFi'));
o.rmempty = false;
o.optional = false;
o.value('00:00');
o.value('01:00');
o.value('02:00');
o.value('03:00');
o.value('04:00');
o.value('05:00');
o.value('06:00');
o.value('07:00');
o.value('08:00');
o.value('09:00');
o.value('10:00');
o.value('11:00');
o.value('12:00');
o.value('13:00');
o.value('14:00');
o.value('15:00');
o.value('16:00');
o.value('17:00');
o.value('18:00');
o.value('19:00');
o.value('20:00');
o.value('21:00');
o.value('22:00');
o.value('23:00');
o.validate = function(section_id, value) {
return timeValidator(value, _('Stop Time'))
};

o = s.option(form.Flag, 'forcewifidown', _('Force disabling wifi even if stations associated'));
o.default = false;
o.rmempty = false;
o.validate = function(section_id, value) {
if (value == '0') {
if (checkFile("/usr/bin/iwinfo")) {
return true;
} else {
return _("Could not find required program /usr/bin/iwinfo");
}
} else {
return true;
}
};


return m.render()
},
handleSaveApply: function (ev, mode) {
var Fn = L.bind(function() {
fs.exec('/usr/bin/wifi_schedule.sh', ['cron']);
document.removeEventListener('uci-applied',Fn);
});
document.addEventListener('uci-applied', Fn);
this.super('handleSaveApply', [ev, mode]);
},
});

This file was deleted.

Loading

0 comments on commit ac36148

Please sign in to comment.