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.
It connects with Device::connect now and for every reconnect, so
identifier must match exactly, as Device::connect describes. Returns
Error::InvalidConfig if options are invalid
(ReconnectOptions::validate), and otherwise the errors of
Device::connect.
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.
Connects as connect does, with the same errors.
Sourcepub fn cancel_reconnect(&self)
pub fn cancel_reconnect(&self)
Cancel any ongoing reconnection attempts.
A backoff wait or a connect in progress ends at once, and the reconnect
returns Error::Cancelled, as do the operations that were waiting for
it, except those still waiting when
reset_cancellation() is called, which
start a new reconnect. The flag stays set until
reset_cancellation(): until then, an
operation that needs to reconnect also returns Error::Cancelled.
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 device
isn’t connected, this reconnects first. If the closure fails with a
connection error (not connected, a timeout, a failed connection or a
Bluetooth link error), this reconnects once and calls the closure again,
so f can run twice. Any other error is returned as it is, without
reconnecting.
§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<()>
Close the connection and connect again, waiting between attempts with
the backoff of this device’s ReconnectOptions.
Operations that fail while this runs wait for it instead of starting
their own reconnect. A call made while another reconnect is running
waits for that one instead of starting its own, and shares its result:
Ok if it connected, Error::Cancelled if
cancel_reconnect() stopped it (a call still
waiting when reset_cancellation() is
called starts a new reconnect instead), and Error::NotConnected if it
gave up or disconnect() stopped it.
cancel_reconnect() and disconnect() end a reconnect at once, and it
returns Error::Cancelled. After max_attempts failed attempts it
returns Error::Timeout and the state is ConnectionState::Failed.
The old connection is closed first, so until a reconnect succeeds
name() returns None and address()
returns the identifier.
If cancel_reconnect() was called before this method, it returns
Error::Cancelled without connecting. Call
reset_cancellation() first to clear a
previous cancellation.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnect from the device.
Stops a reconnect in progress, which then never installs a new
connection, and closes the connection. It never waits for a reconnect’s
backoff or connect: it waits only for operations already running on the
connection to finish, and for a connection that is already being closed
(up to 5 s). The state is ConnectionState::Disconnected afterwards
even if closing fails; the error is still returned. An operation that
was waiting for the stopped reconnect returns Error::NotConnected
(Error::Cancelled while cancel_reconnect() is in effect). An
operation started after this returns connects again, as before.
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.