aranet_core/traits.rs
1//! Trait abstractions for Aranet device operations.
2//!
3//! This module provides the [`AranetDevice`] trait that abstracts over
4//! real Bluetooth devices and mock devices for testing.
5
6use aranet_types::{CurrentReading, DeviceInfo, DeviceType, HistoryRecord};
7
8use crate::error::Result;
9use crate::history::{HistoryInfo, HistoryOptions};
10use crate::settings::{CalibrationData, MeasurementInterval};
11
12/// Trait abstracting Aranet device operations.
13///
14/// This trait enables writing code that works with both real Bluetooth devices
15/// and mock devices for testing. Implement this trait for any type that can
16/// provide Aranet sensor data.
17///
18/// # Example
19///
20/// ```ignore
21/// use aranet_core::{AranetDevice, Result};
22///
23/// async fn print_reading<D: AranetDevice>(device: &D) -> Result<()> {
24/// let reading = device.read_current().await?;
25/// println!("CO2: {} ppm", reading.co2);
26/// Ok(())
27/// }
28/// ```
29#[allow(async_fn_in_trait)]
30pub trait AranetDevice: Send + Sync {
31 // --- Connection Management ---
32
33 /// Check if the device is connected.
34 async fn is_connected(&self) -> bool;
35
36 /// Connect to the device.
37 ///
38 /// For devices that are already connected, this should be a no-op.
39 /// For devices that support reconnection, this should attempt to reconnect.
40 ///
41 /// The default implementation returns `Ok(())` for backwards compatibility.
42 async fn connect(&self) -> Result<()> {
43 Ok(())
44 }
45
46 /// Disconnect from the device.
47 async fn disconnect(&self) -> Result<()>;
48
49 // --- Device Identity ---
50
51 /// Get the device name, if available.
52 fn name(&self) -> Option<&str>;
53
54 /// Get the device address or identifier.
55 ///
56 /// On Linux/Windows this is typically the MAC address.
57 /// On macOS this is a UUID since MAC addresses are not exposed.
58 fn address(&self) -> &str;
59
60 /// Get the detected device type, if available.
61 fn device_type(&self) -> Option<DeviceType>;
62
63 // --- Current Readings ---
64
65 /// Read the current sensor values.
66 async fn read_current(&self) -> Result<CurrentReading>;
67
68 /// Read device information (model, serial, firmware version, etc.).
69 async fn read_device_info(&self) -> Result<DeviceInfo>;
70
71 /// Read the current RSSI (signal strength) in dBm.
72 ///
73 /// More negative values indicate weaker signals.
74 /// Typical values range from -30 (strong) to -90 (weak).
75 async fn read_rssi(&self) -> Result<i16>;
76
77 // --- Battery ---
78
79 /// Read the battery level (0-100).
80 async fn read_battery(&self) -> Result<u8>;
81
82 // --- History ---
83
84 /// Get information about stored history.
85 async fn get_history_info(&self) -> Result<HistoryInfo>;
86
87 /// Download all historical readings.
88 async fn download_history(&self) -> Result<Vec<HistoryRecord>>;
89
90 /// Download historical readings with custom options.
91 async fn download_history_with_options(
92 &self,
93 options: HistoryOptions,
94 ) -> Result<Vec<HistoryRecord>>;
95
96 // --- Settings ---
97
98 /// Get the current measurement interval.
99 async fn get_interval(&self) -> Result<MeasurementInterval>;
100
101 /// Set the measurement interval.
102 async fn set_interval(&self, interval: MeasurementInterval) -> Result<()>;
103
104 /// Read calibration data from the device.
105 async fn get_calibration(&self) -> Result<CalibrationData>;
106}