pub struct ReconnectingDevice { /* private fields */ }Expand description
A device wrapper that automatically handles reconnection.
This wrapper caches the device name and type upon initial connection so they
can be accessed synchronously via the AranetDevice trait, even while
reconnecting.
Implementations§
Source§impl ReconnectingDevice
impl ReconnectingDevice
Sourcepub async fn connect(
identifier: &str,
options: ReconnectOptions,
) -> Result<Self>
pub async fn connect( identifier: &str, options: ReconnectOptions, ) -> Result<Self>
Create a new reconnecting device wrapper.
Sourcepub async fn connect_with_events(
identifier: &str,
options: ReconnectOptions,
event_sender: EventSender,
) -> Result<Self>
pub async fn connect_with_events( identifier: &str, options: ReconnectOptions, event_sender: EventSender, ) -> Result<Self>
Create with an event sender for notifications.
Sourcepub fn cancel_reconnect(&self)
pub fn cancel_reconnect(&self)
Cancel any ongoing reconnection attempts.
This will cause the reconnect loop to exit on its next iteration.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if reconnection has been cancelled.
Sourcepub fn reset_cancellation(&self)
pub fn reset_cancellation(&self)
Reset the cancellation flag.
Call this before starting a new reconnection attempt if you want to clear
a previous cancellation. The reconnect() method will check if cancelled
at the start of each iteration, so this allows re-using a previously
cancelled ReconnectingDevice.
Sourcepub async fn state(&self) -> ConnectionState
pub async fn state(&self) -> ConnectionState
Get the current connection state.
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if currently connected.
Sourcepub fn identifier(&self) -> &str
pub fn identifier(&self) -> &str
Get the identifier.
Sourcepub async fn with_device<F, Fut, T>(&self, f: F) -> Result<T>
pub async fn with_device<F, Fut, T>(&self, f: F) -> Result<T>
Execute an operation, reconnecting if necessary.
The closure is called with a reference to the device. If the operation fails due to a connection issue, the device will attempt to reconnect and retry the operation.
§Example
let reading = device.with_device(|d| async { d.read_current().await }).await?;Sourcepub async fn reconnect(&self) -> Result<()>
pub async fn reconnect(&self) -> Result<()>
Attempt to reconnect to the device.
This loop can be cancelled by calling cancel_reconnect() from another task.
When cancelled, returns Error::Cancelled.
Note: If cancel_reconnect() was called before this method, reconnection
will still proceed. Call reset_cancellation() explicitly if you want to
clear a previous cancellation before starting a new reconnection attempt.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnect from the device.
Sourcepub async fn attempt_count(&self) -> u32
pub async fn attempt_count(&self) -> u32
Get the number of reconnection attempts made.
Sourcepub async fn address(&self) -> String
pub async fn address(&self) -> String
Get the device address (returns identifier if not connected).
Sourcepub async fn device_type(&self) -> Option<DeviceType>
pub async fn device_type(&self) -> Option<DeviceType>
Get the detected device type, if available.