Skip to content

Commit e3b593d

Browse files
committed
futures::Stream for station state and network connectivity.
Allows clients to get asynchronously notified on connectivity changes. `futures::Stream` for station state and network connectivity. Allows clients to get asynchronously notified on connectivity changes.
1 parent a687d57 commit e3b593d

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ zvariant = "5"
1414
uuid = { version = "1", features = ["v4"] }
1515
thiserror = "2.0.17"
1616
strum = {version = "0.27.2", features = ["derive"]}
17+
futures-lite = "2.6.1"
1718

1819
[dev-dependencies]
1920
clap = {version = "4.5.48", features = ["derive"]}

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use futures_lite::{Stream, StreamExt};
2+
use zbus::Proxy;
3+
use zvariant::OwnedValue;
4+
15
pub mod session;
26

37
pub mod station;
@@ -17,3 +21,15 @@ pub mod access_point;
1721
pub mod modes;
1822

1923
pub mod error;
24+
25+
async fn property_stream<'p, T: TryFrom<OwnedValue, Error = zvariant::Error> + Unpin>(
26+
proxy: Proxy<'p>,
27+
property_name: &'p str,
28+
) -> zbus::Result<impl Stream<Item = zbus::Result<T>> + Unpin> {
29+
Ok(Box::pin(
30+
proxy
31+
.receive_property_changed(property_name)
32+
.await
33+
.then(|property_changed| async move { property_changed.get().await }),
34+
))
35+
}

src/network.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{str::FromStr, sync::Arc};
22

3+
use futures_lite::{Stream, StreamExt};
34
use strum::EnumString;
45
use zbus::{Connection, Proxy, Result as ZbusResult};
56
use zvariant::{OwnedObjectPath, OwnedValue};
@@ -50,9 +51,18 @@ impl Network {
5051
}
5152

5253
pub async fn connected(&self) -> ZbusResult<bool> {
54+
self.connected_stream()
55+
.await?
56+
.next()
57+
.await
58+
.ok_or_else(|| zbus::Error::Unsupported)?
59+
}
60+
61+
pub async fn connected_stream(
62+
&self,
63+
) -> zbus::Result<impl Stream<Item = zbus::Result<bool>> + Unpin> {
5364
let proxy = self.proxy().await?;
54-
let is_connected: bool = proxy.get_property("Connected").await?;
55-
Ok(is_connected)
65+
crate::property_stream(proxy, "Connected").await
5666
}
5767

5868
pub async fn device(&self) -> ZbusResult<Device> {

src/station.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{collections::HashMap, str::FromStr, sync::Arc};
22

3+
use futures_lite::{Stream, StreamExt};
34
use strum::EnumString;
45
use zvariant::{OwnedObjectPath, OwnedValue, Value};
56

@@ -50,8 +51,18 @@ impl Station {
5051
}
5152

5253
pub async fn state(&self) -> zbus::Result<State> {
54+
self.state_stream()
55+
.await?
56+
.next()
57+
.await
58+
.ok_or_else(|| zbus::Error::Unsupported)?
59+
}
60+
61+
pub async fn state_stream(
62+
&self,
63+
) -> zbus::Result<impl Stream<Item = zbus::Result<State>> + Unpin> {
5364
let proxy = self.proxy().await?;
54-
proxy.get_property("State").await
65+
crate::property_stream(proxy, "State").await
5566
}
5667

5768
pub async fn connected_network(&self) -> zbus::Result<Option<Network>> {

0 commit comments

Comments
 (0)