From cf162de26a2da11e8da99c4c3bf7292aae2827ef Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 04 Apr 2018 00:09:14 +0100 Subject: [PATCH] Add volume command and start on 'rooms' command --- Cargo.lock | 4 +++- src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- cmake-build-debug/CMakeFiles/clion-log.txt | 1 + 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1574911..19af588 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -873,6 +873,7 @@ [[package]] name = "sonos" version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -894,7 +895,7 @@ "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", - "sonos 0.1.2", + "sonos 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1390,6 +1391,7 @@ "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" +"checksum sonos 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ab7687fa5a7a6cf8de8622192b78e6011f89b52e38a6cccd97fbf37fb0a9ea2e" "checksum ssdp 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "93b04f1a9ca9db318b90e581cf6bdc9a6827983c3943c2087e26572d7b778d29" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "91b52877572087400e83d24b9178488541e3d535259e04ff17a63df1e5ceff59" diff --git a/src/main.rs b/src/main.rs index f3c1987..5ca5f23 100644 --- a/src/main.rs +++ a/src/main.rs @@ -41,6 +41,10 @@ .help("hh:mm:ss/mm:ss") .required(true) .index(1))) + .subcommand(SubCommand::with_name("volume").about("Get or set the volume of the speaker") + .arg(Arg::with_name("VOLUME") + .help("Percent volume to set speaker to 0-100") + .index(1))) .subcommand(SubCommand::with_name("rooms").about("List all of your speakers")) } @@ -110,6 +114,13 @@ i.to_string() }) }, + ("volume", Some(sub)) => { + if let Some(volume) = sub.value_of("VOLUME") { + speaker.set_volume(volume.parse().unwrap()); + } else { + info!("{}", speaker.volume().unwrap()); + } + }, ("seek", Some(sub)) => { let a = sub.value_of("TIMESTAMP").expect("timestamp"); @@ -145,9 +156,25 @@ print!("{}\r", " ".repeat(TWO.len())); stdout().flush().unwrap(); }); + + let devices = sonos::discover().unwrap(); + + let mut rooms = std::collections::HashMap::new(); + + for device in devices { + let coordinator = device.coordinator().unwrap(); - let devices = sonos::discover(); - println!("{:#?}", devices); + let mut room = rooms.entry(coordinator).or_insert(Vec::new()); + room.push(device); + } + + for (key, value) in rooms { + info!("Controller: {}", key); + + for device in value { + info!("d: {}", device.name); + } + } }, _ => { panic!(); @@ -168,7 +195,7 @@ pub fn new(speaker: &Speaker) -> Result { let track = speaker.track()?; - Ok(Track { + Ok(Self { title: track.title, artist: track.artist, album: track.album, @@ -192,6 +219,32 @@ const PROG_BAR_LEN: usize = 25; let percent_played = ((self.running_time.as_secs() as f64 / self.duration.as_secs() as f64) * PROG_BAR_LEN as f64) as usize; write!(f, " [{}{}]", "\u{2587}".repeat(percent_played), "-".repeat(PROG_BAR_LEN - percent_played)) + } +} + +#[derive(Serialize, Deserialize, Debug)] +struct Volume { + volume: u8, +} + +impl Volume { + pub fn new(vol: u8) -> Volume { + Self { + volume: vol, + } + } +} + +impl std::fmt::Display for Volume { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + const MAX_VOLUME: usize = 100; + const PROG_BAR_LEN: usize = 25; + + write!(f, "\u{1F50A} {}/{}", self.volume, MAX_VOLUME)?; + + let percent = (self.volume as usize / MAX_VOLUME) * PROG_BAR_LEN; + + write!(f, " [{}{}]", "\u{2587}".repeat(percent), "-".repeat(PROG_BAR_LEN - percent)) } } diff --git a/cmake-build-debug/CMakeFiles/clion-log.txt b/cmake-build-debug/CMakeFiles/clion-log.txt new file mode 100644 index 0000000..4eeb54c 100644 --- /dev/null +++ a/cmake-build-debug/CMakeFiles/clion-log.txt @@ -1,0 +1,1 @@ +CMakeLists.txt not found in /Volumes/Laptop/Code/sonos-cli -- rgit 0.1.5