2
0

battstatus.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. 'require ui';
  3. 'require rpc';
  4. 'require poll';
  5. 'require baseclass';
  6. var callBatteryStatus = rpc.declare({
  7. object: 'luci.battstatus',
  8. method: 'getBatteryStatus',
  9. expect: { '': {} }
  10. });
  11. var devices = {};
  12. return baseclass.extend({
  13. __init__: function() {
  14. this.updateIndicator();
  15. poll.add(L.bind(this.updateIndicator, this), 5);
  16. },
  17. updateIndicator: function() {
  18. return callBatteryStatus().then(L.bind(function(devs) {
  19. for (var dev in devs) {
  20. var info = devs[dev];
  21. if (info.valid) {
  22. info.status = (info.charging ? _('Charging') : _('Not Charging')) + ": " + info.percentage + "%";
  23. info.state = "active";
  24. if (info.percentage <= 20)
  25. info.color = "Red";
  26. else if (info.percentage <= 30)
  27. info.color = "GoldenRod";
  28. } else {
  29. info.status = info.message;
  30. info.state = "inactive";
  31. }
  32. info.name = "battery-" + dev.replace(" ", "-");
  33. ui.showIndicator(info.name, info.status, null, info.state);
  34. if (typeof info.color != 'undefined') {
  35. info.element = document.querySelector('[data-indicator="${info.name}"]');
  36. info.element.innerHTML = '<span style="color:${info.color}">${info.status}</span>';
  37. }
  38. devices[dev] = info;
  39. }
  40. for (var dev in devices) {
  41. if (!devs.hasOwnProperty(dev)) {
  42. ui.hideIndicator('battery-%s'.format(dev));
  43. delete devices[dev];
  44. }
  45. }
  46. }, this));
  47. }
  48. });