Pass config from main to the relevant places

This commit is contained in:
jmb05
2026-11-02 17:06:42 +01:00
parent de1f731e63
commit cdfc5b8d36
13 changed files with 170 additions and 107 deletions

View File

@@ -112,7 +112,7 @@ pub enum NetworkState {
#[derive(Debug, Clone)]
pub struct NetworkBackendHandle {
subscribers: Vec<Sender<NetworkState>>,
command_tx: UnboundedSender<NetworkCommand>,
command_tx: Option<UnboundedSender<NetworkCommand>>,
}
struct EventDebouncer {
@@ -140,12 +140,9 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
&self.subscribers
}
fn init(registration_rx: Receiver<Sender<NetworkState>>) {
fn init(mut self, registration_rx: Receiver<Sender<NetworkState>>) {
let (command_tx, mut command_rx) = tokio::sync::mpsc::unbounded_channel::<NetworkCommand>();
let instance = Self {
subscribers: vec![],
command_tx,
};
self.command_tx = Some(command_tx);
std::thread::spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
@@ -163,7 +160,7 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
std::thread::spawn({
let nm = nm.clone();
let mut instance = instance.clone();
let mut instance = self.clone();
move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
@@ -172,7 +169,7 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
runtime.block_on(async move {
while let Ok(tx) = registration_rx.recv() {
if tx.send(NetworkState::Init(instance.command_tx.clone())).is_ok() {
if tx.send(NetworkState::Init(instance.command_tx.clone().unwrap())).is_ok() {
instance.subscribers.push(tx);
let _ = instance.send_all_networks(&nm).await;
let _ = instance.send_active_connection(&nm).await;
@@ -205,7 +202,7 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
// Run the command without stopping event processing
// while a potentially slow connection operation runs.
let nm = nm.clone();
let instance_clone = instance.clone();
let instance_clone = self.clone();
tokio::spawn(async move {
match command {
@@ -241,7 +238,7 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
match event {
Some(event_res) => {
match event_res {
Ok(event) => instance.handle_event(&nm, event, &mut debouncer).await,
Ok(event) => self.handle_event(&nm, event, &mut debouncer).await,
Err(err) => error!(&LOGGER, "Error: {err:?}"),
}
}
@@ -250,7 +247,7 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
}
_ = &mut debouncer.sleep, if debouncer.deadline.is_some() => {
debouncer.deadline = None;
let _ = instance.send_all_networks(&nm).await;
let _ = self.send_all_networks(&nm).await;
}
}
}
@@ -260,6 +257,12 @@ impl BackendThread<NetworkState, UnboundedSender<NetworkCommand>> for NetworkBac
}
impl NetworkBackendHandle {
pub fn new() -> NetworkBackendHandle {
Self {
subscribers: vec![],
command_tx: None,
}
}
async fn connect(&self, nm: &NetworkManager, ssid: String, creds: WifiSecurity) {
let previous_ssid = nm.current_ssid().await;
let res = nm.connect(&ssid, None, creds).await;