From fe95106d0d635b4487b691211e0636c185f81a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Mon, 15 Apr 2024 09:49:53 +0100 Subject: [PATCH] Adapt some missing web API methods to the new NetworkSystem --- rust/agama-server/src/network/system.rs | 26 ++++++++++++------ rust/agama-server/src/network/web.rs | 36 ++++++++----------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/rust/agama-server/src/network/system.rs b/rust/agama-server/src/network/system.rs index 6e93285fd2..28de1eed73 100644 --- a/rust/agama-server/src/network/system.rs +++ b/rust/agama-server/src/network/system.rs @@ -1,6 +1,6 @@ use super::{ error::NetworkStateError, - model::{Device, StateConfig}, + model::{AccessPoint, Device, StateConfig}, NetworkAdapterError, }; use crate::network::{ @@ -8,10 +8,7 @@ use crate::network::{ model::{Connection, GeneralState}, Action, Adapter, NetworkState, }; -use agama_lib::{ - error::ServiceError, - network::{settings::NetworkConnection, types::DeviceType}, -}; +use agama_lib::{error::ServiceError, network::types::DeviceType}; use std::{error::Error, sync::Arc}; use tokio::sync::{ mpsc::{self, error::SendError, UnboundedReceiver, UnboundedSender}, @@ -117,9 +114,7 @@ impl NetworkSystem { /// It hides the details of the message-passing behind a convenient API. #[derive(Clone)] pub struct NetworkSystemClient { - /// Channel to talk to the server. This field is temporarily public, but it will - /// be set to private once web interface is adapted. - pub actions: UnboundedSender, + actions: UnboundedSender, } // TODO: add a NetworkSystemError type @@ -203,6 +198,21 @@ impl NetworkSystemClient { let result = rx.await?; Ok(result?) } + + /// Returns the collection of access points. + pub async fn get_access_points(&self) -> Result, NetworkSystemError> { + let (tx, rx) = oneshot::channel(); + self.actions.send(Action::GetAccessPoints(tx))?; + let access_points = rx.await?; + Ok(access_points) + } + + pub async fn wifi_scan(&self) -> Result<(), NetworkSystemError> { + let (tx, rx) = oneshot::channel(); + self.actions.send(Action::RefreshScan(tx)).unwrap(); + let result = rx.await?; + Ok(result?) + } } struct NetworkSystemServer { diff --git a/rust/agama-server/src/network/web.rs b/rust/agama-server/src/network/web.rs index ee2589cf53..cfec6fa602 100644 --- a/rust/agama-server/src/network/web.rs +++ b/rust/agama-server/src/network/web.rs @@ -91,17 +91,11 @@ pub async fn network_service( #[utoipa::path(get, path = "/network/state", responses( (status = 200, description = "Get general network config", body = GenereralState) ))] -async fn general_state(State(state): State) -> Json { - let (tx, rx) = oneshot::channel(); - state - .network - .actions - .send(Action::GetGeneralState(tx)) - .unwrap(); - - let state = rx.await.unwrap(); - - Json(state) +async fn general_state( + State(state): State, +) -> Result, NetworkError> { + let general_state = state.network.get_state().await?; + Ok(Json(general_state)) } #[utoipa::path(put, path = "/network/state", responses( @@ -119,28 +113,20 @@ async fn update_general_state( #[utoipa::path(get, path = "/network/wifi", responses( (status = 200, description = "List of wireless networks", body = Vec) ))] -async fn wifi_networks(State(state): State) -> Json> { - let (tx, rx) = oneshot::channel(); - state.network.actions.send(Action::RefreshScan(tx)).unwrap(); - let _ = rx.await.unwrap(); - let (tx, rx) = oneshot::channel(); - state - .network - .actions - .send(Action::GetAccessPoints(tx)) - .unwrap(); - - let access_points = rx.await.unwrap(); +async fn wifi_networks( + State(state): State, +) -> Result>, NetworkError> { + state.network.wifi_scan().await?; + let access_points = state.network.get_access_points().await?; let mut networks = vec![]; - for ap in access_points { if !ap.ssid.to_string().is_empty() { networks.push(ap); } } - Json(networks) + Ok(Json(networks)) } #[utoipa::path(get, path = "/network/devices", responses(