modemmanager_helper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. 'require baseclass';
  3. 'require fs';
  4. return baseclass.extend({
  5. _mmcliBin: '/usr/bin/mmcli',
  6. _emptyStringValue: '--',
  7. _parseIndex: function (dbusPath) {
  8. var index = dbusPath.split('/').slice(-1);
  9. return parseInt(index);
  10. },
  11. _parseOutput: function (output) {
  12. try {
  13. return this._removeEmptyStrings(JSON.parse(output));
  14. } catch (err) {
  15. return null;
  16. }
  17. },
  18. _removeEmptyStrings: function (obj) {
  19. if (obj == null) {
  20. return obj;
  21. }
  22. if (typeof obj == 'string') {
  23. if (obj == this._emptyStringValue) {
  24. obj = null;
  25. }
  26. } else if (Array.isArray()) {
  27. obj = obj.map(L.bind(function (it) {
  28. return this._removeEmptyStrings(it);
  29. }, this));
  30. } else {
  31. var keys = Object.keys(obj);
  32. keys.forEach(L.bind(function (key) {
  33. obj[key] = this._removeEmptyStrings(obj[key]);
  34. }, this));
  35. }
  36. return obj;
  37. },
  38. getModems: function () {
  39. return fs.exec_direct(this._mmcliBin, [ '-L', '-J' ]).then(L.bind(function (res) {
  40. var json = this._parseOutput(res);
  41. if (json == null) {
  42. return [];
  43. }
  44. var modems = json['modem-list'];
  45. var tasks = [];
  46. modems.forEach(L.bind(function (modem) {
  47. var index = this._parseIndex(modem);
  48. if (!isNaN(index)) {
  49. tasks.push(this.getModem(index));
  50. }
  51. }, this));
  52. return Promise.all(tasks);
  53. }, this));
  54. },
  55. getModem: function (index) {
  56. return fs.exec_direct(this._mmcliBin, [ '-m', index, '-J' ]).then(L.bind(function (modem) {
  57. return this._parseOutput(modem);
  58. }, this));
  59. },
  60. getModemSims: function (modem) {
  61. var tasks = [];
  62. var simSlots = modem.generic['sim-slots'];
  63. var sim = modem.generic.sim;
  64. if (sim != null && !simSlots.includes(sim)) {
  65. simSlots.push(sim);
  66. }
  67. simSlots.forEach(L.bind(function (modem) {
  68. var index = this._parseIndex(modem);
  69. if (!isNaN(index)) {
  70. tasks.push(this.getSim(index));
  71. }
  72. }, this));
  73. return Promise.all(tasks);
  74. },
  75. getSim: function (index) {
  76. return fs.exec_direct(this._mmcliBin, [ '-i', index, '-J' ]).then(L.bind(function (sim) {
  77. return this._parseOutput(sim);
  78. }, this));
  79. },
  80. getModemLocation: function (modem) {
  81. var index = this._parseIndex(modem['dbus-path']);
  82. return fs.exec_direct(this._mmcliBin, [ '-m', index, '--location-get', '-J' ]).then(L.bind(function (location) {
  83. return this._parseOutput(location);
  84. }, this));
  85. }
  86. });