Struct ReconnectingDevice

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn is_cancelled(&self) -> bool

Check if reconnection has been cancelled.

Source

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.

Source

pub async fn state(&self) -> ConnectionState

Get the current connection state.

Source

pub async fn is_connected(&self) -> bool

Check if currently connected.

Source

pub fn identifier(&self) -> &str

Get the identifier.

Source

pub async fn with_device<F, Fut, T>(&self, f: F) -> Result<T>
where F: Fn(&Device) -> Fut, Fut: Future<Output = 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?;
Source

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.

Source

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.

Source

pub async fn attempt_count(&self) -> u32

Get the number of reconnection attempts made.

Source

pub async fn name(&self) -> Option<String>

Get the device name, if available and connected.

Source

pub async fn address(&self) -> String

Get the device address (returns identifier if not connected).

Source

pub async fn device_type(&self) -> Option<DeviceType>

Get the detected device type, if available.

Trait Implementations§

Source§

impl AranetDevice for ReconnectingDevice

Source§

async fn is_connected(&self) -> bool

Check if the device is connected.
Source§

async fn connect(&self) -> Result<()>

Connect to the device. Read more
Source§

async fn disconnect(&self) -> Result<()>

Disconnect from the device.
Source§

fn name(&self) -> Option<&str>

Get the device name, if available.
Source§

fn address(&self) -> &str

Get the device address or identifier. Read more
Source§

fn device_type(&self) -> Option<DeviceType>

Get the detected device type, if available.
Source§

async fn read_current(&self) -> Result<CurrentReading>

Read the current sensor values.
Source§

async fn read_device_info(&self) -> Result<DeviceInfo>

Read device information (model, serial, firmware version, etc.).
Source§

async fn read_rssi(&self) -> Result<i16>

Read the current RSSI (signal strength) in dBm. Read more
Source§

async fn read_battery(&self) -> Result<u8>

Read the battery level (0-100).
Source§

async fn get_history_info(&self) -> Result<HistoryInfo>

Get information about stored history.
Source§

async fn download_history(&self) -> Result<Vec<HistoryRecord>>

Download all historical readings.
Source§

async fn download_history_with_options( &self, options: HistoryOptions, ) -> Result<Vec<HistoryRecord>>

Download historical readings with custom options.
Source§

async fn get_interval(&self) -> Result<MeasurementInterval>

Get the current measurement interval.
Source§

async fn set_interval(&self, interval: MeasurementInterval) -> Result<()>

Set the measurement interval.
Source§

async fn get_calibration(&self) -> Result<CalibrationData>

Read calibration data from the device.

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.

§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,