|
| 1 | +'use strict'; |
| 2 | +'require view'; |
| 3 | +'require form'; |
| 4 | +'require network'; |
| 5 | +'require uci'; |
| 6 | + |
| 7 | +return view.extend({ |
| 8 | + load: function() { |
| 9 | + return Promise.all([ |
| 10 | + network.getNetworks(), |
| 11 | + network.getDevices() |
| 12 | + ]); |
| 13 | + }, |
| 14 | + |
| 15 | + render: function(data) { |
| 16 | + var m, s, o; |
| 17 | + var networks = data[0] || []; |
| 18 | + var devices = data[1] || []; |
| 19 | + var ifaceMap = {}; |
| 20 | + |
| 21 | + m = new form.Map('smart-reboot', _('Smart Reboot'), |
| 22 | + _('Reboot only when the network is idle at the configured dawn time.')); |
| 23 | + |
| 24 | + s = m.section(form.TypedSection, 'settings', _('Settings')); |
| 25 | + s.anonymous = true; |
| 26 | + |
| 27 | + o = s.option(form.Flag, 'enabled', _('Enable')); |
| 28 | + o.rmempty = false; |
| 29 | + |
| 30 | + o = s.option(form.DummyValue, 'last_auto_reboot', _('Last automatic reboot')); |
| 31 | + o.cfgvalue = function(section_id) { |
| 32 | + return uci.get('smart-reboot', section_id, 'last_auto_reboot') || _('Never'); |
| 33 | + }; |
| 34 | + |
| 35 | + o = s.option(form.Value, 'time', _('Reboot time (HH:MM)')); |
| 36 | + o.placeholder = '04:00'; |
| 37 | + o.rmempty = false; |
| 38 | + o.validate = function(section_id, value) { |
| 39 | + if (!value || !value.match(/^([01][0-9]|2[0-3]):[0-5][0-9]$/)) |
| 40 | + return _('Time format must be HH:MM (24-hour). Example: 04:00'); |
| 41 | + |
| 42 | + return true; |
| 43 | + }; |
| 44 | + |
| 45 | + o = s.option(form.Flag, 'all_ifaces', _('Select all interfaces')); |
| 46 | + o.rmempty = false; |
| 47 | + |
| 48 | + o = s.option(form.MultiValue, 'ifaces', _('Monitored interfaces')); |
| 49 | + o.widget = 'select'; |
| 50 | + o.size = 8; |
| 51 | + o.depends('all_ifaces', '0'); |
| 52 | + o.depends('all_ifaces', ''); |
| 53 | + |
| 54 | + networks.forEach(function(netif) { |
| 55 | + var logical = netif.getName(); |
| 56 | + var dev = netif.getL3Device() || netif.getL2Device() || netif.getDevice(); |
| 57 | + var ifname = dev && dev.getName ? dev.getName() : null; |
| 58 | + |
| 59 | + if (!ifname || ifname === 'lo') |
| 60 | + return; |
| 61 | + |
| 62 | + ifaceMap[ifname] = ifaceMap[ifname] || { labels: [] }; |
| 63 | + if (logical && ifaceMap[ifname].labels.indexOf(logical) < 0) |
| 64 | + ifaceMap[ifname].labels.push(logical); |
| 65 | + }); |
| 66 | + |
| 67 | + (devices || []).forEach(function(dev) { |
| 68 | + var name = dev.getName(); |
| 69 | + if (!name || name === 'lo') |
| 70 | + return; |
| 71 | + |
| 72 | + ifaceMap[name] = ifaceMap[name] || { labels: [] }; |
| 73 | + }); |
| 74 | + |
| 75 | + Object.keys(ifaceMap).sort().forEach(function(ifname) { |
| 76 | + var labels = ifaceMap[ifname].labels; |
| 77 | + var text = labels.length ? '%s (%s)'.format(labels.join(', '), ifname) : ifname; |
| 78 | + o.value(ifname, text); |
| 79 | + }); |
| 80 | + |
| 81 | + o = s.option(form.Value, 'sample_seconds', _('Idle sampling duration (seconds)')); |
| 82 | + o.datatype = 'uinteger'; |
| 83 | + o.placeholder = '120'; |
| 84 | + o.rmempty = false; |
| 85 | + |
| 86 | + o = s.option(form.Value, 'byte_threshold', _('Idle threshold (bytes)')); |
| 87 | + o.datatype = 'uinteger'; |
| 88 | + o.placeholder = '262144'; |
| 89 | + o.rmempty = false; |
| 90 | + |
| 91 | + return m.render(); |
| 92 | + } |
| 93 | +}); |
0 commit comments