Struct mijia::MijiaSession

source ·
pub struct MijiaSession {
    pub bt_session: BluetoothSession,
}
Expand description

A wrapper around a Bluetooth session which adds some methods for dealing with Mijia sensors. This is the main entry point to the library.

The underlying Bluetooth session may still be accessed. For example, to scan for sensors:

// Create a new session. This establishes the D-Bus connection. In this case we ignore the join
// handle, as we don't intend to run indefinitely.
let (_, session) = MijiaSession::new().await?;

// Start scanning for Bluetooth devices, and wait a few seconds for some to be discovered.
session.bt_session.start_discovery().await?;
time::sleep(Duration::from_secs(5)).await;

// Get the list of sensors which are currently known.
let sensors = session.get_sensors().await?;

Fields§

§bt_session: BluetoothSession

The underlying BluetoothSession. You can use this for Bluetooth operations which are not specific to Mijia sensors, such as connecting and disconnecting.

Implementations§

source§

impl MijiaSession

source

pub async fn new( ) -> Result<(impl Future<Output = Result<(), SpawnError>>, Self), BluetoothError>

Returns a tuple of (join handle, Self). If the join handle ever completes then you’re in trouble and should probably restart the process.

source

pub async fn get_sensors(&self) -> Result<Vec<SensorProps>, BluetoothError>

Get a list of all Mijia sensors which have currently been discovered.

source

pub async fn get_time(&self, id: &DeviceId) -> Result<SystemTime, MijiaError>

Get the current time of the sensor.

source

pub async fn set_time( &self, id: &DeviceId, time: SystemTime ) -> Result<(), MijiaError>

Set the current time of the sensor.

source

pub async fn get_temperature_unit( &self, id: &DeviceId ) -> Result<TemperatureUnit, MijiaError>

Get the temperature unit which the sensor uses for its display.

source

pub async fn set_temperature_unit( &self, id: &DeviceId, unit: TemperatureUnit ) -> Result<(), BluetoothError>

Set the temperature unit which the sensor uses for its display.

source

pub async fn get_comfort_level( &self, id: &DeviceId ) -> Result<ComfortLevel, MijiaError>

Get the comfort level configuration which determines when the sensor displays a happy face.

source

pub async fn set_comfort_level( &self, id: &DeviceId, comfort_level: &ComfortLevel ) -> Result<(), MijiaError>

Set the comfort level configuration which determines when the sensor displays a happy face.

source

pub async fn get_history_range( &self, id: &DeviceId ) -> Result<Range<u32>, MijiaError>

Get the range of indices for historical data stored on the sensor.

source

pub async fn delete_history(&self, id: &DeviceId) -> Result<(), BluetoothError>

Delete all historical data stored on the sensor.

source

pub async fn get_last_history_record( &self, id: &DeviceId ) -> Result<HistoryRecord, MijiaError>

Get the last historical record stored on the sensor.

source

pub async fn start_notify_history( &self, id: &DeviceId, start_index: Option<u32> ) -> Result<(), BluetoothError>

Start receiving historical records from the sensor.

Arguments
  • id: The ID of the sensor to request records from.
  • start_index: The record index to start at. If this is not specified then all records which have not yet been received from the sensor since it was connected will be requested.
source

pub async fn stop_notify_history( &self, id: &DeviceId ) -> Result<(), BluetoothError>

Stop receiving historical records from the sensor.

source

pub async fn get_all_history( &self, id: &DeviceId ) -> Result<Vec<Option<HistoryRecord>>, MijiaError>

Try to get all historical records for the sensor.

source

pub async fn start_notify_sensor( &self, id: &DeviceId ) -> Result<(), BluetoothError>

Assuming that the given device ID refers to a Mijia sensor device and that it has already been connected, subscribe to notifications of temperature/humidity readings, and adjust the connection interval to save power.

Notifications will be delivered as events by MijiaSession::event_stream().

source

pub async fn event_stream( &self ) -> Result<impl Stream<Item = MijiaEvent>, BluetoothError>

Get a stream of reading/history/disconnected events for all sensors.

Trait Implementations§

source§

impl Debug for MijiaSession

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.