Merge pull request #589 from billz/zbchristian/wifi-sorting

Sort and filter available client networks
This commit is contained in:
Bill Zimmerman 2020-05-26 09:52:51 +02:00 committed by GitHub
commit 8c6f0bd5fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 59 deletions

View File

@ -13,5 +13,6 @@ $ssid = null;
knownWifiStations($networks);
nearbyWifiStations($networks, !isset($_REQUEST["refresh"]));
connectedWifiStations($networks);
sortNetworksByRSSI($networks);
echo renderTemplate('wifi_stations', compact('networks'));

View File

@ -94,6 +94,7 @@ function DisplayWPAConfig()
nearbyWifiStations($networks);
connectedWifiStations($networks);
sortNetworksByRSSI($networks);
echo renderTemplate("configure_client", compact("status"));
}

View File

@ -52,7 +52,8 @@ function nearbyWifiStations(&$networks, $cached = true)
}
$scan_results = cache(
$cacheKey, function () {
$cacheKey,
function () {
exec('sudo wpa_cli -i ' . RASPI_WIFI_CLIENT_INTERFACE . ' scan');
sleep(3);
@ -63,16 +64,30 @@ function nearbyWifiStations(&$networks, $cached = true)
}
);
// get the name of the AP. Should be excluded from nearby networks
exec('cat '.RASPI_HOSTAPD_CONFIG.' | sed -rn "s/ssid=(.*)\s*$/\1/p" ', $ap_ssid);
$ap_ssid = $ap_ssid[0];
foreach (explode("\n", $scan_results) as $network) {
$arrNetwork = preg_split("/[\t]+/", $network); // split result into array
if (!array_key_exists(4, $arrNetwork) ||
trim($arrNetwork[4]) == $ap_ssid) {
continue;
}
$ssid = trim($arrNetwork[4]);
// filter SSID string: anything invisible in 7bit ASCII or quotes -> ignore network
if (preg_match('[\x00-\x1f\x7f-\xff\'\`\´\"]', $ssid)) {
continue;
}
// If network is saved
if (array_key_exists(4, $arrNetwork) && array_key_exists($arrNetwork[4], $networks)) {
$networks[$arrNetwork[4]]['visible'] = true;
$networks[$arrNetwork[4]]['channel'] = ConvertToChannel($arrNetwork[1]);
if (array_key_exists($ssid, $networks)) {
$networks[$ssid]['visible'] = true;
$networks[$ssid]['channel'] = ConvertToChannel($arrNetwork[1]);
// TODO What if the security has changed?
} else {
$networks[$arrNetwork[4]] = array(
$networks[$ssid] = array(
'configured' => false,
'protocol' => ConvertToSecurity($arrNetwork[3]),
'channel' => ConvertToChannel($arrNetwork[1]),
@ -82,9 +97,11 @@ function nearbyWifiStations(&$networks, $cached = true)
);
}
// Save RSSI
if (array_key_exists(4, $arrNetwork)) {
$networks[$arrNetwork[4]]['RSSI'] = $arrNetwork[2];
// Save RSSI, if the current value is larger than the already stored
if (array_key_exists(4, $arrNetwork) && array_key_exists($arrNetwork[4], $networks)) {
if (! array_key_exists('RSSI', $networks[$arrNetwork[4]]) || $networks[$ssid]['RSSI'] < $arrNetwork[2]) {
$networks[$ssid]['RSSI'] = $arrNetwork[2];
}
}
}
}
@ -98,3 +115,21 @@ function connectedWifiStations(&$networks)
}
}
}
function sortNetworksByRSSI(&$networks)
{
$valRSSI = array();
foreach ($networks as $SSID => $net) {
if (!array_key_exists('RSSI', $net)) {
$net['RSSI'] = -1000;
}
$valRSSI[$SSID] = $net['RSSI'];
}
$nets = $networks;
arsort($valRSSI);
$networks = array();
foreach ($valRSSI as $SSID => $RSSI) {
$networks[$SSID] = $nets[$SSID];
$networks[$SSID]['RSSI'] = $RSSI;
}
}

View File

@ -40,7 +40,9 @@
<div class="info-item-wifi"><?php echo _("RSSI"); ?></div>
<div>
<?php echo htmlspecialchars($network['RSSI'], ENT_QUOTES);
<?php
if (isset($network['RSSI']) && $network['RSSI'] >= -200) {
echo htmlspecialchars($network['RSSI'], ENT_QUOTES);
echo "dB (";
if ($network['RSSI'] >= -50) {
echo 100;
@ -50,6 +52,9 @@
echo 2*($network['RSSI'] + 100);
}
echo "%)";
} else {
echo " not found ";
}
?>
</div>