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
impl MijiaSession
sourcepub async fn new(
) -> Result<(impl Future<Output = Result<(), SpawnError>>, Self), BluetoothError>
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.
sourcepub async fn get_sensors(&self) -> Result<Vec<SensorProps>, BluetoothError>
pub async fn get_sensors(&self) -> Result<Vec<SensorProps>, BluetoothError>
Get a list of all Mijia sensors which have currently been discovered.
sourcepub async fn get_time(&self, id: &DeviceId) -> Result<SystemTime, MijiaError>
pub async fn get_time(&self, id: &DeviceId) -> Result<SystemTime, MijiaError>
Get the current time of the sensor.
sourcepub async fn set_time(
&self,
id: &DeviceId,
time: SystemTime
) -> Result<(), MijiaError>
pub async fn set_time( &self, id: &DeviceId, time: SystemTime ) -> Result<(), MijiaError>
Set the current time of the sensor.
sourcepub async fn get_temperature_unit(
&self,
id: &DeviceId
) -> Result<TemperatureUnit, MijiaError>
pub async fn get_temperature_unit( &self, id: &DeviceId ) -> Result<TemperatureUnit, MijiaError>
Get the temperature unit which the sensor uses for its display.
sourcepub async fn set_temperature_unit(
&self,
id: &DeviceId,
unit: TemperatureUnit
) -> Result<(), BluetoothError>
pub async fn set_temperature_unit( &self, id: &DeviceId, unit: TemperatureUnit ) -> Result<(), BluetoothError>
Set the temperature unit which the sensor uses for its display.
sourcepub async fn get_comfort_level(
&self,
id: &DeviceId
) -> Result<ComfortLevel, MijiaError>
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.
sourcepub async fn set_comfort_level(
&self,
id: &DeviceId,
comfort_level: &ComfortLevel
) -> Result<(), MijiaError>
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.
sourcepub async fn get_history_range(
&self,
id: &DeviceId
) -> Result<Range<u32>, MijiaError>
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.
sourcepub async fn delete_history(&self, id: &DeviceId) -> Result<(), BluetoothError>
pub async fn delete_history(&self, id: &DeviceId) -> Result<(), BluetoothError>
Delete all historical data stored on the sensor.
sourcepub async fn get_last_history_record(
&self,
id: &DeviceId
) -> Result<HistoryRecord, MijiaError>
pub async fn get_last_history_record( &self, id: &DeviceId ) -> Result<HistoryRecord, MijiaError>
Get the last historical record stored on the sensor.
sourcepub async fn start_notify_history(
&self,
id: &DeviceId,
start_index: Option<u32>
) -> Result<(), BluetoothError>
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.
sourcepub async fn stop_notify_history(
&self,
id: &DeviceId
) -> Result<(), BluetoothError>
pub async fn stop_notify_history( &self, id: &DeviceId ) -> Result<(), BluetoothError>
Stop receiving historical records from the sensor.
sourcepub async fn get_all_history(
&self,
id: &DeviceId
) -> Result<Vec<Option<HistoryRecord>>, MijiaError>
pub async fn get_all_history( &self, id: &DeviceId ) -> Result<Vec<Option<HistoryRecord>>, MijiaError>
Try to get all historical records for the sensor.
sourcepub async fn start_notify_sensor(
&self,
id: &DeviceId
) -> Result<(), BluetoothError>
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()
.
sourcepub async fn event_stream(
&self
) -> Result<impl Stream<Item = MijiaEvent>, BluetoothError>
pub async fn event_stream( &self ) -> Result<impl Stream<Item = MijiaEvent>, BluetoothError>
Get a stream of reading/history/disconnected events for all sensors.