Skip to content

Commit

Permalink
Adapt some missing web API methods to the new NetworkSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 15, 2024
1 parent d54eb8e commit fe95106
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
26 changes: 18 additions & 8 deletions rust/agama-server/src/network/system.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use super::{
error::NetworkStateError,
model::{Device, StateConfig},
model::{AccessPoint, Device, StateConfig},
NetworkAdapterError,
};
use crate::network::{
dbus::Tree,
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},
Expand Down Expand Up @@ -117,9 +114,7 @@ impl<T: Adapter + Send + 'static> NetworkSystem<T> {
/// 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<Action>,
actions: UnboundedSender<Action>,
}

// TODO: add a NetworkSystemError type
Expand Down Expand Up @@ -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<Vec<AccessPoint>, 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<T: Adapter> {
Expand Down
36 changes: 11 additions & 25 deletions rust/agama-server/src/network/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,11 @@ pub async fn network_service<T: Adapter + std::marker::Send + 'static>(
#[utoipa::path(get, path = "/network/state", responses(
(status = 200, description = "Get general network config", body = GenereralState)
))]
async fn general_state(State(state): State<NetworkState>) -> Json<GeneralState> {
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<NetworkState>,
) -> Result<Json<GeneralState>, NetworkError> {
let general_state = state.network.get_state().await?;
Ok(Json(general_state))
}

#[utoipa::path(put, path = "/network/state", responses(
Expand All @@ -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<AccessPoint>)
))]
async fn wifi_networks(State(state): State<NetworkState>) -> Json<Vec<AccessPoint>> {
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<NetworkState>,
) -> Result<Json<Vec<AccessPoint>>, 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(
Expand Down

0 comments on commit fe95106

Please sign in to comment.