pub struct DeviceManager { /* private fields */ }Expand description
Manager for multiple Aranet devices.
Implementations§
Source§impl DeviceManager
impl DeviceManager
Sourcepub fn with_event_capacity(capacity: usize) -> Self
pub fn with_event_capacity(capacity: usize) -> Self
Create a manager with custom event capacity.
Sourcepub fn with_config(config: ManagerConfig) -> Self
pub fn with_config(config: ManagerConfig) -> Self
Create a manager with full configuration.
Sourcepub fn events(&self) -> &EventDispatcher
pub fn events(&self) -> &EventDispatcher
Get the event dispatcher for subscribing to events.
Sourcepub fn config(&self) -> &ManagerConfig
pub fn config(&self) -> &ManagerConfig
Get the manager configuration.
Sourcepub async fn scan(&self) -> Result<Vec<DiscoveredDevice>>
pub async fn scan(&self) -> Result<Vec<DiscoveredDevice>>
Scan for available devices.
Sourcepub async fn scan_with_options(
&self,
options: ScanOptions,
) -> Result<Vec<DiscoveredDevice>>
pub async fn scan_with_options( &self, options: ScanOptions, ) -> Result<Vec<DiscoveredDevice>>
Scan with custom options.
Sourcepub async fn add_device(&self, identifier: &str) -> Result<()>
pub async fn add_device(&self, identifier: &str) -> Result<()>
Add a device to the manager by identifier.
Sourcepub async fn add_device_with_options(
&self,
identifier: &str,
reconnect_options: ReconnectOptions,
) -> Result<()>
pub async fn add_device_with_options( &self, identifier: &str, reconnect_options: ReconnectOptions, ) -> Result<()>
Add a device with custom reconnect options.
Sourcepub async fn connect(&self, identifier: &str) -> Result<()>
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.
Sourcepub async fn disconnect(&self, identifier: &str) -> Result<()>
pub async fn disconnect(&self, identifier: &str) -> Result<()>
Disconnect from a device.
Sourcepub async fn remove_device(&self, identifier: &str) -> Result<()>
pub async fn remove_device(&self, identifier: &str) -> Result<()>
Remove a device from the manager.
Sourcepub async fn device_ids(&self) -> Vec<String>
pub async fn device_ids(&self) -> Vec<String>
Get a list of all managed device IDs.
Sourcepub async fn device_count(&self) -> usize
pub async fn device_count(&self) -> usize
Get the number of managed devices.
Sourcepub async fn connected_count(&self) -> usize
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.
Sourcepub async fn connected_count_verified(&self) -> usize
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.
Sourcepub async fn read_current(&self, identifier: &str) -> Result<CurrentReading>
pub async fn read_current(&self, identifier: &str) -> Result<CurrentReading>
Read current values from a specific device.
Sourcepub async fn read_all(&self) -> HashMap<String, Result<CurrentReading>>
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.
Sourcepub async fn connect_all(&self) -> HashMap<String, Result<()>>
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.
Sourcepub async fn disconnect_all(&self) -> HashMap<String, Result<()>>
pub async fn disconnect_all(&self) -> HashMap<String, Result<()>>
Disconnect from all devices (in parallel).
Returns a map of device IDs to disconnection results.
Sourcepub fn try_is_connected(&self, identifier: &str) -> Option<bool>
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.
Sourcepub async fn is_connected(&self, identifier: &str) -> bool
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.
Sourcepub async fn get_device_info(&self, identifier: &str) -> Option<DeviceInfo>
pub async fn get_device_info(&self, identifier: &str) -> Option<DeviceInfo>
Get device info for a specific device.
Sourcepub async fn get_last_reading(&self, identifier: &str) -> Option<CurrentReading>
pub async fn get_last_reading(&self, identifier: &str) -> Option<CurrentReading>
Get the last cached reading for a device.
Sourcepub fn start_health_monitor(
self: &Arc<Self>,
cancel_token: CancellationToken,
) -> JoinHandle<()>
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();