feat: List now shows only unique SSIDs with the strongest corresponding

signal
This commit is contained in:
s-prechtl 2023-06-23 19:45:49 +02:00
parent a005d923fa
commit e5cb2d05f9
2 changed files with 20 additions and 3 deletions

View file

@ -1,5 +1,5 @@
use anyhow::*; use anyhow::*;
use std::{io::Write, mem::align_of_val, process::Command}; use std::{collections::HashMap, io::Write, process::Command, vec};
use wifi_connector::network::Network; use wifi_connector::network::Network;
fn get_available_wifis() -> Result<String> { fn get_available_wifis() -> Result<String> {
@ -113,8 +113,25 @@ fn print_header() {
println!("Index - In use - SSID - Signal"); println!("Index - In use - SSID - Signal");
} }
fn uniquify_networks(networks: Vec<Network>) -> Vec<Network> {
let mut unique_networks: HashMap<String, Network> = HashMap::new();
for network in &networks {
let current_ssid = &network.ssid;
if unique_networks.keys().any(|key| key == current_ssid) {
if unique_networks[current_ssid].signal < network.signal {
unique_networks.insert(current_ssid.to_owned(), network.to_owned());
}
} else {
unique_networks.insert(current_ssid.to_owned(), network.to_owned());
}
}
return unique_networks.into_values().collect();
}
fn main() { fn main() {
let all_networks = get_all_networks(); let all_networks = uniquify_networks(get_all_networks());
print_header(); print_header();
print_networks(&all_networks); print_networks(&all_networks);

View file

@ -1,6 +1,6 @@
use std::fmt::Display; use std::fmt::Display;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Network { pub struct Network {
pub in_use: bool, pub in_use: bool,
pub ssid: String, pub ssid: String,