'use strict'; 'require baseclass'; 'require form'; 'require fs'; 'require view'; 'require ui'; 'require uci'; 'require poll'; 'require dom'; 'require tools.widgets as widgets'; /* Copyright 2021-2024 Rafał Wabik - IceG - From eko.one.pl forum Licensed to the GNU General Public License v3.0. Thanks to https://github.com/koshev-msk for the initial progress bar calculation for rssi/rsrp/rsrq/sinnr. */ function csq_bar(v, m) { var pg = document.querySelector('#csq') var vn = parseInt(v) || 0; var mn = parseInt(m) || 100; var pc = Math.floor((100 / mn) * vn); if (vn >= 20 && vn <= 31 ) { pg.firstElementChild.style.background = 'lime'; var tip = _('Very good'); }; if (vn >= 14 && vn <= 19) { pg.firstElementChild.style.background = 'yellow'; var tip = _('Good'); }; if (vn >= 10 && vn <= 13) { pg.firstElementChild.style.background = 'darkorange'; var tip = _('Weak'); }; if (vn <= 9 && vn >= 1) { pg.firstElementChild.style.background = 'red'; var tip = _('Very weak'); }; pg.firstElementChild.style.width = pc + '%'; pg.style.width = '33%'; pg.setAttribute('title', '%s'.format(v) + ' | ' + tip + ' '); } function rssi_bar(v, m) { var pg = document.querySelector('#rssi') var vn = parseInt(v) || 0; var mn = parseInt(m) || 100; if (vn > -50) { vn = -50 }; if (vn < -110) { vn = -110 }; var pc = Math.floor(100*(1-(-50 - vn)/(-50 - mn))); if (vn > -70) { pg.firstElementChild.style.background = 'lime'; var tip = _('Very good'); }; if (vn >= -85 && vn <= -70) { pg.firstElementChild.style.background = 'yellow'; var tip = _('Good'); }; if (vn >= -100 && vn <= -86) { pg.firstElementChild.style.background = 'darkorange'; var tip = _('Weak'); }; if (vn < -100) { pg.firstElementChild.style.background = 'red'; var tip = _('Very weak'); }; pg.firstElementChild.style.width = pc + '%'; pg.style.width = '33%'; pg.firstElementChild.style.animationDirection = "reverse"; pg.setAttribute('title', '%s'.format(v) + ' | ' + tip + ' '); } function rsrp_bar(v, m) { var pg = document.querySelector('#rsrp') var vn = parseInt(v) || 0; var mn = parseInt(m) || 100; if (vn > -50) { vn = -50 }; if (vn < -140) { vn = -140 }; var pc = Math.floor(120*(1-(-50 - vn)/(-70 - mn))); if (vn >= -80 ) { pg.firstElementChild.style.background = 'lime'; var tip = _('Very good'); }; if (vn >= -90 && vn <= -79) { pg.firstElementChild.style.background = 'yellow'; var tip = _('Good'); }; if (vn >= -100 && vn <= -89) { pg.firstElementChild.style.background = 'darkorange'; var tip = _('Weak'); }; if (vn < -100) { pg.firstElementChild.style.background = 'red'; var tip = _('Very weak'); }; pg.firstElementChild.style.width = pc + '%'; pg.style.width = '33%'; pg.firstElementChild.style.animationDirection = "reverse"; pg.setAttribute('title', '%s'.format(v) + ' | ' + tip + ' '); } function sinr_bar(v, m) { var pg = document.querySelector('#sinr') var vn = parseInt(v) || 0; var mn = parseInt(m) || 100; var pc = Math.floor(100-(100*(1-((mn - vn)/(mn - 40))))); if (vn > 20 ) { pg.firstElementChild.style.background = 'lime'; var tip = _('Excellent'); }; if (vn >= 13 && vn <= 20) { pg.firstElementChild.style.background = 'yellow'; var tip = _('Good'); }; if (vn > 0 && vn <= 12) { pg.firstElementChild.style.background = 'darkorange'; var tip = _('Mid cell'); }; if (vn <= 0) { pg.firstElementChild.style.background = 'red'; var tip = _('Cell edge'); }; pg.firstElementChild.style.width = pc + '%'; pg.style.width = '33%'; pg.firstElementChild.style.animationDirection = "reverse"; pg.setAttribute('title', '%s'.format(v) + ' | ' + tip + ' '); } function rsrq_bar(v, m) { var pg = document.querySelector('#rsrq') var vn = parseInt(v) || 0; var mn = parseInt(m) || 100; var pc = Math.floor(115-(100/mn)*vn); if (vn > 0) { vn = 0; }; if (vn >= -10 ) { pg.firstElementChild.style.background = 'lime'; var tip = _('Excellent'); }; if (vn >= -15 && vn <= -9) { pg.firstElementChild.style.background = 'yellow'; var tip = _('Good'); }; if (vn >= -20 && vn <= -14) { pg.firstElementChild.style.background = 'darkorange'; var tip = _('Mid cell'); }; if (vn < -20) { pg.firstElementChild.style.background = 'red'; var tip = _('Cell edge'); }; pg.firstElementChild.style.width = pc + '%'; pg.style.width = '33%'; pg.firstElementChild.style.animationDirection = "reverse"; pg.setAttribute('title', '%s'.format(v) + ' | ' + tip + ' '); } function SIMdata(data) { var sdata = JSON.parse(data); if (sdata.simslot.length > 0) { return ui.itemlist(E('span'), [ _('SIM Slot'), sdata.simslot, _('SIM IMSI'), sdata.imsi, _('SIM ICCID'), sdata.iccid, _('Modem IMEI'), sdata.imei, _('Hint'), _('CLICK ME TO SEE NEW MENU') ]); } else { return ui.itemlist(E('span'), [ _('SIM IMSI'), sdata.imsi, _('SIM ICCID'), sdata.iccid, _('Modem IMEI'), sdata.imei, _('Hint'), _('CLICK ME TO SEE NEW MENU') ]); } } function active_select() { uci.load('modemdefine').then(function() { var modemz = (uci.get('modemdefine', '@modemdefine[1]', 'comm_port')); if (!modemz) { document.getElementById("modc").disabled = true; } else { document.getElementById("modc").disabled = false; } }); } function formatDuration(sec) { if (sec === '-') { return '-'; } if (sec === '') { return '-'; } var d = Math.floor(sec / 86400), h = Math.floor(sec / 3600) % 24, m = Math.floor(sec / 60) % 60, s = sec % 60; var time = d > 0 ? d + 'd ' : ''; if (time !== '') { time += h + 'h '; } else { time = h > 0 ? h + 'h ' : ''; } if (time !== '') { time += m + 'm '; } else { time = m > 0 ? m + 'm ' : ''; } time += s + 's'; return time; } function formatDateTime(s) { if (s.length == 14) { return s.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, "$1-$2-$3 $4:$5:$6"); } else if (s.length == 12) { return s.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/, "$1-$2-$3 $4:$5"); } else if (s.length == 8) { return s.replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3"); } else if (s.length == 6) { return s.replace(/(\d{4})(\d{2})/, "$1-$2"); } return s; } function checkOperatorName(t) { var w = t.split(" "); var f = {}; for (var i = 0; i < w.length; i++) { var wo = w[i].toLowerCase(); if (!f.hasOwnProperty(wo)) { f[wo] = i; } } var u = Object.keys(f).map(function(wo) { return w[f[wo]]; }); var r = u.join(" "); return r; } return view.extend({ modemDialog: baseclass.extend({ __init__: function(title, description, callback) { this.title = title; this.description = description; this.callback = callback; }, load: function() { return uci.load('modemdefine'); }, render: function(content) { var sections = uci.sections('modemdefine'); var portM = sections.length; var result = ""; for (var i = 1; i < portM; i++) { result += sections[i].comm_port + '_' + sections[i].network + '#' + sections[i].comm_port + ' - ' + sections[i].modem + ' (' + sections[i].user_desc + ');'; } var result = result.slice(0, -1); var result = result.replace("(undefined)", ""); ui.showModal(this.title, [ E('div', { 'class': 'cbi-section' }, [ E('div', { 'class': 'cbi-section-descr' }, this.description), E('div', { 'class': 'cbi-section' }, E('p', {}, E('div', { 'class': 'cbi-value' }, [ E('p'), E('label', { 'class': 'cbi-value-title' }, [ _('Modem') ]), E('div', { 'class': 'cbi-value-field' }, [ E('select', { 'class': 'cbi-input-select', 'id': 'mselect', 'style': 'margin:0px 0; width:100%;', }, (result || "").trim().split(/;/).map(function(cmd) { var fields = cmd.split(/#/); var name = fields[1]; var code = fields[0]; return E('option', { 'value': code }, name ) }) ) ]) ]), ) ), ]), E('div', { 'class': 'right' }, [ E('button', { 'class': 'btn', 'click': ui.createHandlerFn(this, this.handleDissmis), }, _('Cancel')), ' ', E('button', { 'id': 'btn_save', 'class': 'btn cbi-button-positive important', 'click': ui.createHandlerFn(this, this.handleSave), }, _('Save')), ]), ]); }, handleSave: function(ev) { return uci.load('modemdefine').then(function() { var vx = document.getElementById('mselect').value; var marr = vx.split('_'); uci.set('modemdefine', '@general[0]', 'main_modem', marr[0].toString()); uci.set('modemdefine', '@general[0]', 'main_network', marr[1].toString()); uci.save(); uci.apply(); window.setTimeout(function() { if (!poll.active()) poll.start(); location.reload(); //ev.target.blur(); }, 2000).finally(); }); }, handleDissmis: function(ev) { ui.hideModal(); if (!poll.active()) poll.start(); }, show: function() { ui.showModal(null, E('p', { 'class': 'spinning' }, _('Loading')) ); poll.stop(); this.load().then(content => { ui.hideModal(); return this.render(content); }).catch(e => { ui.hideModal(); return this.error(e); }) }, }), simDialog: baseclass.extend({ __init__: function(title, description, callback) { this.title = title; this.description = description; this.callback = callback; }, load: function() { return L.resolveDefault(fs.exec_direct('/usr/share/3ginfo-lite/3ginfo.sh', [ 'json' ])); }, render: function(content) { var json = JSON.parse(content); if (json) { if (!json.imei.length > 2) { return false, poll.start() } } ui.showModal(this.title, [ E('div', { 'class': 'cbi-section' }, [ E('div', { 'class': 'cbi-section-descr' }, this.description), E('div', { 'class': 'cbi-section' }, E('p', {}, E('div', { 'class': 'cbi-value' }, [ E('p'), E('label', { 'class': 'cbi-value-title' }, [ _('SIM IMSI') ]), E('div', { 'class': 'cbi-value-field' }, [ E('input', { 'class': 'cbi-input-text', 'readonly': 'readonly', 'value': json.imsi }, null), ]), E('label', { 'class': 'cbi-value-title' }, [ _('SIM ICCID') ]), E('div', { 'class': 'cbi-value-field' }, [ E('input', { 'class': 'cbi-input-text', 'readonly': 'readonly', 'value': json.iccid }, null), ]), E('label', { 'class': 'cbi-value-title' }, [ _('Modem IMEI') ]), E('div', { 'class': 'cbi-value-field' }, [ E('input', { 'class': 'cbi-input-text', 'readonly': 'readonly', 'value': json.imei }, null), ]) ]), ) ), ]), E('div', { 'class': 'right' }, [ E('button', { 'class': 'btn', 'click': ui.createHandlerFn(this, this.handleDissmis), }, _('Close')), ]), ]); }, handleDissmis: function(ev) { ui.hideModal(); if (!poll.active()) poll.start(); }, show: function() { ui.showModal(null, E('p', { 'class': 'spinning' }, _('Loading')) ); poll.stop(); this.load().then(content => { ui.hideModal(); return this.render(content); }).catch(e => { ui.hideModal(); return this.error(e); }) }, }), formdata: { threeginfo: {} }, load: function() { return L.resolveDefault(fs.exec_direct('/usr/share/3ginfo-lite/3ginfo.sh', [ 'json' ])); }, render: function(data) { var m, s, o; active_select(); var upModemDialog = new this.modemDialog( _('Defined modems'), _('Interface for selecting user defined modems.'), ); var upSIMDialog = new this.simDialog( _('SIM card menu'), _('Information read from the SIM card and device.'), ); if (data != null){ try { var json = JSON.parse(data); if(!json.hasOwnProperty('error')){ if (json.registration == 'SIM not inserted' || json.registration == '-') { ui.addNotification(null, E('p', _('Problem with registering to the network, check the SIM card.')), 'info'); } if (json.registration == 'SIM PIN required') { ui.addNotification(null, E('p', _('SIM PIN required')), 'info'); } if (json.registration == 'SIM PUK required') { ui.addNotification(null, E('p', _('SIM PUK required')), 'info'); } if (json.registration == 'SIM failure') { ui.addNotification(null, E('p', _('SIM failure')), 'info'); } if (json.registration == 'SIM busy') { ui.addNotification(null, E('p', _('SIM busy')), 'info'); } if (json.registration == 'SIM wrong') { ui.addNotification(null, E('p', _('SIM wrong')), 'info'); } if (json.registration == 'SIM PIN2 required') { ui.addNotification(null, E('p', _('SIM PIN2 required')), 'info'); } if (json.registration == 'SIM PUK2 required') { ui.addNotification(null, E('p', _('SIM PUK2 required')), 'info'); } if (json.signal == '0' || json.signal == '' || json.signal == '-') { ui.addNotification(null, E('p', _('There is a problem reading data from the modem. \

Please check: \