Struct DeviceManager

Source
pub struct DeviceManager { /* private fields */ }
Expand description

Manager for multiple Aranet devices.

Implementations§

Source§

impl DeviceManager

Source

pub fn new() -> Self

Create a new device manager.

Source

pub fn with_event_capacity(capacity: usize) -> Self

Create a manager with custom event capacity.

Source

pub fn with_config(config: ManagerConfig) -> Self

Create a manager with full configuration.

Source

pub fn events(&self) -> &EventDispatcher

Get the event dispatcher for subscribing to events.

Source

pub fn config(&self) -> &ManagerConfig

Get the manager configuration.

Source

pub async fn scan(&self) -> Result<Vec<DiscoveredDevice>>

Scan for available devices.

Source

pub async fn scan_with_options( &self, options: ScanOptions, ) -> Result<Vec<DiscoveredDevice>>

Scan with custom options.

Source

pub async fn add_device(&self, identifier: &str) -> Result<()>

Add a device to the manager by identifier.

Source

pub async fn add_device_with_options( &self, identifier: &str, reconnect_options: ReconnectOptions, ) -> Result<()>

Add a device with custom reconnect options.

Source

pub async fn connect(&self, identifier: &str) -> Result<()>

Connect to a device.

This method performs an atomic connect-or-skip operation:

  • If the device doesn’t exist, it’s added and connected
  • If the device exists but is not connected, it’s connected
  • If the device is already connected, this is a no-op

The lock is held during the device entry update to prevent race conditions, but released during the actual BLE connection to avoid blocking other operations.

Source

pub async fn disconnect(&self, identifier: &str) -> Result<()>

Disconnect from a device.

Source

pub async fn remove_device(&self, identifier: &str) -> Result<()>

Remove a device from the manager.

Source

pub async fn device_ids(&self) -> Vec<String>

Get a list of all managed device IDs.

Source

pub async fn device_count(&self) -> usize

Get the number of managed devices.

Source

pub async fn connected_count(&self) -> usize

Get the number of connected devices (fast, doesn’t query BLE).

This returns the number of devices that have an active device handle, without querying the BLE stack. Use connected_count_verified for an accurate count that queries each device.

Source

pub async fn connected_count_verified(&self) -> usize

Get the number of connected devices (verified via BLE).

This method queries each device to verify its connection status. The lock is released before making BLE calls to avoid contention.

Source

pub async fn read_current(&self, identifier: &str) -> Result<CurrentReading>

Read current values from a specific device.

Source

pub async fn read_all(&self) -> HashMap<String, Result<CurrentReading>>

Read current values from all connected devices (in parallel).

This method releases the lock before performing async BLE operations, allowing other tasks to add/remove devices while reads are in progress. All reads are performed in parallel for maximum performance.

Source

pub async fn connect_all(&self) -> HashMap<String, Result<()>>

Connect to all known devices (in parallel).

Returns a map of device IDs to connection results.

Source

pub async fn disconnect_all(&self) -> HashMap<String, Result<()>>

Disconnect from all devices (in parallel).

Returns a map of device IDs to disconnection results.

Source

pub fn try_is_connected(&self, identifier: &str) -> Option<bool>

Check if a specific device is connected (fast, doesn’t query BLE).

This method attempts to check if a device has an active connection handle without blocking. Returns None if the lock couldn’t be acquired immediately, or Some(bool) indicating whether the device has a connection handle.

Note: This only checks if we have a device handle, not whether the actual BLE connection is still alive. Use is_connected for a verified check.

Source

pub async fn is_connected(&self, identifier: &str) -> bool

Check if a specific device is connected (verified via BLE).

The lock is released before making the BLE call.

Source

pub async fn get_device_info(&self, identifier: &str) -> Option<DeviceInfo>

Get device info for a specific device.

Source

pub async fn get_last_reading(&self, identifier: &str) -> Option<CurrentReading>

Get the last cached reading for a device.

Source

pub fn start_health_monitor( self: &Arc<Self>, cancel_token: CancellationToken, ) -> JoinHandle<()>

Start a background health check task that monitors connection status.

This spawns a task that periodically checks device connections and attempts to reconnect devices that have auto_reconnect enabled.

The task will run until the provided cancellation token is cancelled.

§Example
use tokio_util::sync::CancellationToken;

let manager = Arc::new(DeviceManager::new());
let cancel = CancellationToken::new();
let handle = manager.start_health_monitor(cancel.clone());

// Later, to stop the health monitor:
cancel.cancel();
handle.await.unwrap();

Trait Implementations§

Source§

impl Default for DeviceManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more