🏡 index : ~doyle/sonos.rs.git

author Jordan Doyle <jordan@doyle.wf> 2017-12-09 21:10:29.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-12-09 21:10:29.0 +00:00:00
commit
2b5e6d9fe9a192ac868d830d777644f744ceff80 [patch]
tree
451b93c628a2abf109315e978dd3520af68e6727
parent
e270a6d78fd0e922061aea781a6c7dac0c314333
download
2b5e6d9fe9a192ac868d830d777644f744ceff80.tar.gz

Use Durations rather than hh:mm:ss strings



Diff

 Cargo.toml                |  2 +-
 src/device.rs             | 38 +++++++++++++++++++++++++++++++-------
 tests/integration_test.rs |  2 +-
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 1697f95..f0ee944 100644
--- a/Cargo.toml
+++ a/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "sonos"
version = "0.1.0"
authors = ["Jordan Doyle <jordanjd@amazon.com>"]
authors = ["Jordan Doyle <jordan@9t9t9.com>"]
license = "MIT"

[dependencies]
diff --git a/src/device.rs b/src/device.rs
index b70e956..ef7b8ce 100644
--- a/src/device.rs
+++ a/src/device.rs
@@ -1,13 +1,14 @@
extern crate regex;
extern crate reqwest;
extern crate xmltree;

use std::net::IpAddr;
use std::io::Read;
use std::time::Duration;
use error::*;
use self::xmltree::Element;
use self::reqwest::header::{ContentType, Headers};
use self::regex::Regex;
use std::io::Read;

#[derive(Debug)]
pub struct Speaker {
@@ -28,8 +29,8 @@
    pub album: String,
    pub queue_position: u64,
    pub uri: String,
    pub duration: String,
    pub relative_time: String,
    pub duration: Duration,
    pub running_time: Duration,
}

#[derive(Debug, PartialEq)]
@@ -269,7 +270,15 @@
    }

    /// Seek to a time on the current track

    pub fn seek(&self, hours: &u8, minutes: &u8, seconds: &u8) -> Result<()> {
    pub fn seek(&self, time: &Duration) -> Result<()> {
        const SECS_PER_MINUTE: u64 = 60;
        const MINS_PER_HOUR: u64 = 60;
        const SECS_PER_HOUR: u64 = 3600;

        let seconds = time.as_secs() % SECS_PER_MINUTE;
        let minutes = (time.as_secs() / SECS_PER_MINUTE) % MINS_PER_HOUR;
        let hours = time.as_secs() / SECS_PER_HOUR;

        self.soap(
            "MediaRenderer/AVTransport/Control",
            "urn:schemas-upnp-org:service:AVTransport:1",
@@ -503,6 +512,21 @@
        let metadata = metadata
            .get_child("item")
            .chain_err(|| "Failed to parse XML from Sonos controller")?;

        // convert the given hh:mm:ss to a Duration
        let duration: Vec<u64> = element_to_string(resp.get_child("TrackDuration")
            .chain_err(|| "Failed to get track duration")?)
            .splitn(3, ":")
            .map(|s| s.parse::<u64>().unwrap())
            .collect();
        let duration = Duration::from_secs((duration[0] * 3600) + (duration[1] * 60) + duration[2]);

        let running_time: Vec<u64> = element_to_string(resp.get_child("RelTime")
            .chain_err(|| "Failed to get relative time")?)
            .splitn(3, ":")
            .map(|s| s.parse::<u64>().unwrap())
            .collect();
        let running_time = Duration::from_secs((running_time[0] * 3600) + (running_time[1] * 60) + running_time[2]);

        Ok(Track {
            title: element_to_string(metadata
@@ -520,10 +544,8 @@
                .unwrap(),
            uri: element_to_string(resp.get_child("TrackURI")
                .chain_err(|| "Failed to get track uri")?),
            duration: element_to_string(resp.get_child("TrackDuration")
                .chain_err(|| "Failed to get track duration")?),
            relative_time: element_to_string(resp.get_child("RelTime")
                .chain_err(|| "Failed to get relative time")?),
            duration,
            running_time,
        })
    }
}
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index 61d7188..db9aff4 100644
--- a/tests/integration_test.rs
+++ a/tests/integration_test.rs
@@ -83,7 +83,7 @@
#[test]
#[should_panic]
fn fail_on_set_invalid_volume() {
    sonos::discover().unwrap()[0]
    get_speaker()
        .set_volume(101)
        .expect_err("Didn't fail on invalid volume");
}