Skip to main content

sonos_state/
lib.rs

1//! Internal implementation detail of [`sonos-sdk`](https://crates.io/crates/sonos-sdk). Not intended for direct use.
2//!
3//! Sonos State Management
4//!
5//! A sync-first state management system for Sonos devices.
6//!
7//! # Features
8//!
9//! - **Sync API**: All operations are synchronous - no async/await required
10//! - **Type-safe State**: Strongly typed properties with automatic change detection
11//! - **Change Events**: Blocking iterator over property changes
12//! - **Watch Pattern**: Register for property changes, iterate to receive them
13//!
14//! # Quick Start
15//!
16//! ```rust,ignore
17//! use sonos_state::{StateManager, Volume, SpeakerId};
18//! use sonos_discovery;
19//!
20//! // Create state manager (sync - no .await!)
21//! let manager = StateManager::new()?;
22//!
23//! // Add discovered devices
24//! let devices = sonos_discovery::get();
25//! manager.add_devices(devices)?;
26//!
27//! // Get current property value
28//! let speaker_id = SpeakerId::new("RINCON_123");
29//! if let Some(vol) = manager.get_property::<Volume>(&speaker_id) {
30//!     println!("Current volume: {}%", vol.0);
31//! }
32//!
33//! // Watch for changes
34//! manager.register_watch(&speaker_id, "volume");
35//!
36//! // Blocking iteration over changes
37//! for event in manager.iter() {
38//!     println!("{} changed on {}", event.property_key, event.speaker_id);
39//!     if let Some(vol) = manager.get_property::<Volume>(&event.speaker_id) {
40//!         println!("New volume: {}%", vol.0);
41//!     }
42//! }
43//! ```
44//!
45//! # Non-blocking Iteration
46//!
47//! ```rust,ignore
48//! // Check for events without blocking
49//! for event in manager.iter().try_iter() {
50//!     println!("Event: {:?}", event);
51//! }
52//!
53//! // Wait with timeout
54//! if let Some(event) = manager.iter().recv_timeout(Duration::from_secs(1)) {
55//!     println!("Got event: {:?}", event);
56//! }
57//! ```
58
59// Core modules
60pub mod model;
61pub mod property;
62
63// Event decoding
64pub mod decoder;
65
66// Event processing
67pub(crate) mod event_worker;
68
69// Sync-first API
70pub mod iter;
71pub mod speaker;
72pub mod state;
73
74// Error types
75pub mod error;
76
77// ============================================================================
78// Re-exports - Main API
79// ============================================================================
80
81// State manager
82pub use state::{
83    ChangeEvent, ChangeSource, EventInitFn, StateManager, StateManagerBuilder, WriteOutcome,
84    WriteStamp,
85};
86
87// Change iterator
88pub use iter::ChangeIterator;
89
90// Properties
91pub use property::{
92    Bass, CurrentTrack, GroupInfo, GroupMembership, GroupMute, GroupVolume, GroupVolumeChangeable,
93    Loudness, Mute, PlaybackState, Position, Property, Scope, SonosProperty, Topology, Treble,
94    Volume,
95};
96
97// Model types
98pub use model::{GroupId, SpeakerId, SpeakerInfo};
99
100// Event decoder
101pub use decoder::{
102    decode_event, decode_topology_event, parse_track_metadata, DecodedChanges, PropertyChange,
103    TopologyChanges,
104};
105
106// Error types
107pub use error::{Result, StateError};
108
109// ============================================================================
110// Prelude
111// ============================================================================
112
113/// Commonly used types for convenient importing
114pub mod prelude {
115    // Properties
116    pub use crate::property::{
117        Bass, CurrentTrack, GroupMembership, GroupMute, GroupVolume, GroupVolumeChangeable,
118        Loudness, Mute, PlaybackState, Position, Property, Scope, Topology, Treble, Volume,
119    };
120
121    // Model types
122    pub use crate::model::{GroupId, SpeakerId, SpeakerInfo};
123
124    // State management
125    pub use crate::decoder::PropertyChange;
126    pub use crate::iter::ChangeIterator;
127    pub use crate::state::{ChangeEvent, ChangeSource, StateManager};
128
129    // Error types
130    pub use crate::error::{Result, StateError};
131}