Add volume command and start on 'rooms' command
Diff
Cargo.lock | 4 +-
cmake-build-debug/CMakeFiles/clion-log.txt | 1 +-
src/main.rs | 59 +++++++++++++++++++++++++++++--
3 files changed, 60 insertions(+), 4 deletions(-)
@@ -873,6 +873,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[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 @@ dependencies = [
"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 @@ dependencies = [
"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"
@@ -0,0 +1 @@
CMakeLists.txt not found in /Volumes/Laptop/Code/sonos-cli
@@ -41,6 +41,10 @@ fn argparse<'a, 'b>() -> clap::App<'a, 'b> {
.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 @@ fn main() {
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");
@@ -146,8 +157,24 @@ fn main() {
stdout().flush().unwrap();
});
let devices = sonos::discover();
println!("{:#?}", devices);
let devices = sonos::discover().unwrap();
let mut rooms = std::collections::HashMap::new();
for device in devices {
let coordinator = device.coordinator().unwrap();
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 @@ impl Track {
pub fn new(speaker: &Speaker) -> Result<Track, sonos::Error> {
let track = speaker.track()?;
Ok(Track {
Ok(Self {
title: track.title,
artist: track.artist,
album: track.album,
@@ -196,6 +223,32 @@ impl std::fmt::Display for Track {
}
#[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))
}
}
#[derive(Serialize, Deserialize, Debug)]
struct Info {
pub ip: IpAddr,
pub model: String,