dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use std::sync::Arc;
use zbus::{Connection, Error, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.Device.Statistics",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Statistics {
    // Property accessors
    #[zbus(property)]
    fn refresh_rate_ms(&self) -> Result<u32, Error>;

    #[zbus(property)]
    fn set_refresh_rate_ms(&self, value: u32) -> Result<(), Error>;

    #[zbus(property)]
    fn tx_bytes(&self) -> Result<u64, Error>;

    #[zbus(property)]
    fn rx_bytes(&self) -> Result<u64, Error>;
}

pub struct DeviceStatistics {
    path: String,
    connection: Arc<Connection>,
}

impl DeviceStatistics {
    pub async fn new(connection: Arc<Connection>, path: String) -> Result<Self, Error> {
        let stats = DeviceStatistics { path, connection };
        // Test that the statistics interface exists by trying to get a property
        let proxy = StatisticsProxy::builder(&stats.connection)
            .path(stats.path.clone())?
            .build()
            .await?;
        // This should throw an error if the statistics interface doesn't exist
        let _ = proxy.refresh_rate_ms().await?;
        Ok(stats)
    }

    pub async fn refresh_rate_ms(&self) -> Result<u32, Error> {
        let proxy = StatisticsProxy::builder(&self.connection)
            .path(self.path.clone())?
            .build()
            .await?;

        proxy.refresh_rate_ms().await
    }

    pub async fn set_refresh_rate_ms(&self, value: u32) -> Result<(), Error> {
        let proxy = StatisticsProxy::builder(&self.connection)
            .path(self.path.clone())?
            .build()
            .await?;

        proxy.set_refresh_rate_ms(value).await
    }

    pub async fn tx_bytes(&self) -> Result<u64, Error> {
        let proxy = StatisticsProxy::builder(&self.connection)
            .path(self.path.clone())?
            .build()
            .await?;

        proxy.tx_bytes().await
    }

    pub async fn rx_bytes(&self) -> Result<u64, Error> {
        let proxy = StatisticsProxy::builder(&self.connection)
            .path(self.path.clone())?
            .build()
            .await?;

        proxy.rx_bytes().await
    }
}