diff options
author | Florian Eckert <fe@dev.tdt.de> | 2023-07-03 11:55:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-03 11:55:08 +0200 |
commit | 51d9b9d33f35c5c1452909677a75b6ac7ccf6de6 (patch) | |
tree | e943a65f5d4510d0e38c591afd3937f058a517e8 /protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js | |
parent | b06de7e4a11c5d17c8a1334b7806ba1ea90de3ca (diff) | |
parent | 594bc35b870c09ea47a66a59c5dfafd47b9b17d5 (diff) |
Merge pull request #6451 from lvoegl/pr/20230602-proto-modemmanager-status
luci-proto-modemmanager: add status page
Diffstat (limited to 'protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js')
-rw-r--r-- | protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js b/protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js new file mode 100644 index 0000000000..b8558b885b --- /dev/null +++ b/protocols/luci-proto-modemmanager/htdocs/luci-static/resources/modemmanager_helper.js @@ -0,0 +1,101 @@ +'use strict'; +'require baseclass'; +'require fs'; + +return baseclass.extend({ + + _mmcliBin: '/usr/bin/mmcli', + + _emptyStringValue: '--', + + _parseIndex: function (dbusPath) { + var index = dbusPath.split('/').slice(-1); + return parseInt(index); + }, + + _parseOutput: function (output) { + try { + return this._removeEmptyStrings(JSON.parse(output)); + } catch (err) { + return null; + } + }, + + _removeEmptyStrings: function (obj) { + if (obj == null) { + return obj; + } + + if (typeof obj == 'string') { + if (obj == this._emptyStringValue) { + obj = null; + } + } else if (Array.isArray()) { + obj = obj.map(L.bind(function (it) { + return this._removeEmptyStrings(it); + }, this)); + } else { + var keys = Object.keys(obj); + keys.forEach(L.bind(function (key) { + obj[key] = this._removeEmptyStrings(obj[key]); + }, this)); + } + + return obj; + }, + + getModems: function () { + return fs.exec_direct(this._mmcliBin, [ '-L', '-J' ]).then(L.bind(function (res) { + var json = this._parseOutput(res); + if (json == null) { + return []; + } + var modems = json['modem-list']; + var tasks = []; + + modems.forEach(L.bind(function (modem) { + var index = this._parseIndex(modem); + if (!isNaN(index)) { + tasks.push(this.getModem(index)); + } + }, this)); + return Promise.all(tasks); + }, this)); + }, + + getModem: function (index) { + return fs.exec_direct(this._mmcliBin, [ '-m', index, '-J' ]).then(L.bind(function (modem) { + return this._parseOutput(modem); + }, this)); + }, + + getModemSims: function (modem) { + var tasks = []; + var simSlots = modem.generic['sim-slots']; + var sim = modem.generic.sim; + if (sim != null && !simSlots.includes(sim)) { + simSlots.push(sim); + } + + simSlots.forEach(L.bind(function (modem) { + var index = this._parseIndex(modem); + if (!isNaN(index)) { + tasks.push(this.getSim(index)); + } + }, this)); + return Promise.all(tasks); + }, + + getSim: function (index) { + return fs.exec_direct(this._mmcliBin, [ '-i', index, '-J' ]).then(L.bind(function (sim) { + return this._parseOutput(sim); + }, this)); + }, + + getModemLocation: function (modem) { + var index = this._parseIndex(modem['dbus-path']); + return fs.exec_direct(this._mmcliBin, [ '-m', index, '--location-get', '-J' ]).then(L.bind(function (location) { + return this._parseOutput(location); + }, this)); + } +}); |