luci-app-wol: use uci etherwake default interface
[project/luci.git] / applications / luci-app-wol / htdocs / luci-static / resources / view / wol.js
1 'use strict';
2 'require uci';
3 'require fs';
4 'require ui';
5 'require rpc';
6 'require form';
7 'require tools.widgets as widgets';
8
9 return L.view.extend({
10 formdata: { wol: {} },
11
12 callHostHints: rpc.declare({
13 object: 'luci-rpc',
14 method: 'getHostHints',
15 expect: { '': {} }
16 }),
17
18 load: function() {
19 return Promise.all([
20 L.resolveDefault(fs.stat('/usr/bin/etherwake')),
21 L.resolveDefault(fs.stat('/usr/bin/wol')),
22 this.callHostHints(),
23 uci.load('etherwake')
24 ]);
25 },
26
27 render: function(data) {
28 var has_ewk = data[0],
29 has_wol = data[1],
30 hosts = data[2],
31 m, s, o;
32
33 this.formdata.has_ewk = has_ewk;
34 this.formdata.has_wol = has_wol;
35
36 m = new form.JSONMap(this.formdata, _('Wake on LAN'),
37 _('Wake on LAN is a mechanism to remotely boot computers in the local network.'));
38
39 s = m.section(form.NamedSection, 'wol');
40
41 if (has_ewk && has_wol) {
42 o = s.option(form.ListValue, 'executable', _('WoL program'),
43 _('Sometimes only one of the two tools works. If one fails, try the other one'));
44
45 o.value('/usr/bin/etherwake', 'Etherwake');
46 o.value('/usr/bin/wol', 'WoL');
47 }
48
49 if (has_ewk) {
50 o = s.option(widgets.DeviceSelect, 'iface', _('Network interface to use'),
51 _('Specifies the interface the WoL packet is sent on'));
52
53 o.default = uci.get('etherwake', 'setup', 'interface');
54 o.rmempty = false;
55 o.noaliases = true;
56 o.noinactive = true;
57
58 if (has_wol)
59 o.depends('executable', '/usr/bin/etherwake');
60 }
61
62 o = s.option(form.Value, 'mac', _('Host to wake up'),
63 _('Choose the host to wake up or enter a custom MAC address to use'));
64
65 o.rmempty = false;
66
67 Object.keys(hosts).sort().forEach(function(mac) {
68 o.value(mac, E([], [ mac, ' (', E('strong', [hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?']), ')' ]));
69 });
70
71 if (has_ewk) {
72 o = s.option(form.Flag, 'broadcast', ('Send to broadcast address'));
73
74 if (has_wol)
75 o.depends('executable', '/usr/bin/etherwake');
76 }
77
78 return m.render();
79 },
80
81 handleWakeup: function(ev) {
82 var map = document.querySelector('#maincontent .cbi-map'),
83 data = this.formdata;
84
85 return L.dom.callClassMethod(map, 'save').then(function() {
86 if (!data.wol.mac)
87 return alert(_('No target host specified!'));
88
89 var bin = data.executable || (data.has_ewk ? '/usr/bin/etherwake' : '/usr/bin/wol'),
90 args = [];
91
92 if (bin == '/usr/bin/etherwake') {
93 args.push('-D', '-i', data.wol.iface);
94
95 if (data.wol.broadcast == '1')
96 args.push('-b');
97
98 args.push(data.wol.mac);
99 }
100 else {
101 args.push('-v', data.wol.mac);
102 }
103
104 ui.showModal(_('Waking host'), [
105 E('p', { 'class': 'spinning' }, [ 'Starting WoL utility…' ])
106 ]);
107
108 return fs.exec(bin, args).then(function(res) {
109 ui.showModal(_('Waking host'), [
110 res.stderr ? E('p', [ res.stdout ]) : '',
111 res.stderr ? E('pre', [ res.stderr ]) : '',
112 E('div', { 'class': 'right' }, [
113 E('button', {
114 'class': 'cbi-button cbi-button-primary',
115 'click': ui.hideModal
116 }, [ _('Dismiss') ])
117 ])
118 ]);
119 }).catch(function(err) {
120 ui.hideModal();
121 ui.addNotification(null, [
122 E('p', [ _('Waking host failed: '), err ])
123 ]);
124 });
125 });
126 },
127
128 addFooter: function() {
129 return E('div', { 'class': 'cbi-page-actions' }, [
130 E('button', {
131 'class': 'cbi-button cbi-button-save',
132 'click': L.ui.createHandlerFn(this, 'handleWakeup')
133 }, [ _('Wake up host') ])
134 ]);
135 }
136 });