You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.3 KiB
164 lines
4.3 KiB
7 years ago
|
(function (w) {
|
||
7 years ago
|
const authStr = ['Open', 'WEP', 'WPA', 'WPA2', 'WPA/WPA2']
|
||
|
let curSSID
|
||
7 years ago
|
|
||
|
// Get XX % for a slider input
|
||
7 years ago
|
function rangePt (inp) {
|
||
|
return Math.round(((inp.value / inp.max) * 100)) + '%'
|
||
7 years ago
|
}
|
||
|
|
||
|
// Display selected STA SSID etc
|
||
7 years ago
|
function selectSta (name, password, ip) {
|
||
|
$('#sta_ssid').val(name)
|
||
|
$('#sta_password').val(password)
|
||
|
|
||
7 years ago
|
$('#sta-nw').toggleClass('hidden', name.length === 0)
|
||
7 years ago
|
$('#sta-nw-nil').toggleClass('hidden', name.length > 0)
|
||
|
|
||
7 years ago
|
$('#sta-nw .essid').html(esc(name))
|
||
7 years ago
|
const nopw = undef(password) || password.length === 0
|
||
7 years ago
|
$('#sta-nw .passwd').toggleClass('hidden', nopw)
|
||
|
$('#sta-nw .nopasswd').toggleClass('hidden', !nopw)
|
||
|
$('#sta-nw .ip').html(ip.length > 0 ? tr('wifi.connected_ip_is') + ip : tr('wifi.not_conn'))
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Update display for received response */
|
||
7 years ago
|
function onScan (resp, status) {
|
||
|
// var ap_json = {
|
||
|
// "result": {
|
||
|
// "inProgress": "0",
|
||
|
// "APs": [
|
||
|
// {"essid": "Chlivek", "bssid": "88:f7:c7:52:b3:99", "rssi": "204", "enc": "4", "channel": "1"},
|
||
|
// {"essid": "TyNikdy", "bssid": "5c:f4:ab:0d:f1:1b", "rssi": "164", "enc": "3", "channel": "1"},
|
||
|
// ]
|
||
|
// }
|
||
|
// };
|
||
7 years ago
|
|
||
7 years ago
|
if (status !== 200) {
|
||
7 years ago
|
// bad response
|
||
7 years ago
|
rescan(5000) // wait 5sm then retry
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
|
try {
|
||
7 years ago
|
resp = JSON.parse(resp)
|
||
7 years ago
|
} catch (e) {
|
||
7 years ago
|
console.log(e)
|
||
|
rescan(5000)
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
const done = !bool(resp.result.inProgress) && (resp.result.APs.length > 0)
|
||
|
rescan(done ? 15000 : 1000)
|
||
|
if (!done) return // no redraw yet
|
||
7 years ago
|
|
||
|
// clear the AP list
|
||
7 years ago
|
let $list = $('#ap-list')
|
||
7 years ago
|
// remove old APs
|
||
7 years ago
|
$('#ap-list .AP').remove()
|
||
7 years ago
|
|
||
7 years ago
|
$list.toggleClass('hidden', !done)
|
||
|
$('#ap-loader').toggleClass('hidden', done)
|
||
7 years ago
|
|
||
|
// scan done
|
||
|
resp.result.APs.sort(function (a, b) {
|
||
7 years ago
|
return b.rssi - a.rssi
|
||
7 years ago
|
}).forEach(function (ap) {
|
||
7 years ago
|
ap.enc = parseInt(ap.enc)
|
||
7 years ago
|
|
||
7 years ago
|
if (ap.enc > 4) return // hide unsupported auths
|
||
7 years ago
|
|
||
7 years ago
|
let item = mk('div')
|
||
7 years ago
|
|
||
7 years ago
|
let $item = $(item)
|
||
7 years ago
|
.data('ssid', ap.essid)
|
||
|
.data('pwd', ap.enc)
|
||
|
.attr('tabindex', 0)
|
||
7 years ago
|
.addClass('AP')
|
||
7 years ago
|
|
||
|
// mark current SSID
|
||
7 years ago
|
if (ap.essid === curSSID) {
|
||
7 years ago
|
$item.addClass('selected')
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
let inner = mk('div')
|
||
7 years ago
|
let escapedSSID = $.htmlEscape(ap.essid)
|
||
7 years ago
|
$(inner).addClass('inner')
|
||
7 years ago
|
.htmlAppend(`<div class="rssi">${ap.rssi_perc}</div>`)
|
||
|
.htmlAppend(`<div class="essid" title="${escapedSSID}">${escapedSSID}</div>`)
|
||
|
.htmlAppend(`<div class="auth">${authStr[ap.enc]}</div>`)
|
||
7 years ago
|
|
||
|
$item.on('click', function () {
|
||
7 years ago
|
let $th = $(this)
|
||
7 years ago
|
|
||
7 years ago
|
const conn_ssid = $th.data('ssid')
|
||
|
let conn_pass = ''
|
||
7 years ago
|
|
||
|
if (+$th.data('pwd')) {
|
||
|
// this AP needs a password
|
||
7 years ago
|
conn_pass = prompt(tr('wifi.enter_passwd').replace(':ssid:', conn_ssid))
|
||
|
if (!conn_pass) return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
$('#sta_password').val(conn_pass)
|
||
|
$('#sta_ssid').val(conn_ssid)
|
||
|
selectSta(conn_ssid, conn_pass, '')
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
item.appendChild(inner)
|
||
|
$list[0].appendChild(item)
|
||
|
})
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
function startScanning () {
|
||
|
$('#ap-loader').removeClass('hidden')
|
||
|
$('#ap-scan').addClass('hidden')
|
||
|
$('#ap-loader .anim-dots').html('.')
|
||
7 years ago
|
|
||
7 years ago
|
scanAPs()
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Ask the CGI what APs are visible (async) */
|
||
7 years ago
|
function scanAPs () {
|
||
7 years ago
|
if (_demo) {
|
||
7 years ago
|
onScan(_demo_aps, 200)
|
||
7 years ago
|
} else {
|
||
7 years ago
|
$.get('http://' + _root + '/cfg/wifi/scan', onScan)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
function rescan (time) {
|
||
|
setTimeout(scanAPs, time)
|
||
7 years ago
|
}
|
||
|
|
||
|
/** Set up the WiFi page */
|
||
7 years ago
|
function wifiInit (cfg) {
|
||
7 years ago
|
// Update slider value displays
|
||
|
$('.Row.range').forEach(function (x) {
|
||
7 years ago
|
let inp = x.querySelector('input')
|
||
|
let disp1 = x.querySelector('.x-disp1')
|
||
|
let disp2 = x.querySelector('.x-disp2')
|
||
|
let t = rangePt(inp)
|
||
|
$(disp1).html(t)
|
||
|
$(disp2).html(t)
|
||
7 years ago
|
$(inp).on('input', function () {
|
||
7 years ago
|
t = rangePt(inp)
|
||
|
$(disp1).html(t)
|
||
|
$(disp2).html(t)
|
||
|
})
|
||
|
})
|
||
7 years ago
|
|
||
|
// Forget STA credentials
|
||
|
$('#forget-sta').on('click', function () {
|
||
7 years ago
|
selectSta('', '', '')
|
||
|
return false
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
selectSta(cfg.sta_ssid, cfg.sta_password, cfg.sta_active_ip)
|
||
|
curSSID = cfg.sta_active_ssid
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
w.init = wifiInit
|
||
|
w.startScanning = startScanning
|
||
|
})(window.WiFi = {})
|