wol.js 3.5 KB

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